Created
November 17, 2010 20:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you do it that way it will never rescue, because of ensure.