import { Config } from '../config'; import { JobService } from '../job'; /** * A Processor's task is to continuously attempt to retrieve new jobs * that it is capable of handling. * * When handling a job, it will mark the job as in progress, call the appropriate handler * and save the new execution status after the handler finishes executing. * * If it detects that it is not picking up any new jobs for some time, it will back off and introduce * a sleep between */ export declare class Processor { private readonly config; private readonly jobService; private readonly jobMethods; private readonly logger; private batchWaitTimeMs; private stopProcessingCallback; private isProcessing; private runningJobs; /** * Builds a new Processor. It will only handle jobs which * have a `method` contained in `jobMethods`. If you pass `undefined` `jobMethods`, * the Processor will handle all jobs regardless of `method` * * The Processor will start processing once the `start` method is called. * @param jobMethods `method` of jobs to handle. If undefined, all jobs regardless of `method` will be handled. * @param overrideJobHandlers Used for setting job handlers for a specific processor instance only, instead of the global library-wide ones set with `configure` function */ constructor(config: Config, jobService: JobService, jobMethods?: string[]); /** * Initiates job processing for this processor. */ start(): void; /** * Returns the number of jobs currently running in the background. */ getRunningJobsSize(): number; /** * Clears jobs that have been running for too long from the set of running jobs. * Too long is defined by the diff between current time and start being greater than configured `jobRunningTimeoutMs`. */ private clearTimedOutJobsFromRunning; private performStop; private processBatches; /** * * @returns Promise resolving `true` when no more jobs are being executed. * Will wait for current running batch to finish or time it out if it takes * more time than is configured in config under `jobRunningTimeoutMs`. */ stop(): Promise; /** * Waits a number of ms before returning. Used for ensuring * we don't repeatedly spam the database when we repeatedly get no * scheduled jobs from it. Works with an linearly increasing backoff with a max value. */ private waitAfterEmptyBatch; /** * Called when we get a batch with at least one job from database. * This means we should lower wait times between batches because * we now have jobs to perform. */ private lowerBatchWaitTime; private resolveHandler; /** * Processes next batch of eligible jobs. * * A job is eligible if: * - it is/has not been processed by another processor * - the current time is after the job's `runAfter` * - the job method is included in the `jobMethods` of this processor * @returns `true` if there was a job to process, `false` if there was not. */ private processNextBatch; private processSingleJob; }