LoginSignup
6
1

More than 1 year has passed since last update.

Sidekiqを本番環境のEC2バックグラウンドで起動

Last updated at Posted at 2022-03-06

開発環境と共通する実装

Sidekiqは非同期処理を実行するライブラリです。

同時にジョブを実行するために、他のジョブを待たずにメイラーを実行するなどのメリットがあります。
以下の記事が参考になります

Railsで非同期処理を行える「Sidekiq」

本番環境においては、キャッシュサーバーを別に指定し、以下のファイル群を配備しているはずです。

config/initializers/sidekiq.rb
config/sidekiq.yml

そして筆者はEC2にアプリケーションをデプロイして、ElastiCacheサーバーのRedisを配備しているので、sidekiq.rbにて指定しています。

sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = {
    url: ENV["SIDEKIQ_URL"] #本番環境はElastiCacheのEndpoint
  }
end
Sidekiq.configure_client do |config|
  config.redis = {
    url: ENV["SIDEKIQ_URL"] #本番環境はElastiCacheのEndpoint
  }
end
sidekiq.yml
:concurrentcy: 25
:queues:
  - default
  - mailers

Sidekiq実行の本番環境ファイルを作成、コマンドを叩く

EC2でSidekiqを実行するにbundle exec rails s -e productionでPumaサーバーをバックグラウンドで実行しながら、ターミナルでbundle exec sidekiq -e productionとするとSidekiqが起動し、
連動するメイラーを動作させると非同期処理でメールが送信されます。

しかし、これではEC2上のターミナルを閉じた時点で、Sidekiqが止まってジョブが溜まって実行されません。

なので
bundle exec sidekiq --daemon -e productionとして
デーモンで実行させようとしますが、

ERROR: Daemonization mode was removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services

となり、デーモンがSidekiq 6.0では使えなくなったという警告が現れました。

なので、EC2インスタンスの場合であればサービスOSのSystemdにて、ファイルを別途作成してSidekiqを実行するイメージになります。

EC2サービスのファイルを作成

EC2であれば /usr/lib/systemd/system/sidekiq.serviceとしてファイルを別途作成してSidekiqを実行できます。

ファイルでは、以下に該当するアプリケーション名とEC2のIAMユーザーの指定を行います。

sidekiq.service
#EC2の/usr/lib/systemd/system/sidekiq.serviceに配置

[Unit]
Description=sidekiq
After=syslog.target network.target

[Service]
# v6.0.6以上の場合にはType=notify
# v6.0.5以下の場合にはType=simple
Type=simple

# プロセスがロックアップされた場合に指定されて秒数以内に再起動する
WatchdogSec=10

# アプリケーションディレクトリの指定
WorkingDirectory=/var/www/アプリ名

# rbenvを利用している場合は以下の記述
ExecStart=/bin/bash -lc 'exec /home/ユーザー名/.rbenv/shims/bundle exec sidekiq -e production'

User=sirius
#Group=ユーザーグループ名
UMask=0002

Environment=MALLOC_ARENA_MAX=2

# クラッシュした場合にリスタートする
RestartSec=1
Restart=on-failure

# ログの吐き出し場所
# Ubuntuの場合/var/log/syslog 、CentOSの場合/var/log/messages
StandardOutput=syslog
StandardError=syslog

SyslogIdentifier=sidekiq

[Install]
WantedBy=multi-user.target

ファイルの作成が完了したらEC2のターミナルで

systemctl enable sidekiqでSidekiqを有効にして
sudo systemctl start sidekiq

で動かし該当するジョブが非同期で実行される準備が整います。

ステータスのチェックはsystemctl status sidekiq.serviceで確認できました。

3月 06 00:19:23 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:19:23.768Z pid=14157 tid=xrzut...ne
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.936Z pid=14157 tid=xmfyt...rt
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.945Z pid=14157 tid=xmfyt c...
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.946Z pid=14157 tid=xmfyt...er
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.947Z pid=14157 tid=xmfyt...s)
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.948Z pid=14157 tid=xmfyt...er
 3月 06 00:20:28 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:28.948Z pid=14157 tid=xmfyt...s)
 3月 06 00:20:29 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:29.335Z pid=14157 tid=xmfyt...s)
 3月 06 00:20:29 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:29.336Z pid=14157 tid=xmfyt...ms
 3月 06 00:20:29 ip-10-0-11-191.ap-northeast-1.compute.internal sidekiq[14157]: 2022-03-06T00:20:29.336Z pid=14157 tid=xmfyt...ne

以上でございます。
何か不備がございましたら、ご指摘いただけたら幸いです。

参考記事

SidekiqをSystemdで管理しCapistranoでデプロイする
Sidekiq 6の新機能・変更点

6
1
1

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
1