Sidekiq は server プロセス起動時に Rails app を boot して、スレッドを作成する。
各スレッドが Redis から job を dequeue して、job を実行する。
bundle exec sidekiq
bin/sidekiq の中で
CLI.run
.https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/bin/sidekiq#L26-L31
CLI.run
- Rails アプリケーションを boot
- (オプションで指定しなければ現在のディレクトリの) config/environment.rb を読み込む
-
Launcherをnewしてrun
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/cli.rb#L37
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/cli.rb#L273
Launcher#run
Manager#start
Manager#start
Manager は Processor の管理(作成、死んだら新しいのを作る、シャットダウン)を行う。
- 初期化時に、指定された並列数分だけの
Processorを作成 -
#startでProcessorをstart
Processor#start
スレッド上で Redis からジョブを逐次取ってきて実行する。
-
Thread.newしてrun -
run- Redis からジョブを fetch
- ジョブを実行 (job class をインスタンス化して、perform を呼ぶ)
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/processor.rb#L67-L80
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/processor.rb#L162-L166
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/processor.rb#L132-L133
https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/processor.rb#L195-L197
