@sidequest/engine
Version:
@sidequest/engine is the core engine of SideQuest, a distributed background job processing system for Node.js and TypeScript.
101 lines (96 loc) • 4.85 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var core = require('@sidequest/core');
var node_fs = require('node:fs');
var node_url = require('node:url');
var _import = require('../utils/import.cjs');
var manualLoader = require('./manual-loader.cjs');
/**
* Runs a job by dynamically importing its script and executing the specified class.
* @param jobData The job data containing script and class information
* @param config The non-nullable engine configuration.
* @returns A promise resolving to the job result.
*/
async function run({ jobData, config }) {
await injectSidequestConfig(config);
let script = {};
try {
core.logger("Runner").debug(`Importing job script "${jobData.script}"`);
let scriptUrl;
if (jobData.script === manualLoader.MANUAL_SCRIPT_TAG) {
core.logger("Runner").debug("Manual job resolution is enabled; importing 'sidequest.jobs.js' job script.");
try {
// When manual job resolution is enabled, import from the sidequest.jobs.js script
if (!config.jobsFilePath) {
// If no custom path is provided, search for sidequest.jobs.js in parent directories
// throws if not found
scriptUrl = manualLoader.findSidequestJobsScriptInParentDirs();
}
else {
// If a custom path is provided, resolve it and ensure it exists
scriptUrl = manualLoader.resolveScriptPath(config.jobsFilePath);
if (!node_fs.existsSync(node_url.fileURLToPath(scriptUrl))) {
throw new Error(`The specified jobsFilePath does not exist. Resolved to: ${scriptUrl}`);
}
}
}
catch (error) {
const errorMessage = `Failed to locate 'sidequest.jobs.js' for manual job resolution: ${error instanceof Error ? error.message : String(error)}`;
core.logger("Runner").error(errorMessage);
const errorData = core.toErrorData(error);
return { __is_job_transition__: true, type: "failed", error: errorData };
}
}
else {
core.logger("Runner").debug("Manual job resolution is disabled; importing specified job script.");
// Convert relative path to absolute file URL for dynamic import
scriptUrl = core.resolveScriptPathForJob(jobData.script);
}
script = (await import(scriptUrl));
core.logger("Runner").debug(`Successfully imported job script "${jobData.script}"`);
}
catch (error) {
const errorMessage = `Failed to import job script "${jobData.script}": ${error instanceof Error ? error.message : String(error)}`;
core.logger("Runner").error(errorMessage);
const errorData = core.toErrorData(error);
return { __is_job_transition__: true, type: "failed", error: errorData };
}
const JobClass = script[jobData.class] ?? script.default;
if (!JobClass || typeof JobClass !== "function") {
const error = `Invalid job class: ${jobData.class}`;
core.logger("Runner").error(error);
const errorData = core.toErrorData(new Error(error));
return { __is_job_transition__: true, type: "failed", error: errorData };
}
const job = new JobClass(...jobData.constructor_args);
job.injectJobData(jobData);
core.logger("Runner").debug(`Executing job class "${jobData.class}" with args:`, jobData.args);
return job.perform(...jobData.args);
}
/**
* Injects the provided Sidequest engine configuration into the job script.
*
* Dynamically imports the `Sidequest` module and applies the configuration,
* ensuring migrations are skipped. Logs the process and handles errors gracefully,
* allowing execution to proceed even if configuration injection fails.
*
* @param config - The engine configuration object to inject into Sidequest.
* @returns A promise that resolves to `true` if the configuration was injected successfully,
* or `false` if an error occurred.
*/
async function injectSidequestConfig(config) {
try {
core.logger("Runner").debug("Injecting Sidequest config into job script");
const { Sidequest } = await _import.importSidequest();
await Sidequest.configure({ ...config, skipMigration: true });
core.logger("Runner").debug("Successfully injected Sidequest config");
return true;
}
catch (error) {
core.logger("Runner").warn(`Failed to inject Sidequest config: ${error instanceof Error ? error.message : String(error)}. Proceeding anyway.`);
return false;
}
}
exports.default = run;
exports.injectSidequestConfig = injectSidequestConfig;
//# sourceMappingURL=runner.cjs.map