如何使用Rails发条宝石来运行rake任务?

use*_*556 11 rake ruby-on-rails heroku

从发条中调用rake任务的语法是什么?我尝试了各种语法,似乎没什么用.(我对钟表工作特别感兴趣,因为Heroku支持它.)

这是我的clock.rb,使用的是when gem使用的相同语法:

module Clockwork
  puts "testing clockwork!"
  every(30.seconds, 'Send Messages') {
    rake 'scheduler:send_messages'
    }
end
Run Code Online (Sandbox Code Playgroud)

这是我在scheduler.rake中的rake任务:

task :send_messages => :environment do
  puts "rake task run successfully!"
end
Run Code Online (Sandbox Code Playgroud)

这是我开始发条过程时会发生的事情:

$ clockwork lib/clock.rb
testing clockwork!
I, [2012-07-16T14:42:58.107245 #46427]  INFO -- : Starting clock for 1 events: [ Send Messages ]
I, [2012-07-16T14:42:58.107364 #46427]  INFO -- : Triggering 'Send Messages'
attempting to run rake task!
E, [2012-07-16T14:42:58.107437 #46427] ERROR -- : undefined method `rake' for Clockwork:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

每30秒运行一次.如您所见,clock.rb已成功执行.但我不能为我的生活弄清楚运行rake任务的语法.不幸的是,发条自述没有帮助:

https://github.com/tomykaira/clockwork

hgm*_*mnz 9

rake 不是方法,所以你不能在这里调用它.

你可以弹出并调用它,比如

every(30.seconds, 'Send Messages') {
  `rake scheduler:send_messages`
}
Run Code Online (Sandbox Code Playgroud)

或者更确切地说,使用heroku API调用一个新的分离进程.这是我现在的首选方法:

Heroku::API.new.post_ps('your-app', 'rake scheduler:send_messages')
Run Code Online (Sandbox Code Playgroud)

Heroku :: API可从heroku.rb获得:https://github.com/heroku/heroku.rb