Is Rails's "delayed_job" for cron task really?
Asked Answered
D

6

10

delayed_job is at http://github.com/collectiveidea/delayed_job

Can delayed_job have the ability to do cron task? Such as running a script every night at 1am. Or run a script every 1 hour.

If not, what are the suitable gems that can do that? And can it be monitored remotely using a browser, and have logging of success and error?

Didactics answered 2/9, 2010 at 23:55 Comment(0)
D
21

I worked on a project that tried to use DelayedJob to schedule future items. It sucked.

Instead I recommend you use the whenever gem:

http://github.com/javan/whenever

Whenever is a Ruby gem that provides a clear syntax for defining cron jobs. It outputs valid cron syntax and can even write your crontab file for you. It is designed to work well with Rails applications and can be deployed with Capistrano. Whenever works fine independently as well.

Code looks like this (from github)

  every 3.hours do
    runner "MyModel.some_process"
    rake "my:rake:task"
    command "/usr/bin/my_great_command"
  end

  every 1.day, :at => '4:30 am' do
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda"
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
    runner "Task.do_something_great"
  end

Here's a RailsCast video on how to use it.

And the corresponding ASCIICast.

Doud answered 3/9, 2010 at 1:18 Comment(3)
Why/how did delayed_job suck for you?Crow
I found that DJ workers were freezing constantly, and they were hard to keep running. I have since switched to Sidekiq and never looked back. It's perfect. Works absolutely fantasticly. I wish my company would fork over the money for the added funtionality of Sidekiq Pro just because it's so awesome.Chess
It does suck. I've been there too.Vulture
T
4

I think cron is a better tool for this than delayed_job. I've used it in a project before, and it really excels at running at task in the background or at a particular time. But, for recurring tasks that happen at regular times, I think cron is the best tool.

Check out whenever (and its Railscast) to easily schedule cron jobs that can run rake tasks (or thor, or shell scripts, or anything else.) You can use the rake tasks to update your models and then have some sort of dashboard controller that looks at the various statuses.

Tallbott answered 3/9, 2010 at 0:36 Comment(0)
C
3

You can also use the ClockWork gem: https://github.com/adamwiggins/clockwork-rails-dj

Clockwork runs as a separate daemon and can be used to trigger jobs of any sort that either getting added to a job queueing system or run right away.

Use Delayed_Job for what it's good for, a job queueing system which can be distributed over multiple nodes (or not). Use something else to add jobs to the queue at the right time.

I was using rake(or runner)/cron/whenever gem to schedule background tasks but was finding my server load was just so high because I would be getting hit constantly with rake/runner loading up the rails environment. Delayed_Job workers are your rails daemons that stay running so you aren't constantly firing up Rails every time a background task is required.

Chess answered 3/4, 2012 at 21:59 Comment(0)
S
2

Whenever works great.

I also like rufus-scheduler

/config/initializers/task_scheduler.rb

Then in that file:

scheduler = Rufus::Scheduler.start_new  

scheduler.every("1m") do  
   DailyDigest.send_digest!  
end 

I originally found this posted here

I've tried it and it works well.

update

Now that I look back at that link it's pretty much the only rails company that I would want to work for. They have made some many gems and add such much to the community. Not to mention they have a huge team!

Subulate answered 3/9, 2010 at 1:23 Comment(0)
S
2

I run multiple cron delayed_jobs for nightly statistic and report generating and also for data scrapping at certain intervals. Here's how I do it:

https://aaronvb.com/articles/recurring-delayed-job-with-cron.html

Spadework answered 29/10, 2010 at 0:8 Comment(0)
C
1

I created a gem for this:

https://github.com/sellect/delayed_cron

It works with sidekiq and delayed_job currently. Looking to add resque soon. I know this is a bit late, but it does pretty much exactly what you were looking for.

Calvincalvina answered 25/5, 2014 at 5:5 Comment(2)
The delayed_cron gem has not worked correctly with delayed_job since at least 2014-03-06. Even after fixing DelayedCron::Jobs::DelayedJob#perform the gem still does not work correctly: cron jobs with an interval are not rescheduled (whether or not the job completed successfully).Hienhieracosphinx
@Hienhieracosphinx I personally do not use delayed_job as the background processor with delayed_cron, I prefer sidekiq. However, there have been updates to the DelayedJob class inside of delayed_cron in versions 0.2.2 and 0.2.3. Which version of the gem are you using? Either way I plan to update delayed_cron to use Rail's ActiveJob to abstract the job adaptors so compatibility with different background processors shouldn't be a problem in the future.Calvincalvina

© 2022 - 2024 — McMap. All rights reserved.