@sidequest/engine
Version:
@sidequest/engine is the core engine of SideQuest, a distributed background job processing system for Node.js and TypeScript.
50 lines (46 loc) • 1.64 kB
JavaScript
;
var core = require('@sidequest/core');
var path = require('path');
var Piscina = require('piscina');
const runnerPath = path.resolve(__dirname, "runner.js");
/**
* A pool of worker threads for running jobs in parallel using Piscina.
*/
class RunnerPool {
nonNullConfig;
/** The underlying Piscina worker pool. */
pool;
/**
* Creates a new RunnerPool.
* @param nonNullConfig The non-nullable engine configuration.
*/
constructor(nonNullConfig) {
this.nonNullConfig = nonNullConfig;
this.pool = new Piscina({
filename: runnerPath,
minThreads: this.nonNullConfig.minThreads,
maxThreads: this.nonNullConfig.maxThreads,
idleTimeout: this.nonNullConfig.idleWorkerTimeout,
});
core.logger("RunnerPool").debug(`Created worker pool with min ${this.nonNullConfig.minThreads} threads and max ${this.nonNullConfig.maxThreads} threads`);
}
/**
* Runs a job in the worker pool.
* @param job The job data to run.
* @param signal Optional event emitter for cancellation.
* @returns A promise resolving to the job result.
*/
run(job, signal) {
core.logger("RunnerPool").debug(`Running job ${job.id} in pool`);
return this.pool.run({ jobData: job, config: this.nonNullConfig }, { signal });
}
/**
* Destroys the worker pool and releases resources.
*/
async destroy() {
core.logger("RunnerPool").debug("Destroying worker pool");
await this.pool.destroy();
}
}
exports.RunnerPool = RunnerPool;
//# sourceMappingURL=runner-pool.cjs.map