简体   繁体   English

如何使用 Rails clockwork gem 运行 rake 任务?

[英]How do I use Rails clockwork gem to run rake tasks?

What is the syntax for calling rake tasks from clockwork?从 clockwork 调用 rake 任务的语法是什么? I've tried all kinds of syntax, and nothing seems to work.我尝试了各种语法,但似乎没有任何效果。 (I'm specifically interested in clockwork because Heroku's supporting it.) (我对发条特别感兴趣,因为 Heroku 支持它。)

Here's my clock.rb, using the same syntax that the whenever gem uses:这是我的 clock.rb,使用与 whenever gem 使用相同的语法:

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

And here's my rake task in scheduler.rake:这是我在 scheduler.rake 中的 rake 任务:

task :send_messages => :environment do
  puts "rake task run successfully!"
end

And here's what happens when I start a clockwork process:下面是当我开始发条过程时会发生什么:

$ 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)

This runs every 30 seconds.每 30 秒运行一次。 As you can see, the clock.rb is executed successfully.可以看到,clock.rb 执行成功了。 But I can't for the life of me figure out the syntax to run a rake task.但我终其一生都无法弄清楚运行 rake 任务的语法。 The clockwork readme is no help, unfortunately:不幸的是,发条自述文件没有帮助:

https://github.com/tomykaira/clockwork https://github.com/tomykaira/clockwork

rake is not a method, so you can't invoke it like that here. rake不是一种方法,因此您不能在此处那样调用它。

You can either shell out and invoke it, something like您可以取出并调用它,例如

every(30.seconds, 'Send Messages') {
  `rake scheduler:send_messages`
}

or rather invoke a new detached process using the heroku API.或者更确切地说,使用 heroku API 调用一个新的分离进程。 This is my preferred method right now:这是我现在首选的方法:

Heroku::API.new.post_ps('your-app', 'rake scheduler:send_messages')

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

You can pass in a block to every that executes your rake task:您可以将块传递给执行 rake 任务的every块:

every(1.day, "namespace:task") do
  ApplicationName::Application.load_tasks
  Rake::Task['namespace:task'].invoke
end

You can add the following method to your clock.rb file:您可以将以下方法添加到您的clock.rb文件中:

def execute_rake(file,task)
require 'rake'
rake = Rake::Application.new
Rake.application = rake
Rake::Task.define_task(:environment)
load "#{Rails.root}/lib/tasks/#{file}"
rake[task].invoke
end

and then call然后打电话

execute_rake("your_rake_file.rake","your:rake:task")

in your handler在你的处理程序中

Invoking the task using Rake::Task['...'].invoke works well the first time, but the task need to be reenabled to be invoked again later.使用Rake::Task['...'].invoke任务第一次运行良好,但需要重新启用该任务以便稍后再次调用。

    ApplicationName::Application.load_tasks

    module Clockwork do
        every(10.minutes, "namespace:task") do
          Rake::Task['namespace:task'].invoke
          Rake::Task['namespace:task'].reenable
        end
    end

Otherwise the task will be invoked the first time, but no more after that until the clockwork process is restarted.否则,任务将在第一次被调用,但之后不会再被调用,直到发条进程重新启动。

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

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