Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: dev-admin cleans child process automatically #1633

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/xarc-app-dev/lib/dev-admin/admin-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const WebpackDevRelay = require("./webpack-dev-relay");
const { displayLogs } = require("./log-reader");
const { fork } = require("child_process");
const ConsoleIO = require("./console-io");
const logger = require("@xarc/app/lib/logger");
const winstonLogger = require("@xarc/app/lib/winston-logger");
const winston = require("winston");
const logger = winstonLogger(winston, false);
const { doCleanup } = require("./cleanup");
const xaa = require("xaa");
const { formUrl } = require("../utils");
const {
Expand Down Expand Up @@ -91,11 +94,6 @@ class AdminServer {
// await this.startProxyServer("--inspect-brk");
await this.startProxyServer();
}

this._shutdown ||
setTimeout(() => {
this.showMenu(true);
}, 100);
}

logTime(msg) {
Expand Down Expand Up @@ -214,6 +212,9 @@ ${proxyItem}<magenta>M</> - Show this menu <magenta>Q</> - Shutdown
this.kill(APP_SERVER_NAME, "SIGINT"),
this.kill(PROXY_SERVER_NAME, "SIGINT")
]);

this._io.shutdown();
await doCleanup();
this._io.exit();
}

Expand Down Expand Up @@ -545,6 +546,11 @@ ${instruction}`
checkLine: str => {
if (!this._fullyStarted && str.includes("server running")) {
this._fullyStarted = true;
// opens menu automatically once after startup
this._shutdown ||
setTimeout(() => {
this.showMenu(true);
}, 5000);
this.logTime(`App is ready in dev mode, total time took`);
}
return str;
Expand Down
47 changes: 47 additions & 0 deletions packages/xarc-app-dev/lib/dev-admin/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";

/* eslint-disable no-console, no-process-exit */
const { psChildren } = require("ps-get");

/**
* Attempt to clean up any child process remaining
*/
let cleaned = false;

const doCleanup = async () => {
if (cleaned) {
return;
}
cleaned = true;
const children = await psChildren(process.pid);
children.reverse().forEach(c => {
try {
console.log(
"detected child process left from dev-admin, PID:",
c.pid,
"- trying to terminate it"
);
process.kill(c.pid, "SIGINT");
} catch {
//
}
});
};

["uncaughtException", "unhandledRejection"].forEach(event => {
process.on(event, async err => {
console.log("dev-admin failure", event, err.stack);
await doCleanup();
process.exit(process.exitCode);
});
});

["SIGTERM", "SIGINT", "SIGHUP"].forEach(sig => {
process.on(sig, async name => {
console.log("dev-admin received signal:", name);
await doCleanup();
process.exit(process.exitCode);
});
});

module.exports = { doCleanup };
2 changes: 2 additions & 0 deletions packages/xarc-app-dev/lib/dev-admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const AdminServer = require("./admin-server");

const admin = new AdminServer(parsed);

require("./cleanup");

admin.start();
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"nyc": "^14.1.1",
"optional-require": "^1.0.0",
"prompts": "^2.2.1",
"ps-get": "^1.0.1",
"ps-get": "^1.1.0",
"regenerator-runtime": "^0.13.2",
"request": "^2.88.0",
"require-at": "^1.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/xarc-app/lib/winston-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/* eslint-disable no-magic-numbers */

const makeWinstonLogger = winston => {
const makeWinstonLogger = (winston, handlers = true) => {
return new winston.Logger({
exceptionHandlers: [
exceptionHandlers: handlers && [
new winston.transports.Console({
colorize: true,
prettyPrint: true
Expand Down