如何从delayed_job运行rake任务

发布于 2024-10-02 16:12:21 字数 1385 浏览 2 评论 0原文

我想从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-10-09 16:12:21

为什么不只进行系统调用呢?

system "rake apn:notifications:deliver"

Why don't do just a system call ?

system "rake apn:notifications:deliver"
春庭雪 2024-10-09 16:12:21

我相信如果你将其称为一个单独的过程会更容易。请参阅从 Ruby 运行命令的 5 种方法

def perform
  `rake -f #{Rails.root.join("Rakefile")} #{@task}`
end

如果您想捕获任何错误,您应该捕获 STDERR,如文章中所示。

I believe it's easier if you call it as a separate process. See 5 ways to run commands from Ruby.

def perform
  `rake -f #{Rails.root.join("Rakefile")} #{@task}`
end

If you want to capture any errors, you should capture STDERR as shown in the article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文