node nginx pm2 expressのアプリケーションの基盤を作りたくて色々試しているのですが、ログを実装したら謎の挙動をしていました。
ログ設定を行ってブラウザ更新すると何故か404になったりずっと読み込み中になったり謎の動作をしてしまいました。
pm2 monit
で状況を確認すると以下の様なログが出てました。
ログの本体は/root/.pm2/pm2.log
PM2 error: Change detected on path log/error.log for app web - restarting
PM2 log: Stopping app:web id:0
PM2 log: Stopping app:web id:1
PM2 error: Change detected on path log/error.log for app web - restarting
PM2 error: [Watch] Process name %s is being stopped so I won't restart it
PM2 error: Change detected on path log/info.log for app web - restarting
PM2 error: [Watch] Process name %s is being stopped so I won't restart it
PM2 log: App name:web id:1 disconnected
PM2 log: App [web] with id [1] and pid [615], exited with code [0] via signal [SIGINT]
PM2 error: Change detected on path log/access-2018-07-08.log for app web - restarting
PM2 error: [Watch] Process name %s is being stopped so I won't restart it
PM2 log: App [web] with id [0] and pid [610], exited with code [0] via signal [SIGINT]
PM2 log: pid=615 msg=process killed
PM2 log: Starting execution sequence in -cluster mode- for app name:web id:1
PM2 log: pid=610 msg=process killed
PM2 log: Starting execution sequence in -cluster mode- for app name:web id:0
PM2 log: App name:web id:0 online
PM2 error: (node:413) [DEP0007] DeprecationWarning: worker.suicide is deprecated. Please use worker.exitedAfterDisconnect.
PM2 log: App name:web id:1 online
PM2 error: (node:413) [DEP0007] DeprecationWarning: worker.suicide is deprecated. Please use worker.exitedAfterDisconnect.
開発中でファイルの変更を自動反映させたくて--watch
をつけてたからログが書き込まれる度にリロードされている様子。
普通にドキュメントに載っていました。
Watch & Restart
--watch
に気を取られてignore_watch
を見逃していました。
"watch": ["server", "client"],
"ignore_watch" : ["node_modules", "client/img"],
"watch_options": {
"followSymlinks": false
}
watch | 監視対象 |
ignore_watch | 監視対象外を設定(正規表現) |
"followSymlinks": false | シンボリックリンクの先を監視しない |
ということなので以下で設定しました。
processes.json
{
"apps" : [{
"name" : "dev",
"script" : "bin/www",
"instances" : "max",
"instance_var": "INSTANCE_ID",
"exec_mode" : "cluster",
"watch": ["./"],
"ignore_watch" : ["node_modules", "log", "\.git"],
"watch_options": {
"followSymlinks": false,
"usePolling": true
},
"env" : {
"NODE_ENV" : "development"
}
},
{
"name" : "prod",
"script" : "bin/www",
"instances" : "max",
"instance_var": "INSTANCE_ID",
"exec_mode" : "cluster",
"env" : {
"NODE_ENV" : "production"
}
}]
}
開発時のみwatch
したいのでenv_production
を使わずにappsで分けています。
アプリケーションフォルダをマウントしている場合はusePolling
をtrue
にしないとignore_watch
が効きませんでした。
When working with NFS devices you’ll need to set usePolling: true
pm2 start processes.json --only dev
これでとりあえず目的の動作は出来ました。