Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.54k stars 2.62k forks source link

Watch doesn't watch for file changes using json ecosystem file #4599

Open chrisdemetriad opened 4 years ago

chrisdemetriad commented 4 years ago

What's going wrong?

Using a ecosystem file to manage a few scripts, the watch property doesn't watch for file changes.

Whatever paths/values I use in the watch property (absolute, relative, true), if I add one, or in the script name, it doesn't work. I spent two days on it.

It doesn't work.

Changes to test1.js doesn't trigger a restart. If I start test3.js manually with pm2 start bots/test3.js --watch, then changes to test1.js will trigger a restart. process.cwd() returns /home/chris.

How could we reproduce this issue?

  1. Use processes.json
  2. Load two scripts as below
  3. Start with pm2 start processes.json
  4. pm2 list shows all files are watching
  5. Edit test1.js - no restart.
  6. Start individual script: pm2 start test3.js --watch.
  7. Check with pm2 list, if all good, then
  8. Edit test1.js - test3.js will see the change and restart.

    Supporting information

chris@localhost:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic
chris@localhost:~$ cat bots/bots.json
[
        {
                "name": "test1",
                "script": "./bots/test1.js",
                "watch": "./bots/test1.js"
        },
        {
                "name": "test2",
                "script": "./bots/test2.js",
                "cwd": "./bots",
                "watch": true,
                "ignore_watch": [
                        "db",
                        "/db"
                ],
                "watch_options": {
                        "followSymlinks": false
                }
        },
...
chris@localhost:~$ pm2 report

--- PM2 REPORT (Wed Feb 05 2020 15:13:47 GMT+0000 (Coordinated Universal Time))

--- Daemon -------------------------------------------------
pm2d version         : 3.0.3
node version         : 12.12.0
node path            : /usr/bin/pm2
argv                 : /home/chris/.nvm/versions/node/v12.12.0/bin/node,/usr/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : chris
uid                  : 1000
gid                  : 1000
uptime               : 42min

--- CLI ----------------------------------------------------
local pm2            : 3.0.3
node version         : 12.12.0
node path            : /usr/bin/pm2
argv                 : /home/chris/.nvm/versions/node/v12.12.0/bin/node,/usr/bin/pm2,report
argv0                : node
user                 : chris
uid                  : 1000
gid                  : 1000

--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : AMD EPYC 7601 32-Core Processor
cpus nb              : 1
freemem              : 743907328
totalmem             : 2090225664
home                 : /home/chris

--- PM2 list -----------------------------------------------
┌────────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
│ App name           │ id │ mode │ pid   │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
├────────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
│ test1        │ 1  │ fork │ 14937 │ online │ 31      │ 5m     │ 0.1% │ 38.2 MB   │ chris │ enabled  │
│ test2           │ 0  │ fork │ 14930 │ online │ 31      │ 5m     │ 0%   │ 38.2 MB   │ chris │ enabled  │
chrisgo commented 4 years ago

I can confirm this

pm2 --version => 4.2.3 Mac running Vagrant (Virtualbox) with generic/debian10 box

Running pm2 start index.js --watch triggers the watch (pm2 restarts when files change on a vagrant shared folder)

but

Running pm2 start all or pm2 start --only index (using ecosystem.config.js) does not trigger a restart even if pm2 status says watch is enabled

module.exports = {

  // PM2 Documentation
  // https://pm2.io/doc/en/runtime/overview/

  // ==================== APPS ====================

  apps: [{
    name: 'index',
    cwd: '/var/www/project/nodejs/',
    script: 'index.js',
    node_args: '--harmony',
    combine_logs: true,
    out_file: '/home/project/.pm2/logs/access.log',
    error_file: '/home/project/.pm2/logs/error.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss',
    restart: true,
    watch_options: {
      usePolling: true,
      interval: 500,
    },
    ignore_watch: ['logs', 'public', 'views', 'app/commands'],
    env: {
      watch: true,
      NODE_ENV: 'development',
    },
    env_dev: {
      watch: false,
      NODE_ENV: 'development',
    },
    env_staging: {
      watch: false,
      NODE_ENV: 'staging',
    },
    env_production: {
      watch: false,
      NODE_ENV: 'production',
    },
  }],
}

Another thing I noticed is that with the pm2 start index.js --watch, the logs show

PM2         | [Watch] Start watching index
PM2         | App [index:1] starting in -fork mode-
PM2         | App [index:1] online

When starting with pm2 start --only index, it does not show the [Watch] line above

PM2         | App [index:1] starting in -fork mode-
PM2         | App [index:1] online

This also seems to be working pm2 start --only index --watch (adding --watch) but this is probably just suffixing the ".js" file at the end of index so not really using the ecosystem.config.js file

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

omgMath commented 4 years ago

This still happens in 4.4.4.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

omgMath commented 4 years ago

No update has been released in the meantime, i.e. this is still an open issue. Will retest at the next stale notification if needed.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

omgMath commented 4 years ago

Still on 4.4.4

movy commented 2 years ago

I never managed to make default watch settings work in Ubuntu, however using polling via advanced watch options worked: "watch": true, "ignore_watch" : ["node_modules"], "watch_options": { "usePolling": true, "interval": 1000 }

More info: https://github.com/buunguyen/PM2/blob/master/ADVANCED_README.md#watch--restart https://github.com/paulmillr/chokidar#api