1

我想使用 rufus-scheduler 来运行后台任务。我目前有一个用于 Sidekiq 的工作人员测功机,理想情况下只是想安排该过程。根据https://github.com/jmettraux/rufus-scheduler#so-rails上的文档测试 rufus-scheduler

require 'rufus-scheduler'

# Let's use the rufus-scheduler singleton
#
s = Rufus::Scheduler.singleton


# Stupid recurrent task...
#
s.every '1s' do
  Rails.logger.info "hello, it's #{Time.now}"
end

它出现在 web 和 worker 上,我希望它使用 worker 进程。我将如何实现这一目标?

示例输出为:

2014-10-17T00:30:29.382689+00:00 app[web.1]: hello, it's 2014-10-17 00:30:29 +0000
2014-10-17T00:30:29.609192+00:00 app[worker.1]: hello, it's 2014-10-17 00:30:29 +0000
4

1 回答 1

1

像下面这样的东西可以做到这一点:

if running_on_worker_process? # you have to implement that one yourself.
  require 'rufus-scheduler'
  s = Rufus::Scheduler.singleton
  s.every '1d' do
    Rails.logger.info "taking out the garbage..."
  end
end

当心你的工作进程(dyno?)进入睡眠状态(以及调度程序)。

编辑 1

提示:您可以使用 $DYNO 变量 ( https://devcenter.heroku.com/articles/dynos#local-environment-variables ) 来识别您使用的是哪个测功机。

if ENV['DYNO'].match(/^worker\./)
  # ...
end
于 2014-10-17T01:05:29.553 回答