2014-10-17 49 views
1

我想使用rufus-scheduler來運行後臺任務。我目前有一個Sidekiq的工人測功機,理想情況下只是想安排進程。在https://github.com/jmettraux/rufus-scheduler#so-rails將rufus-scheduler安排到heroku上的工人dyno上

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 

測試魯弗斯調度按照文檔它散發出來的網絡和工人都對,我想它使用的工作進程。我將如何實現這一目標?

示例輸出:

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

像下面這樣可以做到這一點:

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 

當心你的工作進程(測功機?)去睡覺(與它的調度)。

編輯1

提示:你可以使用$ DYNO變量(https://devcenter.heroku.com/articles/dynos#local-environment-variables),以確定哪些DYNO你的。

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

thx,這就是蹭,我將如何確定哪個進程調用調度程序?我非常確信工人的動物不會入睡。但如果你有更多的信息,將不勝感激。在網絡方面,我使用了一臺2x Web Dyno,我不認爲它會入睡。 – timpone 2014-10-17 02:30:23

+0

所以這個問題和rufus-scheduler或rails沒有關係...... – jmettraux 2014-10-17 02:49:19

+0

我認爲它的確如此,但是每個人都有權發表意見 – timpone 2014-10-17 06:33:39