UNPKG

@sidequest/engine

Version:

@sidequest/engine is the core engine of SideQuest, a distributed background job processing system for Node.js and TypeScript.

48 lines (45 loc) 1.64 kB
import { logger } from '@sidequest/core'; import Piscina from 'piscina'; import { DEFAULT_RUNNER_PATH } from '../constants.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: DEFAULT_RUNNER_PATH, minThreads: this.nonNullConfig.minThreads, maxThreads: this.nonNullConfig.maxThreads, idleTimeout: this.nonNullConfig.idleWorkerTimeout, }); 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) { 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. */ destroy() { logger("RunnerPool").debug("Destroying worker pool"); void this.pool.destroy(); logger("RunnerPool").debug("Worker pool destroyed"); } } export { RunnerPool }; //# sourceMappingURL=runner-pool.js.map