Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Correctly log errors during job initialisation (fixes #316) #374

Merged
merged 1 commit into from
Aug 22, 2022

Conversation

kinglozzer
Copy link
Collaborator

The alternative to this is changing the the signature of handleBrokenJobException() to make the second param optional with = null, which is technically a BC-break. This method of instantiating a job object is exactly what initialiseJob does:

// create the job class
$impl = $jobDescriptor->Implementation;
/** @var QueuedJob $job */
$job = Injector::inst()->create($impl);
/* @var $job QueuedJob */
if (!$job) {
throw new Exception("Implementation $impl no longer exists");
}

@GuySartorelli
Copy link
Collaborator

GuySartorelli commented May 31, 2022

Are there some steps I can follow to ensure a job breaks during initialisation in order to test this?
And is this something we could write unit tests for?

This method of instantiating a job object is exactly what initialiseJob does

Maybe that should be extracted into its own method so we can reuse the same code? That way if the way we instantiate these changes, we can remain consistent.

@kinglozzer
Copy link
Collaborator Author

To recreate this you just need a job to throw an exception in its setup() method.

I don’t think this fix is complete after all because if the exception shown above (Implementation $impl no longer exists) is thrown then the exception still won’t be logged properly. I wonder if a better approach would be to wrap this in its own try/catch:

$job = $this->initialiseJob($jobDescriptor);

That would allow us to handle the exception-during-initialisation flow differently, so no need to adjust method signatures and break BC

@kinglozzer kinglozzer force-pushed the 316-error-in-init branch from c370b27 to 1f97a1c Compare June 1, 2022 15:30
@kinglozzer
Copy link
Collaborator Author

Okay I’ve pushed an alternative solution

@kinglozzer kinglozzer force-pushed the 316-error-in-init branch from 1f97a1c to c2580f3 Compare June 8, 2022 10:04
@kinglozzer kinglozzer force-pushed the 316-error-in-init branch from c2580f3 to 8e4ba4f Compare June 8, 2022 10:05
@GuySartorelli
Copy link
Collaborator

Looks good from a code perspective. Will try to make some time tomorrow to test it locally just to be sure it does catch those errors.

Copy link
Collaborator

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta love when you see a comment from yourself a week later that says "tomorrow" on it.... sorry about that.

Looks good locally except:

Doesn't log the exception to the job's messages

This does catch and mark the exception and marks the job as broken which is a big improvement on the current state of things - but it doesn't add the exception information to the job's messages like exceptions later in the lifecycle do.

Looks like that's because addJobHandlersToLogger() isn't called until later, which expects a QueuedJob instance (which we obviously don't have at this stage) to pass to QueuedJobHandler.

I don't think QueuedJobHandler should explicitly require a QueuedJob instance (messages are stored on the QueuedJobDescriptor anyway which we do have) - but since it does, we can't just add that handler to the logger.

Next steps

I think we should create a new handler (to either be replaced by QueuedJobHandler or else to be used for this specific situation - preferable the former with QueuedJobHandler being deprecated) which doesn't require a QueuedJob instance.

I don't know if that should be done as part of this PR (it could be considered part of "correctly" logging errors) or as a follow-up. If it's a follow-up, then I'm happy with this PR as-is. It is at least an improvement on the current state of things.

@GuySartorelli
Copy link
Collaborator

@kinglozzer do you have any thoughts about whether logging the message should be done with or after thIs PR?

@kinglozzer
Copy link
Collaborator Author

I think it’d need to be done separately - we can’t switch out QueuedJobHandler for one that doesn’t require a QueuedJob instance without breaking BC (e.g. if someone is using Injector to inject their own handler that uses the QueuedJob object)

Copy link
Collaborator

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well locally. Doesn't log these errors in the message for the job, but that can be follow-up work as discussed.
At least it logs them correctly in the logger which is an improvement over the existing behaviour.

@GuySartorelli
Copy link
Collaborator

I have created #384 for the follow-up work of passing the error as a message into the job to be viewed in the CMS UI.

@GuySartorelli
Copy link
Collaborator

Tagged as 4.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants