作業ログ インスタンス起動時にsidekiqの複数プロセスが立ち上がるようにする 2019/09/01

※この記事に登場するソースコードなどは僕個人が開発しているプロジェクトのものです。仕事のやつじゃないよ

Ubuntu 16.04 なのでsystemdを使います

素晴らしいexampleが公式で用意されてるのでこれを編集して使います

github.com

#
# systemd unit file for CentOS 7, Ubuntu 15.04
#
# Customize this file based on your bundler location, app directory, etc.
# Put this in /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu).
# Run:
#   - systemctl enable sidekiq
#   - systemctl {start,stop,restart} sidekiq
#
# This file corresponds to a single Sidekiq process.  Add multiple copies
# to run multiple processes (sidekiq-1, sidekiq-2, etc).
#
# See Inspeqtor's Systemd wiki page for more detail about Systemd:
# https://github.com/mperham/inspeqtor/wiki/Systemd
#
[Unit]
Description=sidekiq
# start us only once the network and logging subsystems are available,
# consider adding redis-server.service if Redis is local and systemd-managed.
After=syslog.target network.target

# See these pages for lots of options:
# http://0pointer.de/public/systemd-man/systemd.service.html
# http://0pointer.de/public/systemd-man/systemd.exec.html
[Service]
Type=simple
WorkingDirectory=/opt/myapp/current
# If you use rbenv:
# ExecStart=/bin/bash -lc '/home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
# If you use the system's ruby:
ExecStart=/usr/local/bin/bundle exec sidekiq -e production
User=deploy
Group=deploy
UMask=0002

# Greatly reduce Ruby memory fragmentation and heap usage
# https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
Environment=MALLOC_ARENA_MAX=2

# if we crash, restart
RestartSec=1
Restart=on-failure

# output goes to /var/log/syslog
StandardOutput=syslog
StandardError=syslog

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq

[Install]
WantedBy=multi-user.target

まずはシングルプロセスで動作を確認する

# vim /lib/systemd/system/sidekiq.service

上のサンプルを張り付けてUser, Group, WorkingDirectoryなどを適宜修正する。

# systemctl start sidekiq
# systemctl status sidekiq
● sidekiq.service - sidekiq
   Loaded: loaded (/lib/systemd/system/sidekiq.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-09-01 00:52:09 UTC; 6min ago
 Main PID: 2322 (ruby2.5)
    Tasks: 10 (limit: 4218)
   CGroup: /system.slice/sidekiq.service
           └─2322 sidekiq 5.2.7 oak [0 of 5 busy]

問題なさそう。

念のためSidekiqのコンソールで確認してみたけどそちらでも確認できた。

マルチプロセス

僕の環境だと現在4コアのマシンでジョブを動かしているので、同じものを4つ作ります

# cp /lib/systemd/system/sidekiq.service /lib/systemd/system/sidekiq-1.service 
# cp /lib/systemd/system/sidekiq.service /lib/systemd/system/sidekiq-2.service 
# cp /lib/systemd/system/sidekiq.service /lib/systemd/system/sidekiq-3.service 
# cp /lib/systemd/system/sidekiq.service /lib/systemd/system/sidekiq-4.service 
# rm /lib/systemd/system/sidekiq.service
# systemctl start sidekiq-1
# systemctl start sidekiq-2
# systemctl start sidekiq-3
# systemctl start sidekiq-4

f:id:Kenta-s:20190901100735p:plain

OK

あとはそれぞれsystemctl enableしておく

# systemctl enable sidekiq-1
Created symlink /etc/systemd/system/multi-user.target.wants/sidekiq-1.service → /lib/systemd/system/sidekiq-1.service.
# systemctl enable sidekiq-2
Created symlink /etc/systemd/system/multi-user.target.wants/sidekiq-2.service → /lib/systemd/system/sidekiq-2.service.
# systemctl enable sidekiq-3
Created symlink /etc/systemd/system/multi-user.target.wants/sidekiq-3.service → /lib/systemd/system/sidekiq-3.service.
# systemctl enable sidekiq-4
Created symlink /etc/systemd/system/multi-user.target.wants/sidekiq-4.service → /lib/systemd/system/sidekiq-4.service.

これでインスタンスを立ち上げるだけでSidekiqのプロセスが起きてくれます。やたーー