本篇介绍sidekiq基于redis
队列的后端的
异步任务处理,当然还有定时任务,延时处理等功能。
https://github.com/mperham/sidekiq/wiki
http://mperham.github.com/sidekiq/
class="subheader" style="">sidekiq的宣传语:
1.定义异步处理worker类
?
app/worker/hard_work.rb
class HardWorker
include Sidekiq::Worker
sidekiq_options :queue => 'phoenix_job' , :timeout => 60, :retry => true, :backtrace => true
def perform(options,count=5)
puts 'do'
end
end
?
2.加载workers目录
?
application.rb
config.autoload_paths += %W(#{Rails.root}/app/workers)
?
3.加入异步任务处理队列
?
app/
controllers/**_controller.rb
HardWorker.perform_async(params)
?
4.sidekiq.yml配置文件
?
config/sidekiq.yml
:verbose: false
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:queues:
- [phoenix_job,7]
- [default, 2]
staging:
:concurrency: 5
development:
:concurrency: 5
production:
:concurrency: 50
?
5.配置sidekiq链接redis
config/initialiizers/sidekiq.rb
?
ENV["REDISTOGO_URL"] = "redis://username:.password@host:port}"
?
6.unicorn运行after_fork配置
unicorn.rb
after_fork do |server, worker|
ENV["REDISTOGO_URL"] = "redis://username:.password@host:port}"
?
end
7.运行,停止命令
?
stop.sh
bundle exec sidekiqctl quiet tmp/pids/sidekiq.pid
bundle exec sidekiqctl stop tmp/pids/sidekiq.pid
?
start.sh
bundle exec sidekiq -C config/sidekiq.yml -d -e production
?
感觉比resque好用多了,在断网自动重连,日志文件,pid文件,部署,性能等方面都要好。