### TypeScript Queue and Job Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/INDEX.md Demonstrates how to import Bull with TypeScript definitions, create a typed queue, add a job with specific data, and process jobs with type safety. Ensure you have Bull and its TypeScript types installed. ```typescript import Queue, { Job, JobOptions } from 'bull'; const queue = new Queue('myqueue'); const job: Job = await queue.add(data, { attempts: 3 }); queue.process(async (job: Job) => { const { userId } = job.data; return { success: true }; }); ``` -------------------------------- ### Complete Bull Queue Configuration Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md A comprehensive example demonstrating the configuration of Redis connection, key prefix, rate limiting, advanced settings, default job options, and metrics for a production queue. ```javascript const Queue = require('bull'); const queue = new Queue('production-tasks', { // Redis connection redis: { host: process.env.REDIS_HOST || 'localhost', port: process.env.REDIS_PORT || 6379, password: process.env.REDIS_PASSWORD, db: parseInt(process.env.REDIS_DB || '0') }, // Key prefix prefix: 'prod', // Rate limiting limiter: { max: 50, duration: 60000, groupKey: 'tenantId' }, // Advanced settings settings: { lockDuration: 45000, lockRenewTime: 15000, stalledInterval: 10000, maxStalledCount: 3, guardInterval: 3000, retryProcessDelay: 2000 }, // Default job options defaultJobOptions: { attempts: 5, backoff: { type: 'exponential', delay: 3000 }, timeout: 60000, removeOnComplete: { age: 86400, // Keep for 1 day count: 1000 // Or keep max 1000 }, removeOnFail: { age: 604800 // Keep failures for 7 days } }, // Metrics metrics: { maxDataPoints: 500 } }); module.exports = queue; ``` -------------------------------- ### Install TypeScript Definitions Source: https://github.com/optimalbits/bull/blob/develop/README.md Commands to install the DefinitelyTyped definitions for Bull. ```bash npm install @types/bull --save-dev ``` ```bash yarn add --dev @types/bull ``` -------------------------------- ### Example Rate Limiter Configuration Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Shows how to configure rate limiting for a queue, including basic limits and an example of grouping limits by a specific key in job data. ```typescript const queue = new Queue('myqueue', { limiter: { max: 10, duration: 60000 // Process max 10 jobs per minute } }); // With grouping (per user) const queue = new Queue('myqueue', { limiter: { max: 5, duration: 60000, groupKey: 'userId' // Limit 5 jobs per userId per minute } }); ``` -------------------------------- ### Install Bull Package Source: https://github.com/optimalbits/bull/blob/develop/docs/README.md Commands to install the Bull library using npm or yarn. ```bash $ npm install bull --save ``` ```bash $ yarn add bull ``` -------------------------------- ### Install Bull Library Source: https://github.com/optimalbits/bull/blob/develop/README.md Commands to install the Bull package using npm or yarn. ```bash npm install bull --save ``` ```bash yarn add bull ``` -------------------------------- ### Basic Bull Queue Setup Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Demonstrates how to initialize a Bull queue, add a job with options, process jobs, and listen for completion and failure events. ```javascript const Queue = require('bull'); const queue = new Queue('myqueue'); // Add job const job = await queue.add({ userId: 123 }, { attempts: 3, backoff: { type: 'exponential', delay: 2000 }, timeout: 30000 }); // Process job queue.process(async (job) => { console.log('Processing:', job.data); return { success: true }; }); // Listen for events queue.on('completed', (job, result) => { console.log('Done:', result); }); queue.on('failed', (job, error) => { console.log('Failed:', error.message); }); ``` -------------------------------- ### Example Queue Initialization Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Demonstrates how to create a new Bull queue with custom Redis connection details, prefix, advanced settings, and default job options. ```typescript const queue = new Queue('myqueue', { redis: 'redis://localhost:6379', prefix: 'myapp', settings: { lockDuration: 30000, maxStalledCount: 2 }, defaultJobOptions: { attempts: 3, backoff: { type: 'exponential', delay: 2000 } } }); ``` -------------------------------- ### Queue Constructor Examples Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Demonstrates different ways to instantiate a Queue, including simple instantiation, using a Redis URL, and providing custom options for Redis connection and settings. Remember to wait for the queue to be ready using `isReady()`. ```javascript const Queue = require('bull'); // Simple instantiation const myQueue = new Queue('myqueue'); // With Redis URL const myQueue = new Queue('myqueue', 'redis://localhost:6379'); // With options const myQueue = new Queue('myqueue', { redis: { host: 'localhost', port: 6379 }, settings: { lockDuration: 30000, maxStalledCount: 2 } }); // Wait for queue to be ready await myQueue.isReady(); ``` -------------------------------- ### Redis Connection Configuration Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Configure Redis connection details, including host, port, password, and a custom retry strategy. ```javascript const queue = new Queue('myqueue', { redis: { host: 'redis.example.com', port: 6380, password: 'mypassword', db: 0, retryStrategy: (times) => { return Math.min(Math.exp(times), 20000); } } }); ``` -------------------------------- ### Process Jobs with Promise (PDF Example) Source: https://github.com/optimalbits/bull/blob/develop/README.md Illustrates processing a PDF job by returning a promise from the processor function. ```javascript pdfQueue.process(function (job) { // Processors can also return promises instead of using the done callback return pdfAsyncProcessor(); }); ``` -------------------------------- ### Add Job with JobOptions Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Demonstrates how to add a job to the queue with various configuration options, including priority, delay, attempts, backoff strategy, timeout, and removal settings. ```typescript const job = await queue.add( { userId: 123 }, { priority: 5, delay: 10000, attempts: 3, backoff: { type: 'exponential', delay: 2000 }, timeout: 30000, removeOnComplete: true, removeOnFail: { age: 3600 // Remove failed jobs older than 1 hour } } ); ``` -------------------------------- ### Add Job with Fixed Backoff Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Example of adding a job with a fixed backoff strategy, setting a 5-second delay between retries. This ensures consistent retry intervals. ```typescript // Fixed backoff: 5 seconds between retries const job = await queue.add(data, { attempts: 3, backoff: { type: 'fixed', delay: 5000 } }); ``` -------------------------------- ### Add Job with Exponential Backoff Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Example of adding a job with an exponential backoff strategy, using a base delay of 2 seconds. This results in increasing delays between retries (e.g., 2s, 6s, 14s). ```typescript // Exponential: 2s, 6s, 14s, 30s, etc. const job = await queue.add(data, { attempts: 5, backoff: { type: 'exponential', delay: 2000 } }); ``` -------------------------------- ### Add Job with Cron Repeat Limit Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Example of adding a job that repeats every hour, with a limit of 24 repetitions. This prevents a job from repeating indefinitely. ```typescript // Every hour, max 24 times const job = await queue.add(data, { repeat: { cron: '0 * * * *', limit: 24 } }); ``` -------------------------------- ### Handle job completion signaling Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Examples showing the correct and incorrect ways to signal job completion when using Promises versus the done callback. ```js // THIS WON'T WORK!! queue.process(function (job, done) { // Oops! done callback here! return Promise.resolve(); }); ``` ```js queue.process(function (job) { // No done callback here :) return Promise.resolve(); }); ``` -------------------------------- ### Create a separate processor module Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Example of a processor module file that exports a function to handle jobs, suitable for blocking code. ```js // my-processor.js module.exports = function (job) { // do some job return value; }; ``` -------------------------------- ### Server-to-Server Communication with Bull (Server A) Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/patterns.md Example of Server A acting as a message sender and receiver. It defines a queue to receive messages and another queue to send messages to Server B. ```javascript const Queue = require('bull'); // Receive queue const receiveQueue = new Queue('Server A'); receiveQueue.process(async (job) => { console.log('Received message from Server B:', job.data); return { received: true }; }); // Send queue const sendQueue = new Queue('Server B'); await sendQueue.add({ message: 'Hello from Server A' }); ``` -------------------------------- ### Add Job with Weekday Cron Repeat and Timezone Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Example of adding a job that repeats every weekday at 5 PM, specifying the 'America/New_York' timezone. This allows for precise scheduling across different timezones. ```typescript // Every weekday at 5 PM const job = await queue.add(data, { repeat: { cron: '0 17 * * 1-5', tz: 'America/New_York' } }); ``` -------------------------------- ### Server-to-Server Communication with Bull (Server B) Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/patterns.md Example of Server B acting as a message sender and receiver. It defines a queue to receive messages from Server A and another queue to send messages back. ```javascript const Queue = require('bull'); // Receive queue const receiveQueue = new Queue('Server B'); receiveQueue.process(async (job) => { console.log('Received message from Server A:', job.data); return { received: true }; }); // Send queue const sendQueue = new Queue('Server A'); await sendQueue.add({ message: 'Hello from Server B' }); ``` -------------------------------- ### Add Job with Start and End Dates Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/repeating-jobs.md Schedules a seasonal job using cron, specifying both a start date and an end date. The job will only run within this defined period. ```javascript await queue.add( { action: 'seasonal' }, { repeat: { cron: '0 0 * * *', startDate: new Date('2025-06-01'), endDate: new Date('2025-08-31') } } ); // This job will start on June 1 and stop after August 31 ``` -------------------------------- ### Add Job with Shorthand Backoff Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Demonstrates the shorthand for configuring backoff, where a number directly specifies a fixed delay in milliseconds. This simplifies the configuration for fixed backoff strategies. ```typescript // Or shorthand (defaults to fixed) const job = await queue.add(data, { attempts: 3, backoff: 5000 }); ``` -------------------------------- ### Periodically Clean Old Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/patterns.md Set up an interval to periodically clean up old jobs from the queue. This example shows how to remove completed jobs older than a week and failed jobs older than a month. ```javascript const cleanTask = setInterval(async () => { // Remove completed jobs older than 1 week await queue.clean(604800000, 'completed'); // Remove failed jobs older than 30 days await queue.clean(2592000000, 'failed'); }, 3600000); // Run every hour // Stop cleaning on graceful shutdown process.on('SIGTERM', () => { clearInterval(cleanTask); }); ``` -------------------------------- ### getFailed Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs that have failed. Supports pagination with start and end indices. ```APIDOC ## getFailed ### Description Gets an array of failed jobs. Supports pagination with start and end indices. ### Method `getFailed(start?: number, end?: number): Promise>>` ### Parameters #### Query Parameters - **start** (number) - Optional - Start index (0-based), defaults to 0. - **end** (number) - Optional - End index (-1 means last), defaults to -1. ### Returns Promise>> - array of Job objects in the failed state. ``` -------------------------------- ### Define Custom Backoff Strategy Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Example of defining a custom backoff strategy using the backoffStrategies configuration object. ```js backoffStrategies: { jitter: function () { return 5000 + Math.random() * 500; } } ``` -------------------------------- ### getCompleted Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs that have completed successfully. Supports pagination with start and end indices. ```APIDOC ## getCompleted ### Description Gets an array of completed jobs. Supports pagination with start and end indices. ### Method `getCompleted(start?: number, end?: number): Promise>>` ### Parameters #### Query Parameters - **start** (number) - Optional - Start index (0-based), defaults to 0. - **end** (number) - Optional - End index (-1 means last), defaults to -1. ### Returns Promise>> - array of Job objects in the completed state. ``` -------------------------------- ### getDelayed Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs currently in the delayed state. Supports pagination with start and end indices. ```APIDOC ## getDelayed ### Description Gets an array of delayed jobs. Supports pagination with start and end indices. ### Method `getDelayed(start?: number, end?: number): Promise>>` ### Parameters #### Query Parameters - **start** (number) - Optional - Start index (0-based), defaults to 0. - **end** (number) - Optional - End index (-1 means last), defaults to -1. ### Returns Promise>> - array of Job objects in the delayed state. ``` -------------------------------- ### getWaiting Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs currently in the waiting state. Supports pagination with start and end indices. ```APIDOC ## getWaiting ### Description Gets an array of waiting jobs. Supports pagination with start and end indices. ### Method `getWaiting(start?: number, end?: number): Promise>>` ### Parameters #### Query Parameters - **start** (number) - Optional - Start index (0-based), defaults to 0. - **end** (number) - Optional - End index (-1 means last), defaults to -1. ### Returns Promise>> - array of Job objects in the waiting state. ### Example ```javascript // Get first 10 waiting jobs const jobs = await queue.getWaiting(0, 9); console.log(`Found ${jobs.length} waiting jobs`); // Get all waiting jobs const allWaiting = await queue.getWaiting(0, -1); ``` ``` -------------------------------- ### getActive Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs currently in the active (processing) state. Supports pagination with start and end indices. ```APIDOC ## getActive ### Description Gets an array of active (currently processing) jobs. Supports pagination with start and end indices. ### Method `getActive(start?: number, end?: number): Promise>>` ### Parameters #### Query Parameters - **start** (number) - Optional - Start index (0-based), defaults to 0. - **end** (number) - Optional - End index (-1 means last), defaults to -1. ### Returns Promise>> - array of Job objects in the active state. ### Example ```javascript const activeJobs = await queue.getActive(); console.log(`${activeJobs.length} jobs currently processing`); ``` ``` -------------------------------- ### Initialize Bull Queues Source: https://github.com/optimalbits/bull/blob/develop/README.md Demonstrates initializing Bull queues with different Redis connection configurations. Specify Redis connection using a string URL or an object. ```javascript const Queue = require('bull'); const videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379'); const audioQueue = new Queue('audio transcoding', { redis: { port: 6379, host: '127.0.0.1', password: 'foobared' } }); // Specify Redis connection using object const imageQueue = new Queue('image transcoding'); const pdfQueue = new Queue('pdf transcoding'); ``` -------------------------------- ### Retrieve Failed Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs that have failed during processing. Supports start and end index parameters. ```javascript const failedJobs = await queue.getFailed(); ``` -------------------------------- ### Add Temporary Repeating Job with Limit Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/repeating-jobs.md Creates a job that repeats at a set interval but is limited to a specific number of executions. This example runs a job every minute for a total of 60 times, effectively for one hour. ```javascript // Repeat every minute for the next hour await queue.add(data, { repeat: { every: 60000, limit: 60 // Stop after 60 iterations } }); ``` -------------------------------- ### Get Failed Jobs Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves an array of failed jobs within a specified range (start and end indices). ```typescript getFailed(start?: number, end?: number, opts?: GetterOpts) : Promise> ``` -------------------------------- ### Get Completed Jobs Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves an array of completed jobs within a specified range (start and end indices). ```typescript getCompleted(start?: number, end?: number, opts?: GetterOpts) : Promise> ``` -------------------------------- ### Get Delayed Jobs Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves an array of delayed jobs within a specified range (start and end indices). ```typescript getDelayed(start?: number, end?: number, opts?: GetterOpts) : Promise> ``` -------------------------------- ### Get Active Jobs Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves an array of active jobs within a specified range (start and end indices). ```typescript getActive(start?: number, end?: number, opts?: GetterOpts) : Promise> ``` -------------------------------- ### Get Waiting Jobs Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves an array of waiting jobs within a specified range (start and end indices). ```typescript getWaiting(start?: number, end?: number, opts?: GetterOpts) : Promise> ``` -------------------------------- ### Add Multiple Repeatable Jobs for Specific Times Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/repeating-jobs.md Schedules a job to run multiple times per day by adding separate repeatable jobs for each desired execution time. This example sets up jobs for 8 AM, 4 PM, and midnight. ```javascript // 3 times per day (8 AM, 4 PM, midnight) // Add 3 separate repeatable jobs await queue.add(data, { repeat: { cron: '0 8 * * *' } }); await queue.add(data, { repeat: { cron: '0 16 * * *' } }); await queue.add(data, { repeat: { cron: '0 0 * * *' } }); ``` -------------------------------- ### Get Job Logs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Retrieves the logs associated with a specific job ID. You can specify start and end indices for the logs. ```javascript const logs = await queue.getJobLogs('job-123'); ``` -------------------------------- ### Retrieve Delayed Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs that are scheduled to run at a future time. Supports start and end index parameters. ```javascript const delayedJobs = await queue.getDelayed(); ``` -------------------------------- ### Queue Constructor with URL and Options Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Combine a Redis URL with an options object for detailed configuration. ```javascript new Queue('queueName', 'redis://localhost:6379', { settings: { ... } }); ``` -------------------------------- ### Initialize a Bull Queue Source: https://github.com/optimalbits/bull/blob/develop/docs/README.md Create a new queue instance by providing a name. ```javascript const myFirstQueue = new Bull('my-first-queue'); ``` -------------------------------- ### Retrieve Waiting Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets an array of jobs currently in the waiting state. You can specify a start and end index to retrieve a subset of jobs. ```javascript // Get first 10 waiting jobs const jobs = await queue.getWaiting(0, 9); console.log(`Found ${jobs.length} waiting jobs`); ``` ```javascript // Get all waiting jobs const allWaiting = await queue.getWaiting(0, -1); ``` -------------------------------- ### Get Specific Job Count Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Retrieves the count for a single job state. Examples include completed, failed, delayed, waiting, paused, and active. ```javascript const completedCount = await queue.getCompletedCount(); ``` ```javascript const failedCount = await queue.getFailedCount(); ``` ```javascript const delayedCount = await queue.getDelayedCount(); ``` ```javascript const waitingCount = await queue.getWaitingCount(); ``` ```javascript const pausedCount = await queue.getPausedCount(); ``` ```javascript const activeCount = await queue.getActiveCount(); ``` -------------------------------- ### Queue Constructor with Options Object Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Configure the queue using an options object, including Redis connection and settings. ```javascript new Queue('queueName', { redis: { host: 'localhost', port: 6379 }, settings: { ... }, limiter: { ... } }); ``` -------------------------------- ### Add Repeatable Job Every N Hours Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/repeating-jobs.md Configures a job to repeat at a fixed interval specified in milliseconds, equivalent to running every 6 hours in this example. This is suitable for tasks requiring regular execution regardless of specific times. ```javascript // Every 6 hours await queue.add(data, { repeat: { every: 6 * 60 * 60 * 1000 } }); ``` -------------------------------- ### Exported Modules from Bull Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/INDEX.md Demonstrates how to import the main Queue class, the Job class for static methods, and utility functions from the Bull library. ```javascript const Queue = require('bull'); // Queue class const queue = new Queue('myqueue'); // Job class (for static methods) const { Job } = require('bull'); const job = await Job.fromId(queue, jobId); // Utilities const { utils } = require('bull'); const ready = await utils.isRedisReady(redisClient); ``` -------------------------------- ### Configure named processors and concurrency Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Demonstrates how concurrency stacks for named processors on a single queue versus using separate queues. ```js /*** * For each named processor, concurrency stacks up, so any of these three process functions * can run with a concurrency of 125. To avoid this behaviour you need to create an own queue * for each process function. */ const loadBalancerQueue = new Queue('loadbalancer'); loadBalancerQueue.process('requestProfile', 100, requestProfile); loadBalancerQueue.process('sendEmail', 25, sendEmail); loadBalancerQueue.process('sendInvitation', 0, sendInvite); const profileQueue = new Queue('profile'); // Max concurrency for requestProfile is 100 profileQueue.process('requestProfile', 100, requestProfile); const emailQueue = new Queue('email'); // Max concurrency for sendEmail is 25 emailQueue.process('sendEmail', 25, sendEmail); ``` -------------------------------- ### Process Jobs from Separate File Source: https://github.com/optimalbits/bull/blob/develop/README.md Demonstrates how to configure a queue to use a processor defined in a separate file, with options for concurrency and naming. ```javascript // Single process: queue.process('/path/to/my/processor.js'); // You can use concurrency as well: queue.process(5, '/path/to/my/processor.js'); // and named processors: queue.process('my processor', 5, '/path/to/my/processor.js'); ``` -------------------------------- ### Create a Bull Queue Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/INDEX.md Instantiate a new Bull queue. This is the primary entry point for managing jobs. Ensure you have Redis running. ```javascript const Queue = require('bull'); const queue = new Queue('myqueue'); ``` -------------------------------- ### Add Job with Daily Cron Repeat Example Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/types.md Example of adding a job that repeats daily at 9 AM using a cron expression. This is useful for scheduling recurring tasks. ```typescript // Daily at 9 AM const job = await queue.add(data, { repeat: { cron: '0 9 * * *' } }); ``` -------------------------------- ### Queue API Reference Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Documentation for the Queue class, including its constructor and over 40 methods for managing jobs and queues. ```APIDOC ## Queue Class ### Description The Queue class is the primary interface for interacting with Bull. It allows you to add jobs, process them, and listen for various events. ### Methods - **Queue(name, opts?)**: Constructor for the Queue class. - **add(job, opts?)**: Adds a new job to the queue. - **process(handler)**: Defines the job processing logic. - **on(event, listener)**: Listens for queue events. ### Configuration Objects - **QueueOptions**: Main configuration object for the Queue constructor (7 fields). - **AdvancedSettings**: Advanced settings for queue behavior (8 settings). - **JobOptions**: Options for adding jobs (11 options). - **RateLimiter**: Options for rate limiting jobs. - **BackoffOptions**: Options for job retry backoff strategies. - **RepeatOptions**: Options for scheduling repeating jobs. - **KeepJobsOptions**: Options for managing completed/failed jobs. - **MetricsOpts**: Options for enabling metrics. - **DebounceOptions**: Options for debouncing jobs. ### Events - **completed**: Emitted when a job successfully completes. - **failed**: Emitted when a job fails. - (10+ other event types documented) ``` -------------------------------- ### Queue Constructor with Redis URL Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Connect to Redis using a provided URL. ```javascript new Queue('queueName', 'redis://localhost:6379'); ``` -------------------------------- ### Queue Constructor Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Instantiates a new Bull queue. You can provide the queue name and optional configuration settings. ```APIDOC ## Queue Constructor ### Description Instantiates a new Bull queue. You can provide the queue name and optional configuration settings. ### Method `new Queue(name, opts?)` or `new Queue(name, url, opts?)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript new Queue('my-queue', { redis: { host: 'localhost', port: 6379 } }) ``` ### Response None ### Key options: `redis`, `settings`, `limiter`, `defaultJobOptions`, `prefix`, `metrics` ``` -------------------------------- ### Queue Constructor Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Creates a new Queue instance. Multiple instances with the same name will share the same jobs in Redis. The queue is not immediately ready; use `isReady()` to wait for the Redis connection and Lua script loading. ```APIDOC ## Queue Constructor Creates a new Queue instance. ### Signature ```javascript new Queue(name: string, opts?: QueueOptions): Queue new Queue(name: string, url: string, opts?: QueueOptions): Queue ``` ### Parameters #### Path Parameters * **name** (string) - Required - Name of the queue * **url** (string) - Optional - Redis connection URL (e.g., `redis://localhost:6379`) * **opts** (QueueOptions) - Optional - Configuration options for the queue ### Returns A new Queue instance that extends EventEmitter. The queue is not immediately ready; use `isReady()` to wait for the Redis connection and Lua script loading. ### Example ```javascript const Queue = require('bull'); // Simple instantiation const myQueue = new Queue('myqueue'); // With Redis URL const myQueue = new Queue('myqueue', 'redis://localhost:6379'); // With options const myQueue = new Queue('myqueue', { redis: { host: 'localhost', port: 6379 }, settings: { lockDuration: 30000, maxStalledCount: 2 } }); // Wait for queue to be ready await myQueue.isReady(); ``` ``` -------------------------------- ### getPausedCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of paused jobs. ```APIDOC ## getPausedCount ### Description Gets the number of paused jobs. ### Method `getPausedCount(): Promise` ### Returns Promise - count of paused jobs. ``` -------------------------------- ### Handle Unknown Backoff Strategy Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/errors.md Demonstrates the incorrect way to specify a backoff strategy, leading to an 'Unknown backoff strategy' error. It then shows the correct method by defining custom strategies during queue creation. ```javascript // WRONG - Unknown strategy const job = await queue.add(data, { attempts: 3, backoff: { type: 'custom-exponential' // Not defined } }); // Error: Unknown backoff strategy // CORRECT - Define custom strategy const queue = new Queue('myqueue', { settings: { backoffStrategies: { 'custom-exponential': (attempts) => attempts * 2000 } } }); const job = await queue.add(data, { attempts: 3, backoff: { type: 'custom-exponential' // Now defined } }); ``` -------------------------------- ### getWaitingCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of waiting jobs. ```APIDOC ## getWaitingCount ### Description Gets the number of waiting jobs. ### Method `getWaitingCount(): Promise` ### Returns Promise - count of waiting jobs. ``` -------------------------------- ### getDelayedCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of delayed jobs. ```APIDOC ## getDelayedCount ### Description Gets the number of delayed jobs. ### Method `getDelayedCount(): Promise` ### Returns Promise - count of delayed jobs. ``` -------------------------------- ### getFailedCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of failed jobs. ```APIDOC ## getFailedCount ### Description Gets the number of failed jobs. ### Method `getFailedCount(): Promise` ### Returns Promise - count of failed jobs. ``` -------------------------------- ### Correct Redis Options for Bull Queue Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/errors.md Illustrates the correct way to configure Redis options when creating a Bull queue, avoiding incompatible settings like `enableReadyCheck: true` or `maxRetriesPerRequest` on subscriber clients. Bull manages these internally. ```javascript // WRONG const queue = new Queue('myqueue', { redis: { enableReadyCheck: true, // Bull sets this to false maxRetriesPerRequest: 1 } }); // CORRECT const queue = new Queue('myqueue', { redis: { host: 'localhost', port: 6379 // Bull handles enableReadyCheck and maxRetriesPerRequest } }); ``` -------------------------------- ### getCompletedCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of completed jobs. ```APIDOC ## getCompletedCount ### Description Gets the number of completed jobs. ### Method `getCompletedCount(): Promise` ### Returns Promise - count of completed jobs. ``` -------------------------------- ### GET /queue/isPaused Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Checks if the queue is currently paused. ```APIDOC ## GET /queue/isPaused ### Description Checks if the queue is paused. ### Method GET ### Parameters #### Query Parameters - **isLocal** (boolean) - Optional - Pass true if you need to know if this particular instance is paused. ### Response #### Success Response (200) - **isPaused** (boolean) - Returns true if the queue is paused. ``` -------------------------------- ### Queue Constructor Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Instantiate a new Bull queue. Options can be provided for Redis connection, settings, job limiting, default job options, prefix, and metrics. ```javascript new Queue(name, opts?) new Queue(name, url, opts?) ``` -------------------------------- ### getCountsPerPriority Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets job counts grouped by the specified priorities. ```APIDOC ## getCountsPerPriority ### Description Gets job counts grouped by the specified priorities. ### Method `getCountsPerPriority(priorities: number[]): Promise<{[index: string]: number}>` ### Parameters #### Path Parameters - **priorities** (number[]) - Required - Array of priority values to get counts for ### Returns Promise - object with priority as key and count as value. ``` -------------------------------- ### Configure Interval Repeating Job Options Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/repeating-jobs.md This snippet demonstrates advanced options for interval-based repeating jobs, including start/end dates, and iteration limits. ```javascript const job = await queue.add( data, { repeat: { every: 60000, startDate: new Date(), endDate: new Date('2025-12-31'), limit: 1000, count: 0 } } ); ``` -------------------------------- ### multi Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Returns a Redis pipeline for executing multiple commands atomically. ```APIDOC ## multi ### Description Returns a Redis pipeline for executing multiple commands atomically. ### Method `multi(): Redis.Pipeline` ### Returns Redis.Pipeline - a pipeline object. ``` -------------------------------- ### getRepeatableCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the number of repeatable jobs currently configured. ```APIDOC ## getRepeatableCount ### Description Gets the number of repeatable jobs currently configured. ### Method `getRepeatableCount(): Promise` ### Returns Promise - count of repeatable jobs. ``` -------------------------------- ### getActiveCount Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets the total number of active (processing) jobs. ```APIDOC ## getActiveCount ### Description Gets the number of active (processing) jobs. ### Method `getActiveCount(): Promise` ### Returns Promise - count of active jobs. ``` -------------------------------- ### Create Multiple Queues Source: https://github.com/optimalbits/bull/blob/develop/README.md Create separate queue instances for different entities to manage them independently. Each queue instance may require new Redis connections. ```javascript const userJohn = new Queue('john'); const userLisa = new Queue('lisa'); . ``` -------------------------------- ### Basic Job Processing Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/patterns.md Add a job to the queue, define a processor to handle it, and listen for completion events. ```javascript const queue = require('bull')('myqueue'); // Add job const job = await queue.add({ userId: 123, action: 'send-email', email: 'user@example.com' }); // Process job queue.process(async (job) => { const { userId, email } = job.data; await sendEmail(email); return { sent: true }; }); // Listen for completion queue.on('completed', (job, result) => { console.log(`Job ${job.id} completed:`, result); }); ``` -------------------------------- ### GET /queue/whenCurrentJobsFinished Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Waits for all currently processing jobs to finish. ```APIDOC ## GET /queue/whenCurrentJobsFinished ### Description Returns a promise that resolves when all jobs currently being processed by this worker have finished. ### Method GET ``` -------------------------------- ### getRepeatableJobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets information about repeatable jobs. Supports pagination and ordering. ```APIDOC ## getRepeatableJobs ### Description Gets information about repeatable jobs. Supports pagination and ordering. ### Method `getRepeatableJobs(start?: number, end?: number, asc?: boolean): Promise` ### Parameters #### Path Parameters - **start** (number) - Optional - Start index, defaults to 0 - **end** (number) - Optional - End index, defaults to -1 - **asc** (boolean) - Optional - If true, return in ascending order, defaults to false ### Returns Promise - array of repeatable job information objects. ``` -------------------------------- ### Job API Reference Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Documentation for the Job class, including its constructor and over 20 methods for interacting with individual jobs. ```APIDOC ## Job Class ### Description The Job class represents a single job within the Bull queue. It provides methods to interact with the job's state, data, and results. ### Methods - **Job(data, opts?)**: Constructor for the Job class. - **fromId(id)**: Static method to retrieve a job by its ID. - (20+ other methods documented for job manipulation and retrieval) ``` -------------------------------- ### Queue#add Method Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Creates a new job and adds it to the queue. If the queue is empty, the job is executed immediately. Otherwise, it's added to the queue for execution. ```APIDOC ## POST /queue/{queueName}/jobs ### Description Adds a new job to the specified queue. Jobs can be executed immediately if the queue is idle or queued for later execution. ### Method POST ### Endpoint `/queue/{queueName}/jobs` ### Parameters #### Path Parameters - **queueName** (string) - Required - The name of the queue to add the job to. #### Request Body - **name** (string) - Optional - A name for the job, allowing specific processors to handle it. - **data** (object) - Required - The payload of the job. - **opts** (JobOpts) - Optional - Options for configuring the job's behavior. ### Request Example ```json { "name": "processUserData", "data": { "userId": 123, "email": "test@example.com" }, "opts": { "priority": 1, "delay": 5000, "attempts": 3, "removeOnComplete": true } } ``` ### Response #### Success Response (200) - **jobId** (string) - The unique identifier for the created job. #### Response Example ```json { "jobId": "123e4567-e89b-12d3-a456-426614174000" } ``` ``` -------------------------------- ### Get Waiting Job Count Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns the number of waiting jobs in the queue. ```typescript getWaitingCount() : Promise ``` -------------------------------- ### Listen to Queue Events Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/INDEX.md Set up event listeners for job lifecycle events like completion or failure. This allows for real-time monitoring and handling of job outcomes. ```javascript queue.on('completed', (job, result) => { console.log('Job completed:', result); }); queue.on('failed', (job, error) => { console.error('Job failed:', error); }); ``` -------------------------------- ### Get Active Job Count Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns the number of active jobs in the queue. ```typescript getActiveCount() : Promise ``` -------------------------------- ### Resource-Constrained Environment Configuration Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Configure Bull for environments with limited resources by disabling stalled checking, reducing guard interval frequency, and setting a drain delay. This also includes default job options to automatically remove completed or failed jobs. ```javascript { settings: { stalledInterval: 0, guardInterval: 30000, drainDelay: 30 }, defaultJobOptions: { removeOnComplete: true, removeOnFail: true } } ``` -------------------------------- ### Get Delayed Job Count Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns the number of delayed jobs in the queue. ```typescript getDelayedCount() : Promise ``` -------------------------------- ### Get Failed Job Count Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns the number of failed jobs in the queue. ```typescript getFailedCount() : Promise ``` -------------------------------- ### Timeout Implementation Notes Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Information on how job timeouts are handled and suggestions for implementing proactive job stopping. ```APIDOC ### Timeout Handling Jobs are marked as failed when they exceed their `timeout` duration, but Bull does not proactively stop the processor function. To ensure jobs stop processing after timing out, consider these strategies: 1. **Periodic Status Check**: Have the job periodically check its status using `job.getStatus()` and exit if the status is `'failed'`. 2. **Cancelable Promises**: Implement jobs as cancelable promises. If the processor's promise has a `cancel()` method, it will be called upon timeout, allowing the job to respond. 3. **External Stopping**: If you have an external mechanism to stop jobs, listen for the `'failed'` event and trigger the stop there. ``` -------------------------------- ### Redis Connection via Environment Variables (JS) Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Connect to Redis using environment variables (e.g., REDIS_HOST, REDIS_PORT, REDIS_PASSWORD) which are automatically picked up by the ioredis client. An empty `redis: {}` configuration is sufficient. ```javascript const queue = new Queue('myqueue', { redis: {} // Picks up REDIS_* environment variables via ioredis }); ``` -------------------------------- ### Get Completed Job Count Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns the number of completed jobs in the queue. ```typescript getCompletedCount() : Promise ``` -------------------------------- ### Common Methods Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Provides a list of common methods for managing queues and jobs, including retrieving jobs, controlling processing, and cleaning up old jobs. ```APIDOC ## Common Methods ### Description Provides a list of common methods for managing queues and jobs, including retrieving jobs, controlling processing, and cleaning up old jobs. ### Method - `getJob(id)` - `getJobCounts()` - `getWaiting()` - `getActive()` - `getCompleted()` - `getFailed()` - `pause()` - `resume()` - `clean()` - `close()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript // Get a job by ID const job = await queue.getJob('job-id'); // Get job counts const counts = await queue.getJobCounts(); // Pause processing await queue.pause(); // Clean up old jobs await queue.clean(1000 * 60 * 60); // Remove jobs older than 1 hour ``` ### Response None ### Purpose: - `getJob()`: Get job by ID - `getJobCounts()`: Count jobs by state - `getWaiting()`, `getActive()`, `getCompleted()`, `getFailed()`: Get jobs in state - `pause()`, `resume()`: Control processing - `clean()`: Remove old jobs - `close()`: Graceful shutdown ``` -------------------------------- ### getMetrics Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Gets metrics about completed or failed jobs within a specified time range. ```APIDOC ## getMetrics ### Description Gets metrics about completed or failed jobs within a specified time range. ### Method `getMetrics(type: 'completed' | 'failed', start?: number, end?: number): Promise` ### Parameters #### Path Parameters - **type** (string) - Required - 'completed' or 'failed' - **start** (number) - Optional - Start point (0 is newest), defaults to 0 - **end** (number) - Optional - End point (-1 is oldest), defaults to -1 ### Returns Promise - object with meta, data array, and count. ``` -------------------------------- ### getJobLogs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Retrieves logs for a specific job, with optional start and end indices for log retrieval. ```APIDOC ## getJobLogs ### Description Retrieves logs for a specific job. Supports pagination for log retrieval. ### Method `getJobLogs(jobId: JobId, start?: number, end?: number): Promise<{logs: string[], count: number}>` ### Parameters #### Path Parameters - **jobId** (JobId) - Required - The ID of the job to retrieve logs for. #### Query Parameters - **start** (number) - Optional - Start index for log retrieval. - **end** (number) - Optional - End index for log retrieval. ### Returns Promise<{logs: string[], count: number}> - An object containing an array of log strings and the total count of logs. ``` -------------------------------- ### POST /queue/addBulk Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Creates an array of jobs and adds them to the queue. ```APIDOC ## POST /queue/addBulk ### Description Creates an array of jobs and adds them to the queue. They follow the same signature as Queue#add. ### Method POST ### Parameters #### Request Body - **jobs** (Array) - Required - An array of objects containing name, data, and opts for each job. ``` -------------------------------- ### Add a Repeating Job Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/README.md Shows how to schedule a job to repeat using a cron syntax, specifying the time and timezone for execution. ```javascript // Every weekday at 9 AM await queue.add( { report: 'daily' }, { repeat: { cron: '0 9 * * 1-5', tz: 'America/New_York' } } ); ``` -------------------------------- ### Get Metrics Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Retrieves metrics for completed or failed jobs within a specified time range. ```typescript getMetrics(type: 'completed' | 'failed', start = 0, end = -1) : Promise<{ meta: { count: number; prevTS: number; prevCount: number; }; data: number[]; count: number; }> ``` -------------------------------- ### Get Job Logs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Retrieves logs for a specific job, including the total count of log entries. ```javascript const { logs, count } = await queue.getJobLogs('job-123'); console.log(`Job has ${count} total log entries`); logs.forEach(log => console.log(log)); ``` -------------------------------- ### Listen to local and global completion events Source: https://github.com/optimalbits/bull/blob/develop/docs/README.md Demonstrates the difference between local event listeners and global event listeners, noting that global events provide only the job ID for performance. ```javascript queue.on('completed', job => { console.log(`Job with id ${job.id} has been completed`); }) ``` ```javascript queue.on('global:completed', jobId => { console.log(`Job with id ${jobId} has been completed`); }) ``` -------------------------------- ### Add Jobs with Repeat Options Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Demonstrates adding jobs with repeat configurations. Note that jobs with identical `jobId` and `repeat` options are not created, but jobs with different repeat configurations or no repeat options are allowed. ```typescript await queue.add({}, { jobId: 'example', repeat: { every: 5 * 1000 } }) await queue.add({}, { jobId: 'example', repeat: { every: 5 * 1000 } }) // Will not be created, same repeat configuration await queue.add({}, { jobId: 'example', repeat: { every: 10 * 1000 } }) // Will be created, different repeat configuration await queue.add({}, { jobId: 'example' }) // Will be created, no regular job with this id await queue.add({}, { jobId: 'example' }) // Will not be created, conflicts with previous regular job ``` -------------------------------- ### Retrieve Completed Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Fetches an array of jobs that have finished successfully. Supports start and end index parameters. ```javascript const completedJobs = await queue.getCompleted(); ``` -------------------------------- ### Configure Redis Cluster Source: https://github.com/optimalbits/bull/blob/develop/PATTERNS.md Enable Redis cluster compatibility by defining a queue prefix within brackets to act as a hash tag. ```javascript const queue = new Queue('cluster', { prefix: '{myprefix}' }); ``` -------------------------------- ### Retrieve Active Jobs Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Fetches an array of jobs that are currently being processed. Optionally specify start and end indices. ```javascript const activeJobs = await queue.getActive(); console.log(`${activeJobs.length} jobs currently processing`); ``` -------------------------------- ### Get Waiting Job Count Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/api-reference/queue.md Retrieves the number of jobs currently in the waiting state, excluding active jobs. ```javascript const numWaiting = await queue.count(); console.log(`${numWaiting} jobs waiting`); ``` -------------------------------- ### Minimal Queue Constructor Source: https://github.com/optimalbits/bull/blob/develop/_autodocs/configuration.md Instantiate a new queue with just a name. ```javascript new Queue('queueName'); ``` -------------------------------- ### Get Job Lock Key with Job#lockKey Source: https://github.com/optimalbits/bull/blob/develop/REFERENCE.md Returns a unique string key that represents the lock for this specific job. ```typescript lockKey(): string ```