我不明白为什么我的 rake 任务没有在 resque worker 中运行。运行
rake :send_this_email
从控制台运行正常,我只是想将它作为 cron 作业运行(如下所示),但是在从工作人员中调用 rake 任务时有些东西无法正常工作。
我的rescue_schedule.yml
send_this_email:
cron: "*/2 * * * *"
class: SendThisEmailWorker
args:
description: "Send email when condition defined in rake task is met"
我的 send_this_email_worker.rb
在 workers
目录中,如果我可以自己从控制台手动调用 rake 任务,那问题一定出在哪里?
require 'rake'
module SendThisEmailWorker
@queue = :send_this_email
def self.perform
Rake::Task["send_this_email"].invoke
end
end
当我启动我的开发服务器时,这个send_this_email
rake 任务应该每 2 分钟运行一次,对吗?不是,resque 管理面板将其显示为队列中的作业。我在这里缺少什么?
感谢您的关注。
更新自 gerep 评论
require 'rake'
module SendThisEmailWorker
@queue = :send_this_email
def self.perform
puts "Hi from the console, I'm started"
Rake::Task["send_this_email"].invoke
end
end
最佳答案
只有 require 'rake'
是不够的。例如,如果你这样做
Rake::Task.tasks #列出所有任务
你会得到[]
您需要告诉您的工作类加载任务。 试试这个
require 'rake'
Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
YOUR_APP_NAME::Application.load_tasks
module SendThisEmailWorker
@queue = :send_this_email
def self.perform
puts "Hi from the console, I'm started"
Rake::Task["send_this_email"].invoke
end
end
YOUR_APP_NAME
是您应用的名称,可以在 config/application.rb
关于ruby-on-rails - Rake 任务不是从 worker 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556710/