简体   繁体   English

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

[英]How do I use the Clockwork Rails scheduler Gem?

I'm having problems with the syntax for the Clockwork scheduler process. 我在Clockwork调度程序过程的语法上遇到问题。 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? ) 我实际上有与该线程中讨论的问题类似的问题,但从未得到完全回答( 如何使用Rails发条gem运行rake任务?

My 'scheduler.rake' is working correctly when I test it with a 'heroku rake send_notifications'. 当我使用“ heroku rake send_notifications”进行测试时,我的“ scheduler.rake”工作正常。 My clock.rb process is working too as it will trigger every 30 seconds. 我的clock.rb进程也正在运行,因为它将每30秒触发一次。 However, I'm having issues with the clock.rb's syntax to correctly run the 'send_notifications' task in my 'rake.scheduler'. 但是,我的clock.rb语法存在问题,无法在我的“ rake.scheduler”中正确运行“ send_notifications”任务。 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: 这是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
}

As you can see I've tried with a detached process using the Heroku API and invoking rake. 如您所见,我已经尝试过使用Heroku API和调用rake进行分离的过程。

When I use the Heroku API, I get this error: 当我使用Heroku API时,出现以下错误:

ERROR -- : uninitialized constant Heroku (NameError)

When I invoke rake, I get the following error: 调用rake时,出现以下错误:

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. 这意味着要确保使用` `来包装rake任务,而不要使用" "因为这会使一些人绊倒。

The docs from Hereko on implementing clockwork might help 来自Hereko的有关实现发条的文档可能会有所帮助

Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork 带有Clockwork的Ruby中的计划作业和自定义时钟进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM