LoginSignup
0
0

More than 1 year has passed since last update.

EC2上のworkプロセスをSupervisorで永続化!

Posted at

なぜsupervisorを使うのか

workerプロセスはartisanコマンドで実行できますが、いろいろな原因でworkerプロセスが停止になる可能性があります。そうなると新しくQueueに入るJOBが処理されなくなります。

queue:workプロセスを永続的に実行し続けるには、キューワーカが止まらずに実行し続けていることを確実にするため、Supervisorのようなプロセスモニタを利用する必要があります。

今回は、EC2にSupervisorをインストールして実行させる方法を備忘録を兼ねて残します。

Supervisor: http://supervisord.org/

設定方法

インストール

EC2コンテナに入り、以下を入力してSupervisorをインストールします。(ubuntu)
sudo apt-get install supervisor

下記コマンドを入力し、バージョンが表示されたらインストールができています。
supervisord -v

設定ファイル作成&編集

Supervisorの設定ファイルがあるか確認します。
/etc/supervisor/配下にsupervisord.confがなければ以下コマンドを入力して設定ファイルを生成させます。
sudo echo_supervisord_conf > /etc/supervisord.conf

設定ファイルが作成されたらvim /etc/supervisor/supervisord.confでファイルを編集します。
大抵はデフォルトで大丈夫ですが各自で設定を行なってください。また、最後の方にある、[include]をコメントアウトし、実行させたいプロセスを記述したファイルのパスを指定します。

supervisord.conf
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
Ärpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include] #コメントアウトする
files = /etc/supervisor/conf.d/*.conf

次に、先ほど[include]に指定したパスにプロセス設定ファイルを作成します。
touch /etc/supervisor/conf.d/laravel-worker.conf

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/hogehoge/artisan queue:work --tries=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/hogehoge/storage/logs/worker.log
startsecs=0

各種説明

[program: ~] ・・・ プログラム名

proccess_name ・・・プロセス名

command ・・・ 実行したいコマンド

autostart ・・・ Supervisorを再起動する時に対象プロセスも自動的に再起動される

autoRestart・・・  trueの場合はプロセスが停止されると自動的に再起動される

user ・・・ 実行ユーザー

numprocs ・・・ 非同期で実行したいプロセス数

redirect_stderr ・・・ trueの場合はプロセスのstderrがstdoutへリダイレクトされる

stdout_logfile ・・・ stdoutを出力したいファイルのフルパス

startsecs ・・・ プロセス開始から実行までの秒数

これで一通り設定が完了です!

supervisor起動

設定ファイルができたら、Supervisorの設定を更新し起動するために以下のコマンドを実行してください。

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start {プログラム名}

プロセスが起動されているか、以下コマンドで確認します。
sudo supervisorctl status

このようにRUNNINGになっていればプロセスが実行されています!
hoge:hogehoge RUNNING pid 651479, uptime 6:44:24

試しにkillコマンドでプロセスを停止させても再起動されるか確認してみましょう!

supervisorコマンド一覧

コマンドは、supervisorctl helpで確認できます。

主なコマンド
version ・・・ バージョン確認

start {プログラム名} ・・・ プロセスの実行。プログラム名の箇所をallにすると全プログラムが実行される。

stop {プログラム名} ・・・ プロセスの停止。プログラム名の箇所をallにすると全プログラムが停止される。

reread ・・・ 設定ファイルを読み込む。

restart {プログラム名} ・・・ プロセスを再起動させる。プログラム名の箇所をallにすると全プログラムがリスタートされる。

relead ・・・ デーモンの再起動。

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