Skip to content

Commit

Permalink
Improve worker exited unexpectedly error
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Mar 11, 2019
1 parent da0c602 commit b5d9c2f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
18 changes: 15 additions & 3 deletions lib/WorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,23 @@ function WorkerHandler(script, _options) {
me.requestQueue = [];
}

var worker = this.worker;
// listen for worker messages error and exit
this.worker.on('error', onError);
this.worker.on('exit', function () {
var error = new Error('Worker terminated unexpectedly');
onError(error);
this.worker.on('exit', function (exitCode, signalCode) {
var message = 'Workerpool Worker terminated Unexpectedly\n';

message += ' exitCode: `' + exitCode + '`\n';
message += ' signalCode: `' + signalCode + '`\n';

message += ' workerpool.script: `' + me.script + '`\n';
message += ' spawnArgs: `' + worker.spawnargs + '`\n';
message += ' spawnfile: `' + worker.spawnfile + '`\n'

message += ' stdout: `' + worker.stdout + '`\n'
message += ' stderr: `' + worker.stderr + '`\n'

onError(new Error(message));
});

this.processing = Object.create(null); // queue with tasks currently in progress
Expand Down
11 changes: 9 additions & 2 deletions test/Pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,15 @@ describe('Pool', function () {
.then(function () {
assert('Promise should not be resolved');
})
.catch(function (err) {
assert.equal(err.toString(), 'Error: Worker terminated unexpectedly');
.catch(function (err) {
assert.ok(err.toString().match(/Error: Workerpool Worker terminated Unexpectedly/));
assert.ok(err.toString().match(/exitCode: `.*`/));
assert.ok(err.toString().match(/signalCode: `.*`/));
assert.ok(err.toString().match(/workerpool.script: `.*\.js`/));
assert.ok(err.toString().match(/spawnArgs: `.*\.js`/));
assert.ok(err.toString().match(/spawnfile: `.*node`/));
assert.ok(err.toString().match(/stdout: `null`/));
assert.ok(err.toString().match(/stderr: `null`/));

assert.equal(pool.workers.length, 0);

Expand Down
19 changes: 17 additions & 2 deletions test/WorkerHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,15 @@ describe('WorkerHandler', function () {
})
.catch(function (err) {
assert(err instanceof Error);
assert.ok(err.stack.match(/Error: Worker terminated unexpectedly/));

assert.ok(err.toString().match(/Error: Workerpool Worker terminated Unexpectedly/));
assert.ok(err.toString().match(/exitCode: `.*`/));
assert.ok(err.toString().match(/signalCode: `.*`/));
assert.ok(err.toString().match(/workerpool.script: `.*\.js`/));
assert.ok(err.toString().match(/spawnArgs: `.*\.js`/));
assert.ok(err.toString().match(/spawnfile: `.*node`/));
assert.ok(err.toString().match(/stdout: `null`/));
assert.ok(err.toString().match(/stderr: `null`/));
done();
});

Expand All @@ -270,7 +277,15 @@ describe('WorkerHandler', function () {
})
.catch(function (err) {
assert(err instanceof Error);
assert.ok(err.stack.match(/Error: Worker terminated unexpectedly/));

assert.ok(err.stack.match(/Error: Workerpool Worker terminated Unexpectedly/));
assert.ok(err.stack.match(/exitCode: `.*`/));
assert.ok(err.stack.match(/signalCode: `.*`/));
assert.ok(err.stack.match(/workerpool.script: `.*\.js`/));
assert.ok(err.stack.match(/spawnArgs: `.*\.js`/));
assert.ok(err.stack.match(/spawnfile: `.*node`/));
assert.ok(err.stack.match(/stdout: `null`/));
assert.ok(err.stack.match(/stderr: `null`/));

done();
});
Expand Down

0 comments on commit b5d9c2f

Please sign in to comment.