簡體   English   中英

如何使用Clockwork Rails調度程序Gem?

[英]How do I use the Clockwork Rails scheduler Gem?

我在Clockwork調度程序過程的語法上遇到問題。 我實際上有與該線程中討論的問題類似的問題,但從未得到完全回答( 如何使用Rails發條gem運行rake任務?

當我使用“ heroku rake send_notifications”進行測試時,我的“ scheduler.rake”工作正常。 我的clock.rb進程也正在運行,因為它將每30秒觸發一次。 但是,我的clock.rb語法存在問題,無法在我的“ rake.scheduler”中正確運行“ send_notifications”任務。 看起來是這樣的:

# scheduler.rake
desc "This task is called by the Heroku scheduler add-on"
task :send_notifications => :environment do

   puts "this test is working correctly!"

end

這是clock.rb的樣子:

require File.expand_path('../../config/boot',        __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'

include Clockwork

every(30.seconds, 'Send notifications') {
   # Heroku::API.new.post_ps('pocket-pal', 'rake scheduler:send_notifications')
    rake scheduler:send_notifications
}

如您所見,我已經嘗試過使用Heroku API和調用rake進行分離的過程。

當我使用Heroku API時,出現以下錯誤:

ERROR -- : uninitialized constant Heroku (NameError)

調用rake時,出現以下錯誤:

ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)

有誰知道這兩種方法的正確語法是什么?

此答案有效,但是您需要完全遵循其字符串語法 因此,在您的情況下:

every(30.seconds, 'Send Notifications') {
  `rake scheduler:send_notifications`
}

這意味着要確保使用` `來包裝rake任務,而不要使用" "因為這會使一些人絆倒。

來自Hereko的有關實現發條的文檔可能會有所幫助

帶有Clockwork的Ruby中的計划作業和自定義時鍾進程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM