简体   繁体   中英

How do I use the Clockwork Rails scheduler Gem?

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?

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.

The docs from Hereko on implementing clockwork might help

Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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