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

When not preserving job records, ensure all prior executions are deleted after successful retry #719

Merged
merged 3 commits into from
Oct 11, 2022

Conversation

ylansegal
Copy link
Contributor

When records are configured to be preserved on on_unhandled_error, and later retried, the previous executions stay in the database and become "orphaned". I believe they should be deleted on a successful retry. This would bring it inline with the behavior for destroying a failed job (eg. via the UI) which results in previous executions also being deleted.

This is my first contribution to this repo. I took a stab at writing a spec that exposes the issue and a fix via a ActiveRecord callback. I am open to suggestions for improvement.

Thank you for making good_job available and open source!

@bensheldon
Copy link
Owner

@ylansegal thank you for opening up this PR. I think you've discovered a bug 🎉

I need to think about this a bit more. I don't think the fix here is quite right because I think if a a job is retried (which will generate a new execution) this PR will delete that new execution, which isn't desired.

Here is where the Execution#destroy is called after performing the job (the subsequent execution gets created/enqueued as part of the ActiveJob performance):

result = execute
job_error = result.handled_error || result.unhandled_error
self.error = [job_error.class, ERROR_MESSAGE_SEPARATOR, job_error.message].join if job_error
if result.unhandled_error && GoodJob.retry_on_unhandled_error
save!
elsif GoodJob.preserve_job_records == true || (result.unhandled_error && GoodJob.preserve_job_records == :on_unhandled_error)
self.finished_at = Time.current
save!
else
destroy!
end

I'm thinking this logic needs to be unrolled to more clearly separate "This is what happened with the job/execution (e.g. succeeded, retried, discarded/handled, unhandled" and then within that have the logic of "and based on the configuration, this is what should happen with the record". I might push up into your branch unless you want to dig into that more.

@ylansegal
Copy link
Contributor Author

I need to think about this a bit more. I don't think the fix here is quite right because I think if a a job is retried (which will generate a new execution) this PR will delete that new execution, which isn't desired.

I definitely had not considered retries fully. My thinking was that failed jobs are preserved, which would not trigger the callback. That seems to be an assumption on how I have configured GoodJob in my project.

I'm thinking this logic needs to be unrolled to more clearly separate "This is what happened with the job/execution (e.g. succeeded, retried, discarded/handled, unhandled" and then within that have the logic of "and based on the configuration, this is what should happen with the record". I might push up into your branch unless you want to dig into that more.

Feel free to push to that branch! You definitely have more knowledge of the internals.

@bensheldon bensheldon changed the title Ensure prior executions are deleted on successful retry When not preserving job records, ensure all prior executions are deleted after successful retry Oct 10, 2022
@bensheldon bensheldon merged commit f726bfb into bensheldon:main Oct 11, 2022
@bensheldon
Copy link
Owner

Released in v3.4.8 🎉 Thank you for the fix 🙌🏻

@bensheldon
Copy link
Owner

bensheldon commented Oct 20, 2022

@ylansegal sorry, I need to revert this because of an unexpected issue in #728. I will figure out a better implementation and get this behavior re-added.

@ylansegal
Copy link
Contributor Author

Thank you @bensheldon

@bensheldon bensheldon added the bug Something isn't working label Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants