Is this a good way to discard a job when it times out? #1365
-
Can anyone see any issues with the following code I added to one of my jobs to handle timeouts? JobTimeoutError = Class.new StandardError
around_perform do |_job, block|
Timeout.timeout 225, JobTimeoutError do
block.call
rescue JobTimeoutError
GoodJob::Job.find(_job.job_id).discard_job "Timed out"
end
end |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I wouldn't recommend calling I think you could do this entirely within ActiveJob using JobTimeoutError = Class.new StandardError
discard_on JobTimeoutError
around_perform do |_job, block|
Timeout.timeout 225, JobTimeoutError do
block.call
end
end Of course, using Ruby |
Beta Was this translation helpful? Give feedback.
-
@bensheldon never mind - I found a solution - will post it shortly. |
Beta Was this translation helpful? Give feedback.
I wouldn't recommend calling
GoodJob::Job#discard_job
in the middle of job execution because I'm not sure if the job subsequently finishing would override that and it would retry (it's a private method so I would generally say "undefined behavior subject to change").I think you could do this entirely within ActiveJob using
discard_on
. My recommendation would look something like this:Of course, using Ruby
Timeout
is unsafe because it could raise in the middle of something and thus ruin a shared resource, but I have stuff…