dominik
dominik

Reputation: 137

Node.js background worker is not gracefully shutting down on heroku

I have the following node.js worker running on Heroku (simplified). It's running as a worker process using a Procfile on a single dyno. I am expecting it to gracefully shut down with an exit code of 0. However, it always exits with an exit code of 1 and doesn't even execute the full shutdown function.

Heroku gives a process 30s to gracefully shut down (see https://devcenter.heroku.com/articles/dynos#shutdown). Node version is 16.13.0.

Locally it of course works like a charm.

Any help is highly appreciated.

let running = true;

(async function () {
  console.log('starting')
  while (running) {
    console.log('ping')
    await new Promise(resolve => setTimeout(resolve, 1000))
  }
})()

process.on('SIGTERM', function () {
  console.log('received SIGTERM')
  console.log('shutting down')
  running = false
  setTimeout(() => {
    console.log('finished shutting down')
    process.exit(0)
  }, 1000)
})

Heroku logs:

2021-11-11T17:27:18.884817+00:00 heroku[test-worker.1]: Restarting
2021-11-11T17:27:18.935900+00:00 heroku[test-worker.1]: State changed from up to starting
2021-11-11T17:27:18.831078+00:00 app[test-worker.1]: ping
2021-11-11T17:27:19.594077+00:00 heroku[test-worker.1]: Stopping all processes with SIGTERM
2021-11-11T17:27:19.654762+00:00 app[test-worker.1]: received SIGTERM
2021-11-11T17:27:19.654801+00:00 app[test-worker.1]: shutting down
2021-11-11T17:27:19.786370+00:00 heroku[test-worker.1]: Process exited with status 1
2021-11-11T17:27:22.660423+00:00 heroku[test-worker.1]: Starting process with command `yarn test-worker`
2021-11-11T17:27:23.532721+00:00 heroku[test-worker.1]: State changed from starting to up
2021-11-11T17:27:24.083102+00:00 app[test-worker.1]: yarn run v1.22.17
2021-11-11T17:27:24.133902+00:00 app[test-worker.1]: $ node lib/test-worker/src/index.js
2021-11-11T17:27:24.192198+00:00 app[test-worker.1]: starting
2021-11-11T17:27:24.192700+00:00 app[test-worker.1]: ping

Upvotes: 1

Views: 281

Answers (1)

dominik
dominik

Reputation: 137

With the help of Heroku support, we were able to figure out that the reason is using yarn. Apparently, yarn is not waiting for child processes to gracefully shut down. See https://github.com/yarnpkg/yarn/issues/4667.

The solution is to not use yarn in your Procfile and to use node directly.

Upvotes: 1

Related Questions