Skip to content

Instantly share code, notes, and snippets.

@kineticac
Created November 17, 2010 20:45
Show Gist options
  • Save kineticac/704047 to your computer and use it in GitHub Desktop.
Save kineticac/704047 to your computer and use it in GitHub Desktop.
Here's a quick way to run a delayed_job job like a cron job. The ensure block is the key to making sure it runs again.
class SampleJob
def perform
begin
# do all your work in the begin block.
puts "hello world"
rescue Exception => e
# rescue any errors so that you know something went wrong. Email yourself the error if you need.
error_msg = "#{Time.now} ERROR (SampleJob#perform): #{e.message} - (#{e.class})\n#{(e.backtrace or []).join("\n")}"
puts error_msg
ensure
# make sure the job gets requeued. the ensure block is always executed, even if there was an exception thrown and caught by rescue.
Delayed::Job.enqueue SampleJob.new, 5, 1.hour.from_now
end
end
end
@Davidslv
Copy link

Davidslv commented Aug 5, 2013

If you do it that way it will never rescue, because of ensure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment