profoundjs
Version:
Profound.js Framework and Server
82 lines (72 loc) • 2.59 kB
JavaScript
;
const path= require("path");
const argv = require("minimist")(process.argv.slice(2));
// Worker process for running automated tests in isolation
// This runs in a forked child process and communicates with the parent via IPC
// If this script is run directly, run the main function
if (require.main === module) {
run(argv).then(() => {
// console.log("Ready for work!");
}).catch((error) => {
console.log("Error in mass test worker:", error);
process.exit(1);
});
}
exports.run = run;
async function run(parms) {
// Load Profound.js framework and config (inherits env vars from parent for transformation mode)
// We need to support both installed and dev versions of Profound.js, so we try both paths
try {
require("profoundjs");
}
catch (error) {
require(path.join(process.cwd(), "profoundjs", "index.js"));
}
// Now load the config
await profound.applyConfig();
if (typeof process.send === "function") {
process.on("uncaughtException", (err) => {
process.send({ type: "CRASH", error: { message: err.message, stack: err.stack } });
process.exit(1);
});
// Listen for messages from parent process
process.on("message", async(msg) => {
if (msg.type === "RUN_TEST") {
// Construct options for runTest
// initOpts (called by runTest) will create the query function and set puiLibrary
const runOpts = {
...msg.data,
mode: "mass",
onComplete: async(err, finishedOpts) => {
// Send test result back to parent
process.send({
type: "TEST_COMPLETE",
data: {
replayId: finishedOpts?.replayId,
status: finishedOpts?.status || (err ? "errored" : "unknown"),
error: err ? { message: err.message, stack: err.stack } : null
}
});
// Request next test from parent
process.send({ type: "REQUEST_WORK" });
}
};
// Run the test using existing runTest function
// runTest will call initOpts which creates query function, fieldsArr, etc.
try {
profound.autoTesting.runTest(runOpts);
}
catch (err) {
console.error(`Error calling runTest:`, err);
}
}
else if (msg.type === "NO_MORE_WORK") {
// Queue is empty, exit gracefully
process.exit(0);
}
});
// Worker is initialized and ready for work
process.send({ type: "REQUEST_WORK" });
}
};