UNPKG

@cloud-copilot/job

Version:

Async job runners with defined worker pools

63 lines 2.1 kB
import { Job, JobResult, Logger } from './job.js'; /** * A worker pool that will run jobs concurrently using promises up to a specified limit. * If no work is being done, it will wait for jobs to be added and run them up to the * maximum concurrency. * * This is designed to create a central queue for jobs of many types to be processed * in a streaming fashion, allowing for efficient resource usage and managed network requests. * */ export declare class ConcurrentWorkerPool<T = void, P = Record<string, unknown>> { private concurrency; private logger; private jobCounter; private resolveMap; private queue; private activeJobs; private waitingResolvers; private workers; private isAcceptingWork; private workAvailablePromise; private resolveWorkAvailable; /** * Create a new runner with the specified concurrency. * * @param concurrency - The maximum number of jobs to run concurrently. * @param logger - Logger instance for logging long-running jobs. */ constructor(concurrency: number, logger: Logger); private worker; private waitForWorkAvailable; private ensureWorkers; private notifyWorkersOfNewWork; private checkIfIdle; /** * Add a job to the queue */ enqueue(job: Job<T, P>): Promise<JobResult<T, P>>; /** * Add multiple jobs to the queue */ enqueueAll(jobs: Job<T, P>[]): Promise<JobResult<T, P>>[]; /** * Returns a promise that resolves when all queued work is complete */ waitForIdle(): Promise<void>; /** * Shutdown the queue - no new jobs will be accepted, but existing jobs will complete. * * Returns when a promise that resolves when all jobs have been processed and * the onComplete callback has been called for each job. */ finishAllWork(): Promise<void>; /** * Get the current queue length */ get queueLength(): number; /** * Get the number of currently active jobs */ get activeJobCount(): number; } //# sourceMappingURL=ConcurrentWorkerPool.d.ts.map