【问题标题】:How do I use the Clockwork Rails scheduler Gem?如何使用 Clockwork Rails 调度程序 Gem?
【发布时间】:2012-11-08 02:05:40
【问题描述】:

我遇到了 Clockwork 调度程序进程的语法问题。我实际上遇到了与此线程中讨论的类似问题,但从未完全回答(How do I use Rails clockwork gem to run rake tasks?

当我使用“heroku rake send_notifications”对其进行测试时,我的“scheduler.rake”工作正常。我的 clock.rb 进程也在工作,因为它将每 30 秒触发一次。但是,在我的“rake.scheduler”中正确运行“send_notifications”任务时,clock.rb 的语法存在问题。这是它的样子:

# 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)

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

【问题讨论】:

  • 将字符串引号添加到 rake 调度程序:send_notifications
  • 任务 send_notifications 在调度程序命名空间内?

标签: ruby-on-rails heroku rake scheduled-tasks


【解决方案1】:

This answer works, 但是你需要完全遵循它的字符串语法。所以在你的情况下:

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

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

【讨论】:

    【解决方案2】:

    Hereko 关于实现发条的文档可能会有所帮助

    Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-16
    • 2016-10-02
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    相关资源
    最近更新 更多