LoginSignup
6
17

More than 5 years have passed since last update.

sidekiqに触る機会があったので個人用にまとめてみた

Last updated at Posted at 2016-02-02

sidekiqについて

  • resqueのような非同期実行を実現するgem

  • バックエンドにredisが必要

  • resqueとは違いsocket型

configure

sidekiq.yml
:verbose: true
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:concurrency: 3 #並列数
:retry: 1
:queues:
  - [hogehoge,1] #queueに重みをつけられる [string 名前,int 重み]

jobの定義

job.rb
class Hogehoge
  include Sidekiq::Worker #sidekiqのinclude
  sidekiq_options queue: :hogehoge #queueの名前指定

  def perform(date=nil)
    puts "test"
  end

起動

  • console起動
bundle exec sidekiq -C config/sidekiq.yml

deamon起動

  • 上記の config/sidekiq.yml:daemon: true を指定するとdaemon起動する

  • capistrano-sidekiqというgemを使用するとcapistranoでdeploy時にdeamon化することが可能

  • resqueの場合はdaemon-spawnなどといったdeamon化するgemを使用して自前でdeamon化用のscriptを用意する必要がある

以下capistrano例

  • Capistrano + sidekiq
gem 'capistrano-sidekiq', github: 'seuros/capistrano-sidekiq'
require 'capistrano/sidekiq'
production.rb
set :pty, false

設定はこれだけのはず

本番実行

bundle exec sidekiq --environment=production

productin環境を指定する場合引数でenvを設定する

更新処理

sidekiqは一度起動したタイミングのソースで動作するのでjob taskを変更しても
更新されない
capistranoでdaemon化ができていない場合は手動でsidekiqのreloadをかける必要がある

ps aux | grep sidekiq
kill -9 hogehoge

またはconfigファイルで設定したpidファイルをcatあたりで確認しそのpidをkillする

cat tmp/pids/sidekiq.pid

その後再起動

bundle exec sidekiq --environment=production

ダッシュボード

sidekiqには標準でsinatraベースのダッシュボードが付いている
railsの場合config/route.rbにルーティングを追加し起動するだけ

  • unicorn や rails server などを起動
route.rb
require 'sidekiq/web'
require 'sidekiq-scheduler/web'

Rails.application.routes.draw do
  mount Sidekiq::Web => '/sidekiq'
end 

http://loalhost:4567/sidekiq などでaccessできる

下記の様なgemを追加するとダッシュボードがカスタマイズされる

gem 'sidekiq-failures'
gem 'sidekiq-history'
gem 'sidekiq-statistic'
6
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
17