mirror of
https://github.com/OptimalBits/bull.git
synced 2026-07-02 08:27:43 +08:00
fix(sandbox): wait for result of sending start command
This commit is contained in:
+5
-11
@@ -10,13 +10,7 @@ let processor;
|
||||
let currentJobPromise;
|
||||
|
||||
const { promisify } = require('util');
|
||||
|
||||
// same as process.send but waits until the send is complete
|
||||
|
||||
// the async version is used below because otherwise
|
||||
// the termination handler may exit before the parent
|
||||
// process has recived the messages it requires
|
||||
const processSendAsync = promisify(process.send.bind(process));
|
||||
const { asyncSend } = require('./utils');
|
||||
|
||||
// https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify
|
||||
if (!('toJSON' in Error.prototype)) {
|
||||
@@ -95,7 +89,7 @@ process.on('message', msg => {
|
||||
currentJobPromise = (async () => {
|
||||
try {
|
||||
const result = (await processor(wrapJob(msg.job))) || {};
|
||||
await processSendAsync({
|
||||
await asyncSend(process, {
|
||||
cmd: 'completed',
|
||||
value: result
|
||||
});
|
||||
@@ -104,7 +98,7 @@ process.on('message', msg => {
|
||||
// eslint-disable-next-line no-ex-assign
|
||||
err = new Error(err);
|
||||
}
|
||||
await processSendAsync({
|
||||
await asyncSend(process, {
|
||||
cmd: 'failed',
|
||||
value: err
|
||||
});
|
||||
@@ -156,7 +150,7 @@ function wrapJob(job) {
|
||||
// so that we can return it from this process synchronously.
|
||||
progressValue = progress;
|
||||
// Send message to update job progress.
|
||||
return processSendAsync({
|
||||
return asyncSend(process, {
|
||||
cmd: 'progress',
|
||||
value: progress
|
||||
});
|
||||
@@ -178,7 +172,7 @@ function wrapJob(job) {
|
||||
* Emulate the real job `log` function.
|
||||
*/
|
||||
job.log = function(row) {
|
||||
return processSendAsync({
|
||||
return asyncSend(process, {
|
||||
cmd: 'log',
|
||||
value: row
|
||||
});
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const { asyncSend } = require('./utils');
|
||||
|
||||
module.exports = function(processFile, childPool) {
|
||||
return function process(job) {
|
||||
return childPool.retain(processFile).then(child => {
|
||||
return childPool.retain(processFile).then(async child => {
|
||||
let msgHandler;
|
||||
let exitHandler;
|
||||
|
||||
child.send({
|
||||
await asyncSend(child, {
|
||||
cmd: 'start',
|
||||
job: job
|
||||
});
|
||||
|
||||
+23
-2
@@ -44,6 +44,27 @@ function killAsync(child, signal, timeoutMs) {
|
||||
return onExit;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
killAsync
|
||||
/*
|
||||
asyncSend
|
||||
Same as process.send but waits until the send is complete
|
||||
the async version is used below because otherwise
|
||||
the termination handler may exit before the parent
|
||||
process has recived the messages it requires
|
||||
*/
|
||||
|
||||
const asyncSend = (proc, msg) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
proc.send(msg, err => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
killAsync,
|
||||
asyncSend
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user