我如何使用Clockwork Rails调度程序Gem? [英] How do I use the Clockwork Rails scheduler Gem?
问题描述
我在Clockwork调度程序过程的语法上遇到问题。我实际上有类似的问题,在这个线程讨论,但从来没有完全回答(我如何使用Rails的clockwork gem来运行rake任务?)
我的'scheduler.rake'工作正常当我用'heroku rake send_notifications'测试它时。我的clock.rb进程正在工作,因为它会每隔30秒触发一次。但是,我在使用clock.rb的语法来正确运行'rake.scheduler'中的'send_notifications'任务时遇到了问题。下面是它的样子:
#scheduler.rake
desc这个任务由Heroku调度器add- on
任务:send_notifications => :环境做
放这个测试工作正常!
end
以下是clock.rb的样子:
需要File.expand_path('../../ config / boot',__FILE__)
需要File.expand_path('。 ./../config/environment',__FILE__)
需要'发条'
包括发条
每个(30.seconds,'发送通知'){
#Heroku :: API.new.post_ps('pocket-pal','rake scheduler:send_notifications')
rake scheduler:send_notifications
}
正如你所看到的,我已经尝试过使用Heroku API并调用rake的分离进程。
当我使用Heroku API时,出现以下错误:
错误 - :未初始化的常量Heroku(NameError)
当我调用rake时,出现以下错误:
ERROR - :未定义的局部变量或方法send_notifications for main:Object(NameError)
有人知道这两种方法的正确语法吗? a href =https://stackoverflow.com/a/11516026/3448554>这个答案很有用,但是你需要严格遵循它的字符串语法。所以在你的情况下:
every(30.seconds,'发送通知'){
`rake scheduler:send_notifications`
}
这意味着确保使用
``
用于包装rake任务,而不是,因为这会让几个人绊倒。
I'm having problems with the syntax for the Clockwork scheduler process. I'm actually having similar issues to what is discussed in this thread but never fully answered(How do I use Rails clockwork gem to run rake tasks?)
My 'scheduler.rake' is working correctly when I test it with a 'heroku rake send_notifications'. My clock.rb process is working too as it will trigger every 30 seconds. However, I'm having issues with the clock.rb's syntax to correctly run the 'send_notifications' task in my 'rake.scheduler'. Here's what it looks like:
# 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
Here's what clock.rb looks like:
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 }
As you can see I've tried with a detached process using the Heroku API and invoking rake.
When I use the Heroku API, I get this error:
ERROR -- : uninitialized constant Heroku (NameError)
When I invoke rake, I get the following error:
ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)
Does anyone know what the correct syntax is for either approach?
解决方案This answer works, however you need to follow its string syntax exactly. So in your case:
every(30.seconds, 'Send Notifications') { `rake scheduler:send_notifications` }
That means making sure to use
` `
for wrapping the rake task, not" "
as that's tripped a few people up.这篇关于我如何使用Clockwork Rails调度程序Gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!