我有一个部署了 Heroku 的 Rails 3 应用程序。我有一个 Sidekiq 工人在app/workers/task_worker.rb
:
class TaskWorker
include Sidekiq::Worker
def perform
...
end
end
如何安排TaskWorker.perform_async
每天凌晨 12:01 执行?
我有一个部署了 Heroku 的 Rails 3 应用程序。我有一个 Sidekiq 工人在app/workers/task_worker.rb
:
class TaskWorker
include Sidekiq::Worker
def perform
...
end
end
如何安排TaskWorker.perform_async
每天凌晨 12:01 执行?
你可能也想看看sidetiq
。https://github.com/tobiassvn/sidetiq gem 通过 gem 支持复杂的时序表达式ice_cube
。
我个人觉得拥有一个可以与 sidekiq 无缝集成的 gem 很舒服。
像这样的东西应该工作:
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
使用此 gem 时要小心,因为某些时间表达式存在一些与性能相关的问题。https://github.com/tobiassvn/sidetiq/wiki/Known-Issues。我给你的表达方式应该可以绕过这个问题。
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.
我说的是显而易见的,但是在那个时候每晚都有一个调用 Sidekiq 作业的 Cron 作业有什么问题?