index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const app = require("./server");
  2. const { port } = require("./config");
  3. const server = app.listen(port, function() {
  4. console.log("Webserver is ready");
  5. });
  6. //
  7. // need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
  8. // this also won't work on using npm start since:
  9. // https://github.com/npm/npm/issues/4603
  10. // https://github.com/npm/npm/pull/10868
  11. // https://github.com/RisingStack/kubernetes-graceful-shutdown-example/blob/master/src/index.js
  12. // if you want to use npm then start with `docker run --init` to help, but I still don't think it's
  13. // a graceful shutdown of node process
  14. //
  15. // quit on ctrl-c when running docker in terminal
  16. process.on("SIGINT", function onSigint() {
  17. console.info(
  18. "Got SIGINT (aka ctrl-c in docker). Graceful shutdown ",
  19. new Date().toISOString()
  20. );
  21. shutdown();
  22. });
  23. // quit properly on docker stop
  24. process.on("SIGTERM", function onSigterm() {
  25. console.info(
  26. "Got SIGTERM (docker container stop). Graceful shutdown ",
  27. new Date().toISOString()
  28. );
  29. shutdown();
  30. });
  31. // shut down server
  32. function shutdown() {
  33. server.close(function onServerClosed(err) {
  34. if (err) {
  35. console.error(err);
  36. process.exit(1);
  37. }
  38. process.exit(0);
  39. });
  40. }
  41. //
  42. // need above in docker container to properly exit
  43. //