繁体   English   中英

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

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

我在Clockwork调度程序过程的语法上遇到问题。 我实际上有与该线程中讨论的问题类似的问题,但从未得到完全回答( 如何使用Rails发条gem运行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的有关实现发条的文档可能会有所帮助

带有Clockwork的Ruby中的计划作业和自定义时钟进程

暂无
暂无

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

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