首页 文章

将rufus-scheduler安排到heroku上的worker dyno上

提问于
浏览
1

我想使用rufus-scheduler来运行后台任务 . 我目前有一个 Worker dyno为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

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?)要进入睡眠状态(以及带有它的调度程序) .

    EDIT 1

    提示:您可以使用$ DYNO变量(https://devcenter.heroku.com/articles/dynos#local-environment-variables)来识别您所在的dyno .

    if ENV['DYNO'].match(/^worker\./)
      # ...
    end
    

相关问题