public
Last active

Running rake tasks in background

  • Download Gist
delayed_tasks.markdown
Markdown
  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

delayed_tasks.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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

how to start delayed job in development ?

@umamahe

bundle exec rake jobs:work

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.