Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recurring schedule using delayed_job

Is it possible to do the following using delayed_job:

  1. Define a class called Tasks

  2. Have a method in Tasks run after every 5 minutes: Tasks.do_processing

  3. When the next 5 minute cycle comes around, then run Tasks.do_processing only if the previous do_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.

like image 348
Zabba Avatar asked Mar 19 '11 21:03

Zabba


2 Answers

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

like image 161
kares Avatar answered Oct 11 '22 12:10

kares


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.

like image 45
Michelle Tilley Avatar answered Oct 11 '22 12:10

Michelle Tilley