2022-07-05 16:30:52 -06:00
|
|
|
'use strict';
|
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
// Called by `child_process` `fork` in `run-in-child-process.js` so we can
|
|
|
|
// get the data and exit the process cleanly.
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-07-06 18:24:29 -06:00
|
|
|
const assert = require('assert');
|
|
|
|
|
2022-08-29 18:13:56 -06:00
|
|
|
const RethrownError = require('../lib/rethrown-error');
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-08-29 18:13:56 -06:00
|
|
|
// Serialize the error and send it back up to the parent process so we can
|
|
|
|
// interact with it and know what happened when the process exits.
|
|
|
|
async function serializeError(err) {
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
process.send(
|
|
|
|
{
|
|
|
|
error: true,
|
|
|
|
name: err.name,
|
|
|
|
message: err.message,
|
|
|
|
stack: err.stack,
|
|
|
|
},
|
|
|
|
(sendErr) => {
|
|
|
|
if (sendErr) {
|
|
|
|
// We just log here instead of rejecting because it's more important
|
|
|
|
// to see the original error we are trying to send up. Let's just
|
|
|
|
// throw the original error below.
|
|
|
|
const sendErrWithDescription = new RethrownError(
|
|
|
|
'Failed to send error to the parent process',
|
|
|
|
sendErr
|
|
|
|
);
|
|
|
|
console.error(sendErrWithDescription);
|
|
|
|
// This will end up hitting the `unhandledRejection` handler and
|
|
|
|
// serializing this error instead (worth a shot) 🤷♀️
|
|
|
|
throw sendErrWithDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't exit the process after encountering one of these because maybe it
|
2022-09-02 19:49:06 -06:00
|
|
|
// doesn't matter to the main-line process in the module.
|
2022-08-29 18:13:56 -06:00
|
|
|
//
|
|
|
|
// If we don't listen for these events, the child will exit with status code 1
|
|
|
|
// (error) when they occur.
|
|
|
|
process.on('uncaughtException', async (err /*, origin*/) => {
|
2022-09-02 19:49:06 -06:00
|
|
|
console.log('2 uncaughtException', err);
|
2022-08-29 18:13:56 -06:00
|
|
|
await serializeError(new RethrownError('uncaughtException in child process', err));
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', async (reason /*, promise*/) => {
|
|
|
|
await serializeError(new RethrownError('unhandledRejection in child process', reason));
|
|
|
|
});
|
|
|
|
|
2022-07-05 17:00:29 -06:00
|
|
|
// Only kick everything off once we receive the options. We pass in the options
|
|
|
|
// this way instead of argv because we will run into `Error: spawn E2BIG` and
|
|
|
|
// `Error: spawn ENAMETOOLONG` with argv.
|
2022-09-02 19:49:06 -06:00
|
|
|
process.on('message', async (runArguments) => {
|
2022-07-05 16:30:52 -06:00
|
|
|
try {
|
2022-09-02 19:49:06 -06:00
|
|
|
assert(runArguments);
|
2022-07-05 16:30:52 -06:00
|
|
|
|
2022-09-02 19:49:06 -06:00
|
|
|
// Require the module that we're supposed to run
|
|
|
|
const modulePath = process.argv[2];
|
|
|
|
assert(
|
|
|
|
modulePath,
|
|
|
|
'Expected `modulePath` to be passed into `child-fork-script.js` via argv[2]'
|
|
|
|
);
|
|
|
|
const moduleToRun = require(modulePath);
|
|
|
|
|
|
|
|
// Run the module
|
|
|
|
const result = await moduleToRun(runArguments);
|
|
|
|
|
|
|
|
assert(result, `No result returned from module we ran (${modulePath}).`);
|
2022-07-06 18:24:29 -06:00
|
|
|
|
|
|
|
// Send back the data we need to the parent.
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
process.send(
|
|
|
|
{
|
2022-09-02 19:49:06 -06:00
|
|
|
data: result,
|
2022-07-06 18:24:29 -06:00
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit once we know the data was sent out. We can't gurantee the
|
|
|
|
// message was received but this should work pretty well.
|
|
|
|
//
|
|
|
|
// Related:
|
|
|
|
// - https://stackoverflow.com/questions/34627546/process-send-is-sync-async-on-nix-windows
|
|
|
|
// - https://github.com/nodejs/node/commit/56d9584a0ead78874ca9d4de2e55b41c4056e502
|
|
|
|
// - https://github.com/nodejs/node/issues/6767
|
|
|
|
process.exit(0);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
2022-07-05 16:30:52 -06:00
|
|
|
});
|
|
|
|
} catch (err) {
|
2022-08-29 18:13:56 -06:00
|
|
|
// We need to wait for the error to completely send to the parent
|
2022-09-02 17:49:45 -06:00
|
|
|
// process before we exit the process.
|
2022-08-29 18:13:56 -06:00
|
|
|
await serializeError(err);
|
|
|
|
|
2022-09-02 17:49:45 -06:00
|
|
|
// Fail the process and exit
|
|
|
|
process.exit(1);
|
2022-07-05 16:30:52 -06:00
|
|
|
}
|
2022-07-05 17:00:29 -06:00
|
|
|
});
|