Is it possible to do the following using delayed_job:
Define a class called
Tasks
Have a method in Tasks run after every 5 minutes:
Tasks.do_processing
When the next 5 minute cycle comes around, then run
Tasks.do_processing
only if the previousdo_processing
has completed
Is this something I have to create on my own or can delayed_job (or some other gem/plugin) do this?
Ps. I know about OS-level cron jobs, but if I used that then it would mean that each time 开发者_开发问答the cron "fired" it would re-load the entire Rails environment, whereas delayed_job only needs to load it once.
You may consider creating a Rake task or other similar standalone processes that takes care of this functionality, and then wrapping it up with Daemons:
What is Daemons?
Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands.
If you want, you can also use daemons to run blocks of ruby code in a daemon process and to control these processes from the main application.
Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash.
If you'd like to do something similar to this :
class MyJob
run_every 1.day
def perform
# code to run ...
end
end
then try out this code: https://gist.github.com/1024726
Thought I might add that the delayed_job_recurring gem is an extension of the gist on @kares's answer.
Usage example:
class MyTask
include Delayed::RecurringJob
run_every 1.day
run_at '11:00am'
timezone 'US/Pacific'
queue 'slow-jobs'
def perform
# Do some work here!
end
end
And schedule it. In a rails app, you might put this in an initializer:
MyTask.schedule!
Been using a version of this in production for a while now for DJ 2.1 . No hackery, uses Delayed Jobs built in hooks. Just make sure you add column named 'queue' to the delayed jobs table, which I believe is something DJ 3 added to the table anyways.
You can use https://github.com/codez/delayed_cron_job which is a Delayed Job plugin to set periodical jobs.
Recurring delayed_job:
http://rifkifauzi.wordpress.com/2010/07/29/8/
精彩评论