Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

App cleanly exits but does not restart #1176

Closed
remotezygote opened this issue Dec 16, 2017 · 12 comments
Closed

App cleanly exits but does not restart #1176

remotezygote opened this issue Dec 16, 2017 · 12 comments

Comments

@remotezygote
Copy link

remotezygote commented Dec 16, 2017

  • nodemon -v: 1.13.2
  • node -v: v9.2.0
  • Operating system/terminal environment: OSX High Sierra, Terminal.app, zsh
  • Command you ran:
    yarn start where my start script is nodemon | pino-colada (works the same without the piped output to pino-colada, but is less intelligible. I have also tried this with the -L flag, and that seems to have no effect. I am guessing I am missing something...

In package.json:

"nodemonConfig": {
    "verbose": false,
    "watch": ["dist"],
    "inspect": false,
    "signal": "SIGTERM"
  },

I am running a webpack --watch to recompile the script into dist/.

Output:

yarn run v1.3.2
$ nodemon -L | pino-colada
[nodemon] 1.13.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/john/workspaces/cabochon/packages/pods/dist/**/*
[nodemon] starting `node dist/index.js`
11:32:46 ✨  Listening at http://localhost:8063 (pid: 99802)...
[nodemon] restarting due to changes...
11:32:52 ✨  Shutting down database connection...
11:32:52 ✨  Shutting down service...
11:32:52 ✨  Cleanly shut down.
[nodemon] clean exit - waiting for changes before restart

Expected behaviour

I would expect the script to receive a SIGUSR2 signal, then after cleanly exiting, restart the script.

Actual behaviour

It gets the SIGUSR2 signal, cleanly exits, then does not restart.

Steps to reproduce

I believe the steps are well documented above.


If applicable, please append the --dump flag on your command and include the output here **ensuring to remove any sensitive/personal details or tokens.

Dump output:

[nodemon] 1.13.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/REDACTED/workspaces/REDACTED/packages/REDACTED/dist/**/*
--------------
node: v9.2.0
nodemon: 1.13.2
command: /usr/local/bin/node /Users/REDACTED/workspaces/REDACTED/packages/REDACTED/node_modules/.bin/nodemon --dump
cwd: /Users/REDACTED/workspaces/REDACTED/packages/REDACTED
OS: darwin x64
--------------
{ run: false,
  system: { cwd: '/Users/REDACTED/workspaces/REDACTED/packages/REDACTED' },
  required: false,
  dirs: [ '/Users/REDACTED/workspaces/REDACTED/packages/REDACTED/dist' ],
  timeout: 1000,
  options:
   { dump: true,
     verbose: false,
     watch: [ 'dist', re: /dist/ ],
     inspect: false,
     signal: 'SIGTERM',
     ignore:
      [ '.git',
        '.nyc_output',
        '.sass-cache',
        'bower_components',
        'coverage',
        'node_modules',
        re: /\.git|\.nyc_output|\.sass\-cache|bower_components|coverage|node_modules/ ],
     ignoreRoot:
      [ '.git',
        '.nyc_output',
        '.sass-cache',
        'bower_components',
        'coverage',
        'node_modules' ],
     restartable: 'rs',
     colours: true,
     execMap: { py: 'python', rb: 'ruby' },
     stdin: true,
     runOnChangeOnly: false,
     stdout: true,
     watchOptions: {},
     execOptions:
      { script: 'dist/index.js',
        exec: 'node',
        args: [],
        scriptPosition: 0,
        nodeArgs: undefined,
        execArgs: [],
        ext: 'js,json',
        env: {} },
     monitor:
      [ '/Users/REDACTED/workspaces/REDACTED/packages/REDACTED/dist/**/*',
        '!.git',
        '!.nyc_output',
        '!.sass-cache',
        '!bower_components',
        '!coverage',
        '!/Users/REDACTED/workspaces/REDACTED/packages/REDACTED/node_modules/**/*' ] },
  load: [Function],
  reset: [Function: reset],
  lastStarted: 0,
  loaded:
   [ '/Users/REDACTED/workspaces/REDACTED/packages/REDACTED/package.json' ],
  watchInterval: null,
  signal: 'SIGTERM',
  command:
   { raw: { executable: 'node', args: [ 'dist/index.js' ] },
     string: 'node dist/index.js' } }
--------------
@remotezygote
Copy link
Author

Also worth noting, if I don't add the signal config option, I don't get a clean signal to the script - I am trapping signals with the following code, and none of it runs unless I specify to signal to use:

export const shutdown = () => {
  shutdownClient()

  if (server) {
    logger.info('Shutting down service...')
    server.close()
  }

  logger.info('Cleanly shut down.')
  running = false
  // process.exit(0) // this line does not seem to have any effect
}

const shutdownSignals = [
  'SIGTERM',
  'SIGINT',
  'SIGUSR2',
]

shutdownSignals.forEach(signal => process.on(signal, shutdown))

@remotezygote
Copy link
Author

Also also, if I follow the instructions here and do:
process.kill(process.pid, 'SIGUSR2')
I get this message at the end instead of the clean exit one, but it still does not restart:
[nodemon] app crashed - waiting for file changes before starting...

@remotezygote
Copy link
Author

I have now taken out all the signal handling code and am using http-graceful-shutdown, but getting the same result. I make a change, it shuts down cleanly but doesn't restart. Make another change, it starts again, then another change, it shuts down but no restart.

@remotezygote
Copy link
Author

I just walked versions back by minor, and when I got back to 1.11.0, it started working perfectly.

@remy
Copy link
Owner

remy commented Dec 16, 2017

Can you try with 1.13.3? There was a process kill change in .2 that was fixed in .3 - it's possible it's that.

Otherwise, can you provide a package.json + index.js via a gist to replicate with?

@remy
Copy link
Owner

remy commented Dec 16, 2017

(sorry, I see you have your package there already, and I suspect the example file you included is enough - can you just give it a test with the latest build?)

@4sh3
Copy link

4sh3 commented Dec 18, 2017

Same error with nodejs 6.0.0, when I save the file is not restarting the nodemon execution, but remains active in the console without reload.

@remy
Copy link
Owner

remy commented Dec 18, 2017

@4sh3 can you confirm nodemon version?

@jonreader
Copy link

@remy

I'm getting what seems to be the same issue.

The standard rs command also does not work.

OS: MacOS Sierra (10.12.6)
Node: 6.0.0
Nodemon: 1.13.3

@remotezygote
Copy link
Author

Tried it with 1.13.3 - no better. It works perfectly in 1.11.0 but nothing after that.

@remy
Copy link
Owner

remy commented Dec 20, 2017

Okay, I think I've got to the bottom of this, but it's down to how you're handling the signal inside your own script.

When your script handles the signal, it still needs to exit, but it needs to exit with a signal that nodemon can respond to. Since your original code was exiting with process.exit(0) nodemon reads that as "the script ran and exited cleanly" - hence why it's waiting for changes.

Using your example from an earlier comment, here's the changes required to make the example work:

--- a/yours.js
+++ b/mine.js
-export const shutdown = () => {
+export const shutdown = signal => () => {
   shutdownClient();
 
   if (server) {
     logger.info('Shutting down service...');
     server.close();
   }

   logger.info('Cleanly shut down.');
   running = false;
-  // process.exit(0) // this line does not seem to have any effect
+  process.kill(process.pid, signal); // this line does not seem to have any effect
 };
 
 const shutdownSignals = ['SIGTERM', 'SIGINT', 'SIGUSR2'];
 
-shutdownSignals.forEach(signal => process.on(signal, shutdown));
+shutdownSignals.forEach(signal => process.once(signal, shutdown(signal)));

The most important two changes are that instead of process.on(…) I'm using .once since we're going to re-send the kill signal, and we want the script to exit and not handle it. The second change being that I change process.exit for process.kill.

I hope that makes sense. It is in the documentation, but I'll make a note to highlight the importance of using once.

@bj97301
Copy link

bj97301 commented Jun 28, 2018

Didnt seem to work for me =/

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants