Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule background task with Sidekiq

I have a Rails 3 app deployed heroku. I have a Sidekiq worker at app/workers/task_worker.rb:

class TaskWorker
  include Sidekiq::Worker
  def perform
    ...
  end
end

How to schedule execution of TaskWorker.perform_async daily at 12:01 a.m?

like image 588
Annie Avatar asked Jul 04 '13 13:07

Annie


People also ask

How do I run Sidekiq in the background?

To run sidekiq, you will need to open a terminal, navigate to your application's directory, and start the sidekiq process, exactly as you would start a web server for the application itself. When the command executes you will see a message that sidekiq has started.

How does Sidekiq scheduler work?

Sidekiq Scheduler is a lightweight job scheduling extension for Sidekiq. It uses Rufus Scheduler under the hood, that is itself an in-memory scheduler. Sidekiq Scheduler extends Sidekiq by starting a Rufus Scheduler thread in the same process, loading and maintaining the schedules for it.


2 Answers

You might want to have a look at sidetiq too. https://github.com/tobiassvn/sidetiq The gem supports complex timing expressions via the ice_cube gem.

I personally found comfortable to have a gem that would integrate seemlessly with sidekiq.

Something like that should work:

class TaskWorker
  include Sidekiq::Worker
  include Sidetiq::Schedulable

  recurrence do
    daily.hour_of_day(0).minute_of_hour(1)
  end

  def perform
    # do magic
  end
end

Careful though when using this gem since there are some performance related issues with some time expressions. https://github.com/tobiassvn/sidetiq/wiki/Known-Issues. The expression I gave you should circumvent this issue though.

like image 124
The Mighty Rubber Duck Avatar answered Oct 14 '22 02:10

The Mighty Rubber Duck


I don't like the overhead Sidetiq adds to Sidekiq so I sought out a different solution.

Apparently Heroku has a little-known, but free scheduler addon that allows you to run rake tasks every 10 minutes, hourly or daily. This is Heroku's answer to cron jobs and it's nice that it's a free add-on. It should work for most non-critical scheduling.

Heroku states in their docs that the scheduler is a "Best Effort" service which may occasionally (but rarely) miss a scheduled event. If it is critical that this job is run, you'll probably want to use a custom clock process. Custom clock processes are more reliable but they count toward your dyno hours. (And as such, incur fees just like any other process.)

Currently it looks like clockwork is the recommended clock process on Heroku.

like image 34
markquezada Avatar answered Oct 14 '22 00:10

markquezada