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

Add GoodJob.preserve_job_records = :on_unhandled_error option to only preserve jobs that errored #145

Merged
merged 3 commits into from
Sep 21, 2020

Conversation

morgoth
Copy link
Collaborator

@morgoth morgoth commented Sep 18, 2020

Adds option :on_error to preserve jobs in database only on final error and delete otherwise.

This is done in 2 commits. First one just introduces the functionality, the second one changes options to :never, :always, :on_error with backward compatibility.

closes #136

@morgoth
Copy link
Collaborator Author

morgoth commented Sep 18, 2020

I also have a bit of refactoring ready on https://github.com/tiramizoo/good_job/compare/preserve-on-error...tiramizoo:refactoring?expand=1
but don't want to put too much into this PR

@bensheldon
Copy link
Owner

@morgoth Thank you for tackling this! I'm really excited to add the functionality.

I'd like to retain (not deprecate) the Boolean values for preserve_job_records, and only add the one on_error symbol right now. I'm not ready to commit fully to symbolic values right now.

@morgoth
Copy link
Collaborator Author

morgoth commented Sep 18, 2020

ok, I removed the second commit

Copy link
Owner

@bensheldon bensheldon left a comment

Choose a reason for hiding this comment

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

@morgoth I have some feedback, but I think we're close.

BTW, after writing out my code suggestion in this review, I went back to your refactoring branch and my suggestion looks nearly identical to your branch :-)

@@ -209,10 +209,12 @@ def perform

if rescued_error && GoodJob.reperform_jobs_on_standard_error
save!
elsif rescued_error && GoodJob.preserve_job_records == :on_error
update!(finished_at: Time.current)
Copy link
Owner

Choose a reason for hiding this comment

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

I think this needs to be testing against self.error(any error that occurred during the job), rather than just rescued_error (errors that bubble up to the GoodJob backend).... or I think we should rename it specifically to be "on_backend_error"/"on_unhandled_error" (naming is hard).

Assuming that self.error matches with your needs for this, I think the entire block might be clearer with further rewriting (apologies it was messy in the first place). Something like:

if rescued_error && GoodJob.reperform_jobs_on_standard_error
  save!
elsif GoodJob.preserve_job_records == true || (error && GoodJob.preserve_job_records == :on_error)
  self.finished_at = Time.current
  save!
else
  destroy!
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually wanted saving only on "rescued" aka "unhandled" error, so I renamed the config to be :on_unhandled_error, as otherwise it would create a bit of noise to have multiple records of the same job/error.
It would be not clear if those entries are coming from the same AJ job or different ones.
This is matching the behavior of other AJ queue adapters.

I think storing on every error could also be an option (maybe another config value?) with some nice web UI grouping, so it's clear that given group of job exception is coming from the same AJ job.

Copy link
Owner

Choose a reason for hiding this comment

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

That is great. That makes me think (as a separate PR) that we should rename the configuration GoodJob. reperform_jobs_on_standard_error as GoodJob.reperform_jobs_on_unhandled_error. That configuration name was always unwieldy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@bensheldon Can I prepare a PR for renaming reperform_jobs_on_standard_error to reperform_jobs_on_unhandled_error with deprecation warning on old usage?

Copy link
Owner

Choose a reason for hiding this comment

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

@morgoth that would be great!

Question: what are your thoughts about an even briefer retry_on_unhandled_error?

@bensheldon
Copy link
Owner

@morgoth re:refactoring. you drew my attention to the unfortunate fact that the local variable error is shadowing GoodJob::Job#error 😓

Copy link
Collaborator Author

@morgoth morgoth left a comment

Choose a reason for hiding this comment

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

I applied my refactoring branch already. Hope it is more clear now.

@@ -209,10 +209,12 @@ def perform

if rescued_error && GoodJob.reperform_jobs_on_standard_error
save!
elsif rescued_error && GoodJob.preserve_job_records == :on_error
update!(finished_at: Time.current)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually wanted saving only on "rescued" aka "unhandled" error, so I renamed the config to be :on_unhandled_error, as otherwise it would create a bit of noise to have multiple records of the same job/error.
It would be not clear if those entries are coming from the same AJ job or different ones.
This is matching the behavior of other AJ queue adapters.

I think storing on every error could also be an option (maybe another config value?) with some nice web UI grouping, so it's clear that given group of job exception is coming from the same AJ job.

Copy link
Owner

@bensheldon bensheldon left a comment

Choose a reason for hiding this comment

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

👍 thank you for this feature! I will merge and then make a quick PR to address my style problems.

retry_or_discard_error = GoodJob::CurrentExecution.error_on_retry ||
GoodJob::CurrentExecution.error_on_discard
result_error = nil
result, result_error = nil, result if result.is_a?(Exception) # rubocop:disable Style/ParallelAssignment
Copy link
Owner

Choose a reason for hiding this comment

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

Style: I think the stylecop should be preserved and this done as 2 lines.

save!
elsif GoodJob.preserve_job_records == true || (GoodJob.preserve_job_records == :on_unhandled_error && unhandled_error)
update!(finished_at: Time.current)
Copy link
Owner

Choose a reason for hiding this comment

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

Style: If a model object has been previously dirtied, save should be used rather than update.

@bensheldon bensheldon changed the title Preserve job on error Add GoodJob.preserve_job_records = :on_unhandled_error option to only preserve jobs that errored Sep 21, 2020
@bensheldon bensheldon merged commit 0b3e0e6 into bensheldon:main Sep 21, 2020
@bensheldon bensheldon added the enhancement New feature or request label Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve only failed jobs
2 participants