我想从delayed_job运行一个rake任务(apn:notifications:从apn_on_rails gem传递).换句话说,我想将一个延迟的工作排入队列,这个工作将调用apn:notifications:提供rake任务.
我从http://geminstallthat.wordpress.com/2008/02/25/run-rake-tasks-with-delayedjob-dj/发现了这个代码http://pastie.org/157390.
我将此代码作为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
一切运行良好,直到delayed_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.
解决方法
为什么不做系统调用?
system "rake apn:notifications:deliver"