Create a gist now

Instantly share code, notes, and snippets.

Running rake tasks in background
  1. Add gem 'delayed_job', '2.1.4' to your Gemfile

  2. Run bundle install

  3. Run rails g migration create_delayed_jobs

  4. Edit the created migration to contain:

     class CreateDelayedJobs < ActiveRecord::Migration
       def self.up
         create_table :delayed_jobs, :force => true do |table|
           table.integer  :priority, :default => 0      # jobs can jump to the front of
           table.integer  :attempts, :default => 0      # retries, but still fail eventually
           table.text     :handler                      # YAML object dump
           table.text     :last_error                   # last failure
           table.datetime :run_at                       # schedule for later
           table.datetime :locked_at                    # set when client working this job
           table.datetime :failed_at                    # set when all retries have failed
           table.text     :locked_by                    # who is working on this object
           table.timestamps
         end
       end
       def self.down
         drop_table :delayed_jobs
       end
     end
    
  5. Create delayed_tasks.rake in lib/tasks, see example bellow for spree_solr_search tasks

class DelayedRakeTask
def initialize(task_name)
@task_name = task_name
end
def perform
Rake::Task[@task_name].invoke
end
end
namespace :solr do
namespace :async do
task :reindex => :environment do
Delayed::Job.enqueue DelayedRakeTask.new('solr:reindex')
end
task :optimize => :environment do
Delayed::Job.enqueue DelayedRakeTask.new('solr:optimize')
end
end
end
@umamahe
umamahe commented Dec 14, 2012

how to start delayed job in development ?

@kienbt01359

@umamahe

bundle exec rake jobs:work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment