如何使用Clockwork Rails调度程序Gem?
我遇到了Clockwork调度程序进程的语法问题。 我实际上遇到的问题与此线程中讨论的内容类似,但从未完全回答( 如何使用Rails发条工具来运行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关于实施发条的文档可能会有所帮助
Ruby中的计划作业和自定义时钟进程与发条