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

fix gracefulExit #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 9 additions & 14 deletions lib/multi-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _Terminal = require('./terminal');
const _BarElement = require('./generic-bar');
const _options = require('./options');
const _EventEmitter = require('events');
const _ShutdownListener = require('./shutdown-listener.js');

// Progress-Bar constructor
module.exports = class MultiBar extends _EventEmitter{
Expand Down Expand Up @@ -33,8 +34,9 @@ module.exports = class MultiBar extends _EventEmitter{
// logging output buffer
this.loggingBuffer = [];

// callback used for gracefulExit
this.sigintCallback = null;
// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
const signals = ['SIGINT', 'SIGTERM'];
this.shuthdownListener = new _ShutdownListener(signals, this.stop.bind(this));
}

// add a new bar to the stack
Expand Down Expand Up @@ -65,12 +67,10 @@ module.exports = class MultiBar extends _EventEmitter{
}

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.sigintCallback === null && this.options.gracefulExit){
this.sigintCallback = this.stop.bind(this);
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
if (this.options.gracefulExit){
this.shuthdownListener.attach();
}

// multiprogress already active ?
if (!this.isActive){
// hide the cursor ?
Expand Down Expand Up @@ -188,13 +188,6 @@ module.exports = class MultiBar extends _EventEmitter{
clearTimeout(this.timer);
this.timer = null;

// remove sigint listener
if (this.sigintCallback){
process.removeListener('SIGINT', this.sigintCallback);
process.removeListener('SIGTERM', this.sigintCallback);
this.sigintCallback = null;
}

// set flag
this.isActive = false;

Expand Down Expand Up @@ -239,6 +232,8 @@ module.exports = class MultiBar extends _EventEmitter{
this.terminal.newline();
}

this.shuthdownListener.detach();

// trigger event
this.emit('stop');
}
Expand Down
41 changes: 41 additions & 0 deletions lib/shutdown-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

module.exports = class ShutdownListener {
constructor(signals, cleanupFunction, processOverride) {
this._signals = signals;
this._isSignalReceived = false;
this._isAttached = false;
this._process = processOverride ? processOverride : process;
this._handler = (signal) => {
if (this._isSignalReceived) {
return;
}
this._isSignalReceived = true;
Promise.resolve(cleanupFunction())
.catch((e) => {
console.log(e);
})
.then(() => {
this._signals.forEach(sig => this._process.removeListener(sig, this._handler));
this._process.kill(this._process.pid, signal);
})
}
}

attach() {
if (this._isAttached) {
return;
}
this._signals.forEach(sig => {
this._process.on(sig, this._handler)
});
this._isAttached = true;
}

detach() {
if (this._isSignalReceived) {
return;
}
this._signals.forEach(sig => this._process.removeListener(sig, this._handler));
this._isAttached = false;
}
}
22 changes: 8 additions & 14 deletions lib/single-bar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _GenericBar = require('./generic-bar');
const _options = require('./options');
const _ShutdownListener = require("./shutdown-listener.js");

// Progress-Bar constructor
module.exports = class SingleBar extends _GenericBar{
Expand All @@ -18,8 +19,9 @@ module.exports = class SingleBar extends _GenericBar{
// update interval
this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);

// callback used for gracefulExit
this.sigintCallback = null;
// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
const signals = ['SIGINT', 'SIGTERM'];
this.shuthdownListener = new _ShutdownListener(signals, this.stop.bind(this));
}

// internal render function
Expand Down Expand Up @@ -66,10 +68,8 @@ module.exports = class SingleBar extends _GenericBar{
}

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.sigintCallback === null && this.options.gracefulExit){
this.sigintCallback = this.stop.bind(this);
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
if (this.options.gracefulExit){
this.shuthdownListener.attach();
}

// save current cursor settings
Expand Down Expand Up @@ -99,13 +99,6 @@ module.exports = class SingleBar extends _GenericBar{
return;
}

// remove sigint listener
if (this.sigintCallback){
process.removeListener('SIGINT', this.sigintCallback);
process.removeListener('SIGTERM', this.sigintCallback);
this.sigintCallback = null;
}

// trigger final rendering
this.render();

Expand Down Expand Up @@ -137,5 +130,6 @@ module.exports = class SingleBar extends _GenericBar{
// new line on complete
this.terminal.newline();
}
this.shuthdownListener.detach();
}
}
}