7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PM2でファイルの変更を自動反映させる

Last updated at Posted at 2018-07-08

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で分けています。

アプリケーションフォルダをマウントしている場合はusePollingtrueにしないとignore_watchが効きませんでした。

When working with NFS devices you’ll need to set usePolling: true

pm2 start processes.json --only dev

これでとりあえず目的の動作は出来ました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?