如何从delayed_job运行rake任务
我想从delayed_job 运行rake 任务(apn:notifications:deliver from the apn_on_rails gem)。换句话说,我想排队一个延迟的作业,它将调用 apn:notifications:deliver rake 任务。
我从 http://pastie.org/157390 找到了这段代码。 wordpress.com/2008/02/25/run-rake-tasks-with-delayedjob-dj/" rel="noreferrer">http://geminstallthat.wordpress.com/2008/02/25/run-rake-tasks -with-delayedjob-dj/。
我将此代码作为 DelayedRake.rb 添加到我的 lib 目录中:
require 'rake'
require 'fileutils'
class DelayedRake
def initialize(task, options = {})
@task = task
@options = options
end
##
# Called by Delayed::Job.
def perform
FileUtils.cd RAILS_ROOT
@rake = Rake::Application.new
Rake.application = @rake
### Load all the Rake Tasks.
Dir[ "./lib/tasks/**/*.rake" ].each { |ext| load ext }
@options.stringify_keys!.each do |key, value|
ENV[key] = value
end
begin
@rake[@task].invoke
rescue => e
RAILS_DEFAULT_LOGGER.error "[ERROR]: task \"#{@task}\" failed. #{e}"
end
end
end
一切都运行良好,直到elaided_job运行并抱怨:
[错误]:任务“apn:notifications:deliver”失败。不知道如何构建任务“apn:notifications:deliver”
我如何让它知道apn_on_rails?我尝试在 DelayedRake 顶部要求'apn_on_rails_tasks',但没有做任何事情。我还尝试将 rake 任务的目录更改为 ./lib/tasks/*.rake
我对 Ruby/Rails 有点陌生。这是在 Heroku 2.3.5 上运行的。
I'd like to run a rake task (apn:notifications:deliver from the apn_on_rails gem) from a delayed_job. In other words, I'd like enqueue a delayed job which will call the apn:notifications:deliver rake task.
I found this code http://pastie.org/157390 from http://geminstallthat.wordpress.com/2008/02/25/run-rake-tasks-with-delayedjob-dj/.
I added this code as DelayedRake.rb to my lib directory:
require 'rake'
require 'fileutils'
class DelayedRake
def initialize(task, options = {})
@task = task
@options = options
end
##
# Called by Delayed::Job.
def perform
FileUtils.cd RAILS_ROOT
@rake = Rake::Application.new
Rake.application = @rake
### Load all the Rake Tasks.
Dir[ "./lib/tasks/**/*.rake" ].each { |ext| load ext }
@options.stringify_keys!.each do |key, value|
ENV[key] = value
end
begin
@rake[@task].invoke
rescue => e
RAILS_DEFAULT_LOGGER.error "[ERROR]: task \"#{@task}\" failed. #{e}"
end
end
end
Everything runs fine until the delayed_job runs and it complains:
[ERROR]: task "apn:notifications:deliver" failed. Don't know how to build task 'apn:notifications:deliver'
How do I let it know about apn_on_rails? I'd tried require 'apn_on_rails_tasks' at the top of DelayedRake which didn't do anything. I also tried changing the directory of rake tasks to ./lib/tasks/*.rake
I'm somewhat new to Ruby/Rails. This is running on 2.3.5 on heroku.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不只进行系统调用呢?
Why don't do just a system call ?
我相信如果你将其称为一个单独的过程会更容易。请参阅从 Ruby 运行命令的 5 种方法。
如果您想捕获任何错误,您应该捕获
STDERR
,如文章中所示。I believe it's easier if you call it as a separate process. See 5 ways to run commands from Ruby.
If you want to capture any errors, you should capture
STDERR
as shown in the article.