How do I use the Clockwork Rails scheduler Gem?

别说谁变了你拦得住时间么 提交于 2019-12-07 08:45:42

问题


I'm having problems with the syntax for the Clockwork scheduler process. 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?)

My 'scheduler.rake' is working correctly when I test it with a 'heroku rake send_notifications'. My clock.rb process is working too as it will trigger every 30 seconds. However, I'm having issues with the clock.rb's syntax to correctly run the 'send_notifications' task in my 'rake.scheduler'. 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:

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.

When I use the Heroku API, I get this error:

ERROR -- : uninitialized constant Heroku (NameError)

When I invoke rake, I get the following error:

ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)

Does anyone know what the correct syntax is for either approach?


回答1:


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.




回答2:


The docs from Hereko on implementing clockwork might help

Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork



来源:https://stackoverflow.com/questions/13465869/how-do-i-use-the-clockwork-rails-scheduler-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!