Not really an issue, just a question.
What's the recommended way of just exiting a job early? Raising an exception? or just returning something, or...?
if @order.status == 1
# End Job Here
else
#Continue
end
Thanks in advance.
fyi, we prefer questions like this to be here in issues so others can find/partake in the discussion ;)
I'll let @mperham chime in, but I don't think you'd want to raise, as that could end up being expensive & then you have the whole retry thing to deal with.
Making your jobs idempotent is a good idea so this is a great question. I don't think the question is any different from "how do I exit a method early?" A job really is just executing the perform method so
def perform(order_id)
@order = Order.find order_id
return if @order.status == 1
...
end
would seem to be fine to me.
You guys are quick to respond. Thanks for the quick answer.
I figured returning should do the trick, just wanted to make sure I didn't run into anything funky.
Sorry to revive this incredibly old issue but I have one question: when logic is pulled out into separate methods on the worker what would be the best way to end early in that case?
For instance:
def die(reason)
Rails.logger.info("#{reason} – dying.")
end
def perform(params)
if params[:is_update]
die("This is an update, not supported *yet*") and return
end
begin
@sale = Spree::Sale.find(params[:sale_id])
rescue ActiveRecord::RecordNotFound
die("The sale with ID #{params[:sale_id]} no longer exists") and return
end
...removed for brevity
end
I would like my little die() method to kill everything without marking the job as a failure. Is there a special exception I can raise, or some other thing I can do to terminate the job without die() and return everywhere?
throw/catch?
On Feb 12, 2020, at 12:04, Michael Whalen notifications@github.com wrote:

Sorry to revive this incredibly old issue but I have one question: when logic is pulled out into separate methods on the worker what would be the best way to end early in that case?For instance:
def die(reason)
Rails.logger.info("#{reason} – dying.")
enddef perform(params)
if params[:is_update]
die("This is an update, not supported yet") and return
endbegin
@sale = Spree::Sale.find(params[:sale_id])
rescue ActiveRecord::RecordNotFound
die("The sale with ID #{params[:sale_id]} no longer exists") and return
end...removed for brevity
end
I would like my little die() method to kill everything without marking the job as a failure. Is there a special exception I can raise, or some other thing I can do to terminate the job without die() and return everywhere?—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
@mperham yeah about 3 minutes after I submitted this comment I came to that realization -- sorry for the noise on this ticket and thank you for Sidekiq!
Most helpful comment
Making your jobs idempotent is a good idea so this is a great question. I don't think the question is any different from "how do I exit a method early?" A job really is just executing the
performmethod sowould seem to be fine to me.