Skip to content

Commit eb768a9

Browse files
committed
feat: add restart and rework close/stop/start/restart for #12
1 parent 6aaf4f0 commit eb768a9

File tree

4 files changed

+1471
-1070
lines changed

4 files changed

+1471
-1070
lines changed

lib/worker.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ function Worker(options) {
6969
* @param {function} callback
7070
*/
7171
Worker.prototype.start = function (cb) {
72-
this.updatePool(cb);
72+
this.updatePool(err => {
73+
this.logger.info('Worker started');
74+
cb(err);
75+
});
7376
};
7477
/**
7578
* Get a report of the actual situation of the worker
@@ -117,30 +120,39 @@ Worker.prototype.removePooler = function (cb) {
117120

118121
/**
119122
* Close the worker, this function might take 60 seconds to finish to do step function design
123+
* remove all the events attached to the worker
120124
* @param {function} callback
121125
*/
122126

123127
Worker.prototype.close = function (cb) {
124-
this.logger.info('Closing the worker ... this might take 60 seconds');
128+
this.stop(cb);
129+
this.removeAllListeners();
130+
};
131+
132+
/**
133+
* Stop the worker
134+
* But does not remove all the events attached to it
135+
* NB: worker.concurrency is set to 0
136+
* @param {function} callback
137+
*/
138+
139+
Worker.prototype.stop = function (cb) {
140+
this.logger.info('Stopping the worker ... this might take 60 seconds');
125141
this.concurrency = 0;
126142
this.updatePool(err => {
127-
this.logger.info('Worker closed');
143+
this.logger.info('Worker stopped');
128144
cb(err);
129145
});
130-
this.removeAllListeners();
131146
};
132147

133148
Worker.prototype.restart = function (cb) {
134-
this.logger.info('Restarting the worker ... this might take 60 seconds');
135149
const oldConcurrency = this.concurrency;
136-
this.concurrency = 0;
137-
this.updatePool(() => {
138-
this.logger.info('Worker closed');
150+
this.stop(err => {
151+
if (err) {
152+
return cb(err);
153+
}
139154
this.concurrency = oldConcurrency;
140-
this.updatePool(err => {
141-
this.logger.info('Worker started');
142-
cb(err);
143-
});
155+
this.start(cb);
144156
});
145157
};
146158

0 commit comments

Comments
 (0)