Skip to content

Is this a good way to discard a job when it times out? #1365

Answered by bensheldon
isaac asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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 Timeout is unsafe because it could raise in the middle of something and thus ruin a shared resource, but I have stuff…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@isaac
Comment options

Answer selected by isaac
Comment options

You must be logged in to vote
1 reply
@isaac
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants