UNPKG

@backgroundjs/core

Version:

An extendible background job queue for js/ts applications

70 lines 2.31 kB
import { MongoClient } from "mongodb"; import { JobStorage } from "./base-storage.js"; import { Job, JobStatus } from "../types.js"; /** * MongoDB storage adapter for JobQueue * * This storage adapter uses MongoDB to store jobs, making it suitable * for distributed environments with multiple instances/processes. * * Note: You must install the 'mongodb' package to use this adapter: * npm install mongodb */ export declare class MongoDBJobStorage implements JobStorage { private readonly collection; private readonly mongoClient; private readonly logging; private readonly staleJobTimeout; constructor(mongoClient: MongoClient, options?: { collectionName?: string; logging?: boolean; staleJobTimeout?: number; }); /** * Save a job * @param job - The job to save */ saveJob(job: Job): Promise<void>; /** * Get a job by ID * @param id - The ID of the job to get * @returns The job with the given ID, or null if the job does not exist */ getJob(id: string): Promise<Job | null>; /** * Get all jobs by status * @param status - The status of the jobs to get * @returns An array of jobs with the given status */ getJobsByStatus(status: JobStatus): Promise<Job[]>; /** * Update a job * @param job - The job to update * @throws Error if the job is not found */ updateJob(job: Job): Promise<void>; /** * Acquire the next pending job * @returns The next pending job, or null if no pending jobs are available */ acquireNextJob(handlerNames?: string[]): Promise<Job | null>; /** * Acquire multiple pending jobs for prefetching * @param batchSize - Number of jobs to prefetch * @returns Array of acquired jobs */ acquireNextJobs(batchSize: number, handlerNames?: string[]): Promise<Job[]>; /** * Complete a job * @param jobId - The ID of the job to complete * @param result - The result of the job */ completeJob(jobId: string, result: any): Promise<void>; /** * Fail a job * @param jobId - The ID of the job to fail * @param error - The error message */ failJob(jobId: string, error: string): Promise<void>; } //# sourceMappingURL=mongodb-storage.d.ts.map