LoginSignup
0
0

More than 1 year has passed since last update.

Sidekiq

Last updated at Posted at 2021-12-31

image.png

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 を読み込む
  • Launchernew して 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

.https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/cli.rb#L105-L116

Launcher#run

  • Manager#start

.https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/launcher.rb#L35

Manager#start

ManagerProcessor の管理(作成、死んだら新しいのを作る、シャットダウン)を行う。

  • 初期化時に、指定された並列数分だけの Processor を作成
  • #startProcessorstart

.https://github.com/mperham/sidekiq/blob/4d0084569c609fa63303d8bfa60f5279930ee353/lib/sidekiq/manager.rb#L32-L47

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

0
0
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
0
0