UNPKG

@backgroundjs/core

Version:

An extendible background job queue for js/ts applications

114 lines (113 loc) 3.52 kB
import { Job, JobStatus } from "../types"; import { JobStorage } from "./base-storage"; import type { Redis } from "ioredis"; /** * Redis storage adapter for JobQueue * * This storage adapter uses Redis to store jobs, making it suitable * for distributed environments with multiple instances/processes. * * Note: You must install the 'ioredis' package to use this adapter: * npm install ioredis */ export interface RedisStorage extends JobStorage { acquireNextJob(ttl?: number): Promise<Job | null>; getJobsByPriority(priority: number): Promise<Job[]>; getScheduledJobs(startTime: Date, endTime?: Date): Promise<Job[]>; clear(): Promise<void>; } export declare class RedisJobStorage implements RedisStorage { private readonly redis; private readonly keyPrefix; private readonly jobListKey; private readonly priorityQueueKeys; private readonly scheduledJobsKey; private readonly logging; private readonly saveJobScript; private readonly updateJobScript; private readonly moveScheduledJobsScript; private readonly completeJobScript; private readonly failJobScript; private readonly staleJobTimeout; /** * Create a new RedisJobStorage * * @param redis - An ioredis client instance * @param options - Configuration options */ constructor(redis: Redis, options?: { keyPrefix?: string; logging?: boolean; staleJobTimeout?: number; }); /** * Save a job to Redis using a Lua script */ saveJob(job: Job): Promise<void>; /** * Get a job by ID */ getJob(id: string): Promise<Job | null>; /** * Get jobs by status */ getJobsByStatus(status: JobStatus): Promise<Job[]>; /** * Update a job using a Lua script */ updateJob(job: Job): Promise<void>; /** * Acquire the next job from the queue, respecting priorities and scheduled times * Also checks for stale jobs that have been processing for too long * @returns The next job or null if no job is available */ acquireNextJob(ttl?: number): Promise<Job | null>; /** * Move scheduled jobs to priority queues using a Lua script * @private */ private moveScheduledJobs; /** * Serialize a job object for Redis storage * @private */ private serializeJob; /** * Deserialize a job object from Redis storage * @private */ private deserializeJob; private getJobKey; private getStatusKey; /** * Get jobs by priority * @param priority - The priority level (1-5) * @returns Array of jobs with the specified priority */ getJobsByPriority(priority: number): Promise<Job[]>; /** * Get scheduled jobs within a time range * @param startTime - Start of time range (inclusive) * @param endTime - End of time range (inclusive) * @returns Array of scheduled jobs within the time range */ getScheduledJobs(startTime?: Date, endTime?: Date): Promise<Job[]>; /** * Complete a job using a Lua script * * @param jobId - ID of the job to complete * @param result - Result of the job */ completeJob(jobId: string, result: any): Promise<void>; /** * Fail a job using a Lua script * * @param jobId - ID of the job to fail * @param error - Error message */ failJob(jobId: string, error: string): Promise<void>; /** * Clear all jobs */ clear(): Promise<void>; }