UNPKG

@backgroundjs/core

Version:

An extendible background job queue for js/ts applications

64 lines (63 loc) 2 kB
import { MongoClient } from "mongodb"; import { JobStorage } from "./base-storage"; import { Job, JobStatus } from "../types"; /** * 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(): Promise<Job | null>; /** * 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>; }