Annie
Annie

Reputation: 3190

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?

Upvotes: 5

Views: 5766

Answers (3)

markquezada
markquezada

Reputation: 8535

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.

Upvotes: 4

The Mighty Rubber Duck
The Mighty Rubber Duck

Reputation: 4488

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.

Upvotes: 5

Henley Wing Chiu
Henley Wing Chiu

Reputation: 22535

I'm stating the obvious, but what's wrong with having a Cron Job that invokes the Sidekiq job every night at that time?

Upvotes: -3

Related Questions