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

问题描述 投票:4回答:2

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

当我使用'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)

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

ruby-on-rails heroku rake scheduled-tasks
2个回答
6
投票

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

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

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


0
投票

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

Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork

© www.soinside.com 2019 - 2024. All rights reserved.