UNPKG

@backgroundjs/core

Version:

An extendible background job queue for js/ts applications

107 lines 3.45 kB
import { Job, JobStatus } from "../types.js"; import { JobStorage } from "./base-storage.js"; import type { Redis } from "ioredis"; export interface RedisStorage extends JobStorage { getJobsByPriority(priority: number): Promise<Job[]>; getScheduledJobs(startTime: Date, endTime?: Date): Promise<Job[]>; clear(): Promise<void>; acquireNextJobs(batchSize: number, handlerNames?: string[]): Promise<Job[]>; } 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 atomicAcquireScript; 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; }); acquireNextJobs(batchSize: number, handlerNames?: string[]): Promise<Job[]>; /** * 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(handlerNames?: string[]): 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>; } //# sourceMappingURL=redis-storage.d.ts.map