|
1 | | -const EventEmitter = require('events').EventEmitter; |
| 1 | +const {EventEmitter} = require('events'); |
2 | 2 | const util = require('util'); |
3 | 3 | const AWS = require('aws-sdk'); |
4 | 4 | const parser = require('aws-arn-parser'); |
@@ -69,7 +69,10 @@ function Worker(options) { |
69 | 69 | * @param {function} callback |
70 | 70 | */ |
71 | 71 | Worker.prototype.start = function (cb) { |
72 | | - this.updatePool(cb); |
| 72 | + this.updatePool(err => { |
| 73 | + this.logger.info('Worker started'); |
| 74 | + cb(err); |
| 75 | + }); |
73 | 76 | }; |
74 | 77 | /** |
75 | 78 | * Get a report of the actual situation of the worker |
@@ -117,25 +120,48 @@ Worker.prototype.removePooler = function (cb) { |
117 | 120 |
|
118 | 121 | /** |
119 | 122 | * 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 |
120 | 124 | * @param {function} callback |
121 | 125 | */ |
122 | 126 |
|
123 | 127 | 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'); |
125 | 141 | this.concurrency = 0; |
126 | 142 | this.updatePool(err => { |
127 | | - this.logger.info('Worker closed'); |
| 143 | + this.logger.info('Worker stopped'); |
128 | 144 | cb(err); |
129 | 145 | }); |
130 | | - this.removeAllListeners(); |
| 146 | +}; |
| 147 | + |
| 148 | +Worker.prototype.restart = function (cb) { |
| 149 | + const oldConcurrency = this.concurrency; |
| 150 | + this.stop(err => { |
| 151 | + if (err) { |
| 152 | + return cb(err); |
| 153 | + } |
| 154 | + this.concurrency = oldConcurrency; |
| 155 | + this.start(cb); |
| 156 | + }); |
131 | 157 | }; |
132 | 158 |
|
133 | 159 | Worker.prototype.execute = function (input, cb, heartbeat) { |
134 | 160 | setImmediate(() => { |
135 | 161 | try { |
136 | 162 | this.fn(input, cb, heartbeat); |
137 | | - } catch (err) { |
138 | | - cb(err); |
| 163 | + } catch (error) { |
| 164 | + cb(error); |
139 | 165 | } |
140 | 166 | }); |
141 | 167 | }; |
|
0 commit comments