Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I run a rake task from a delayed_job
    text
    copied!<p>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.</p> <p>I found this code <a href="http://pastie.org/157390" rel="noreferrer">http://pastie.org/157390</a> from <a href="http://geminstallthat.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/</a>.</p> <p>I added this code as DelayedRake.rb to my lib directory:</p> <pre><code>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 =&gt; e RAILS_DEFAULT_LOGGER.error "[ERROR]: task \"#{@task}\" failed. #{e}" end end end </code></pre> <p>Everything runs fine until the delayed_job runs and it complains: </p> <p>[ERROR]: task "apn:notifications:deliver" failed. Don't know how to build task 'apn:notifications:deliver'</p> <p>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</p> <p>I'm somewhat new to Ruby/Rails. This is running on 2.3.5 on heroku.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload