Skip to content

Commit e48905a

Browse files
committed
refactor (clirunner) move process termination to clirunner
- in debug mode, move process shutdown hooks to clirunner - in cloud mode, move process exit step to clirunner
1 parent d2b6b78 commit e48905a

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

lib/cli/clirunner.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,23 @@ if (!program.args.length) {
6262
}
6363

6464
function debug() {
65-
buildOptions(true)
66-
.then(CodeRunner.debug)
67-
.catch(showError);
65+
buildOptions(true).then((opts) => {
66+
const runner = CodeRunner.debug(opts);
67+
68+
const handleTermination = () => {
69+
logger.info('Termination signal received. Shutting down.');
70+
71+
runner.stop().then(() => process.exit(0));
72+
};
73+
74+
process.on('SIGINT', handleTermination);
75+
process.on('SIGTERM', handleTermination);
76+
}, showError);
6877
}
6978

7079
function cloud() {
7180
buildOptions()
7281
.then((opts) => {
73-
7482
opts.driverHost = this.driverHost;
7583
opts.driverPort = this.driverPort;
7684
opts.driverRequestId = this.driverRequestId;
@@ -85,6 +93,10 @@ function cloud() {
8593

8694
return CodeRunner.cloud(opts);
8795
})
96+
.then(() => {
97+
// force process.exit to avoid "non declared" asynchronous jobs started in business logic
98+
process.exit(0);
99+
})
88100
.catch(showError);
89101
}
90102

@@ -100,6 +112,7 @@ function deploy() {
100112

101113
function showError(err) {
102114
logger.error('Error:', err.message || err);
115+
process.exit(1);
103116
}
104117

105118
function assertValueIsUUID(value, name) {
@@ -120,7 +133,7 @@ function buildOptions(appIsRequired) {
120133
if (appIsRequired) {
121134
opts.app = opts.app || {};
122135

123-
const gatherAppOptions = async(function* () {
136+
const gatherAppOptions = async(function*() {
124137
opts.app.id = program.appId || opts.app.id || (yield prompt('Application ID'));
125138
opts.app.version = program.appVersion || opts.app.version || (yield prompt('Application Version'));
126139
opts.app.secretKey = program.appKey || opts.app.secretKey || (yield prompt('Secret Key'));

lib/server-code/runners/cloud.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,5 @@ exports.start = function(opts) {
2424
});
2525
}
2626

27-
function exit() {
28-
// force process.exit to avoid "non declared" asynchronous jobs started in business logic
29-
process.exit(0);
30-
}
31-
32-
function logError(err) {
33-
logger.error(`Error: ${err.message || err}`);
34-
}
35-
36-
return getTask()
37-
.then(processTask)
38-
.catch(logError)
39-
.then(exit);
27+
return getTask().then(processTask);
4028
};

lib/server-code/runners/debug.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class DebugCodeRunner {
6363
}
6464

6565
start() {
66-
this.addShutdownHook();
67-
6866
Promise.resolve()
6967
.then(() => this.registerRunner())
7068
.then(() => this.keepDebugSessionAlive())
@@ -86,31 +84,25 @@ class DebugCodeRunner {
8684
return ServerCodeModel.build(process.cwd(), files);
8785
}
8886

89-
addShutdownHook() {
90-
const hook = this.stop.bind(this);
91-
92-
process.on('exit', hook);
93-
process.on('SIGINT', hook);
94-
process.on('SIGTERM', hook);
95-
}
96-
9787
stop() {
9888
if (!this.stopped) {
99-
logger.info('Shutting down Code Runner. Please wait..');
100-
10189
if (this.sessionRenewalTimer) {
10290
clearTimeout(this.sessionRenewalTimer);
10391
}
10492

93+
const stopTasks = [];
94+
10595
if (this.debugSessionId) {
106-
this.apiServer.unregisterRunner();
107-
this.messageBroker.del(this.debugSessionId);
96+
stopTasks.push(this.apiServer.unregisterRunner());
97+
stopTasks.push(this.messageBroker.del(this.debugSessionId));
10898
}
10999

110100
this.taskAcquirer.quit();
111101
this.messageBroker.quit();
112102

113103
this.stopped = true;
104+
105+
return Promise.all(stopTasks);
114106
}
115107
}
116108

lib/server-code/services/api-server.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class ApiServerService {
8585
}
8686

8787
unregisterRunner() {
88-
logger.info(`Unregistering runner on ${this.serverUrl}`);
89-
9088
return this.sendRequest('unregisterRunner');
9189
}
9290

0 commit comments

Comments
 (0)