Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved signals to separate file

  • Loading branch information...
commit 3bdb038f5cb00a3bc5877e8c94324870ec0cfd13 1 parent 35aef2d
@mekwall authored
Showing with 73 additions and 43 deletions.
  1. +3 −43 src/overseer.js
  2. +70 −0 src/signals.js
View
46 src/overseer.js
@@ -132,54 +132,14 @@ overseer.run = function() {
}
}
+ fs.writeFileSync(PIDFILE, process.pid);
+
process.on("exit", function(){
console.info("Master: Shutting down...");
fs.unlinkSync(PIDFILE);
});
- fs.writeFileSync(PIDFILE, process.pid);
-
- /**
- * Graceful restart/shutdown
- */
- process.on("SIGHUP",function(){
- console.info("Master: Received SIGHUP from system");
- process.emit("restartWorkers");
- });
-
- process.on("SIGUSR2",function(){
- console.info("Master: Received SIGUSR2 from system");
- overseer.workers.forEach(function (worker) {
- worker.send({ cmd: "stop" });
- });
- });
-
- process.on("SIGTERM", function () {
- console.info("Master: Received SIGTERM from system");
- process.exit();
- });
-
- process.on("SIGINT", function () {
- console.info("Master: Recieved SIGINT from system");
- process.exit();
- });
-
- // Windows doesn't use POSIX signals
- if (process.platform === "win32") {
- const keypress = require("keypress");
- keypress(process.stdin);
- process.stdin.resume();
- process.stdin.setRawMode(true);
- process.stdin.setEncoding("utf8");
- process.stdin.on("keypress", function(char, key) {
- if (key && key.ctrl && key.name == "c") {
- process.emit("SIGUSR2");
- process.exit(0);
- } else if (key && key.ctrl && key.name == "r") {
- process.emit("SIGHUP");
- }
- });
- }
+ require("./signals")(overseer);
if (WATCH) {
const watch = require("watch");
View
70 src/signals.js
@@ -0,0 +1,70 @@
+// Module dependencies
+const fs = require("fs");
+
+module.exports = function (overseer) {
+
+ // Debugger
+ process.on("SIGUSR1", function () {
+ console.info("Master: Received SIGUSR1 from system");
+ });
+
+ // Graceful restart
+ process.on("SIGUSR2",function(){
+ console.info("Master: Received SIGUSR2 from system");
+ overseer.emit("restartWorkers");
+ });
+
+ // Graceful shutdown
+ process.on("SIGHUP",function(){
+ console.info("Master: Received SIGHUP from system");
+ overseer.workers.forEach(function (worker) {
+ worker.send({ cmd: "stop" });
+ });
+ });
+
+ // Terminate process
+ process.on("SIGTERM", function () {
+ console.info("Master: Received SIGTERM from system");
+ process.exit();
+ });
+
+ // User wish to interrupt the process
+ process.on("SIGINT", function () {
+ console.info("Master: Recieved SIGINT from system");
+ process.exit();
+ });
+
+ // Suspend process
+ process.on("SIGTSTP", function () {
+ console.info("Master: Recieved SIGTSTP from system");
+ });
+
+ // Continue process
+ process.on("SIGCONT", function () {
+ console.info("Master: Recieved SIGCONT from system");
+ });
+
+ // Windows doesn't use POSIX signals
+ if (process.platform === "win32") {
+ const tty = require("tty");
+ // Check if it's a terminal or not
+ if (tty.isatty(process.stdout.fd)) {
+ const keypress = require("keypress");
+ keypress(process.stdin);
+ process.stdin.resume();
+ process.stdin.setRawMode(true);
+ process.stdin.setEncoding("utf8");
+ process.stdin.on("keypress", function(char, key) {
+ if (key && key.ctrl && key.name == "c") {
+ process.emit("SIGINT");
+ } else if (key && key.ctrl && key.name == "z") {
+ process.emit("SIGTSTP");
+ } else if (key && key.ctrl && key.name == "d") {
+ process.emit("SIGUSR1");
+ } else if (key && key.ctrl && key.name == "r") {
+ process.emit("SIGUSR2");
+ }
+ });
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.