delayed job

delayed_job_active_record

https://github.com/collectiveidea/delayed_job_active_record

Installation

(1)Add the gem to your Gemfile:

gem 'delayed_job_active_record'

Run bundle install.

(2)If you're using Rails, run the generator to create the migration for the delayed_job table.

rails g delayed_job:active_record
bundle exec rake db:migrate

要是觉得不喜欢这个表名,可以在migrate修改表名。

(3)Running Jobs

script/delayed_job can be used to manage a background process which will start working off jobs.

To do so, add gem "daemons" to your Gemfile and make sure you've run rails generate delayed_job.

You can then do the following:

RAILS_ENV=production script/delayed_job start
RAILS_ENV=production script/delayed_job stop

# Runs two workers in separate processes.
RAILS_ENV=production script/delayed_job -n 2 start
RAILS_ENV=production script/delayed_job stop

# Set the --queue or --queues option to work from a particular queue.
RAILS_ENV=production script/delayed_job --queue=tracking start
RAILS_ENV=production script/delayed_job --queues=mailers,tasks start

# Use the --pool option to specify a worker pool. You can use this option multiple times to start different numbers of workers for different queues.
# The following command will start 1 worker for the tracking queue,
# 2 workers for the mailers and tasks queues, and 2 workers for any jobs:
RAILS_ENV=production script/delayed_job --pool=tracking --pool=mailers,tasks:2 --pool=*:2 start

# Runs all available jobs and then exits
RAILS_ENV=production script/delayed_job start --exit-on-complete
# or to run in the foreground
RAILS_ENV=production script/delayed_job run --exit-on-complete
(4)insert and delete
 def set_push_at
 @video.update_attributes(:will_push_at => params[:cibn_video_broadcast_list][:will_push_at],
 :push_user => current_user_name)
 @video.delay(:run_at => @video.will_push_at).
 send( method_to_perform(@video), {:user_name => current_user_name})
 redirect_to :back, :notice => '操作成功'
 end 

 def cancel_push_at
 @video.update_attributes(:will_push_at => nil)
 job = CibnPushDelayedJob.where('handler like ?' ,
 ["", "CibnVideoBroadcastList", "id: #{@video.id}", ""].join('%') ).last
 CibnPushDelayedJob.delete job.try(:id)
 redirect_to :back, :notice => '操作成功'

 

posted @ 2015-03-06 13:40  冰凌花花~  阅读(383)  评论(0编辑  收藏  举报