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

dou*_*ets 4 rake ruby-on-rails heroku scheduled-tasks

我遇到了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
Run Code Online (Sandbox Code Playgroud)

这是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
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我尝试使用Heroku API并调用rake进行分离过程.

当我使用Heroku API时,我收到此错误:

ERROR -- : uninitialized constant Heroku (NameError)
Run Code Online (Sandbox Code Playgroud)

当我调用rake时,我收到以下错误:

ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)
Run Code Online (Sandbox Code Playgroud)

有谁知道这两种方法的正确语法是什么?

Kel*_*nan 6

这个答案有效,但是你需要完全遵循它的字符串语法. 所以在你的情况下:

every(30.seconds, 'Send Notifications') {
  `rake scheduler:send_notifications`
}
Run Code Online (Sandbox Code Playgroud)

这意味着确保` `用于包装rake任务,而不是" "因为绊倒了一些人.