简体   繁体   English

将rufus-scheduler调度到heroku的dyno上

[英]schedule rufus-scheduler onto worker dyno on heroku

I'd like to use rufus-scheduler to have a background task run. 我想使用rufus-scheduler运行后台任务。 I currently have a worker dyno for Sidekiq and would ideally just like to schedule onto that process. 我目前为Sidekiq配备了一个工人dyno,理想情况下只是想安排该过程。 Testing the rufus-scheduler as per the documentation at https://github.com/jmettraux/rufus-scheduler#so-rails 根据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

It comes out on both the web and worker and I'd like it to use the worker process. 它同时在Web和worker上发布,我希望它使用worker进程。 How would I achieve that? 我将如何实现?

Sample output is: 样本输出为:

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

Something like the following could do it: 像下面这样的事情可以做到:

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

Beware your worker process (dyno?) going to sleep (and the scheduler with it). 当心您的工作进程(dyno?)要入睡(以及与此相关的调度程序)。

EDIT 1 编辑1

Tip: you can use the $DYNO variable ( https://devcenter.heroku.com/articles/dynos#local-environment-variables ) to identify which dyno you're on. 提示:您可以使用$ DYNO变量( https://devcenter.heroku.com/articles/dynos#local-environment-variables )来确定您使用的是dyno。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM