Skip to content

Repository files navigation

Social Card of this repo

npm versionGitHub ActionsCommitizen friendly

bun-queue

A Redis-backed job queue built for Bun, inspired by Laravel's Queue system and BullMQ.

Features

  • Fast and efficient Redis-backed queue system
  • Support for delayed jobs, retries, and prioritization
  • Rate limiting capabilities
  • Job event tracking
  • Reliable job processing with concurrency control
  • Typesafe API

Installation

bun add bun-queue

Basic Usage

import{Queue}from'bun-queue'// Create a queueconstemailQueue=newQueue('emails')// Add a job to the queueconstjob=awaitemailQueue.add({to: 'user@example.com',subject: 'Welcome',body: 'Welcome to our platform!'})console.log(`Job ${job.id} added to the queue`)// Process jobsemailQueue.process(5,async(job)=>{const{ to, subject, body }=job.data// Update progressawaitjob.updateProgress(10)// Simulate sending emailconsole.log(`Sending email to ${to} with subject: ${subject}`)awaitnewPromise(resolve=>setTimeout(resolve,1000))awaitjob.updateProgress(100)return{sent: true,timestamp: Date.now()}})

Job Options

// Add a job with optionsawaitqueue.add({task: 'process-pdf',url: 'https://example.com/document.pdf'},{delay: 5000,// delay for 5 secondsattempts: 3,// retry up to 3 timesbackoff: {type: 'exponential',// 'fixed' or 'exponential'delay: 1000,// milliseconds},priority: 10,// higher number = higher priorityremoveOnComplete: true,// remove job when completedlifo: false,// process in FIFO order (default)jobId: 'custom-id',// provide custom job ID})

Delayed Jobs

// Add a job that will be processed after 30 secondsawaitqueue.add({task: 'send-reminder'},{delay: 30000})

Job Management

// Get a job by IDconstjob=awaitqueue.getJob('job-id')// Get jobs by statusconstwaitingJobs=awaitqueue.getJobs('waiting')constactiveJobs=awaitqueue.getJobs('active')constcompletedJobs=awaitqueue.getJobs('completed')constfailedJobs=awaitqueue.getJobs('failed')// Get job countsconstcounts=awaitqueue.getJobCounts()console.log(counts)// { waiting: 5, active: 2, completed: 10, failed: 1, delayed: 3, paused: 0 }// Pause a queueawaitqueue.pause()// Resume a queueawaitqueue.resume()// Retry a failed jobconstfailedJob=awaitqueue.getJob('failed-job-id')awaitfailedJob.retry()// Remove a jobawaitjob.remove()// Clear all jobsawaitqueue.empty()

Rate Limiting

import{Queue,RateLimiter}from'bun-queue'constqueue=newQueue('api-calls')// Create a rate limiter (100 jobs per minute)constlimiter=newRateLimiter(queue,{max: 100,duration: 60000})// Check if rate limited before adding jobconst{ limited, remaining, resetIn }=awaitlimiter.check()if(!limited){awaitqueue.add({url: 'https://api.example.com/endpoint'})console.log(`Job added. ${remaining} requests remaining.`)}else{console.log(`Rate limited. Try again in ${resetIn}ms.`)}

Configuration

import{Queue}from'bun-queue'// Configure queue with optionsconstqueue=newQueue('tasks',{redis: {url: 'redis://username:password@localhost:6379'// Or provide your own client// client: myRedisClient},prefix: 'myapp',// prefix for Redis keys (default: 'queue')defaultJobOptions: {attempts: 5,backoff: {type: 'exponential',delay: 5000}}})

Environment Variables

The library reads these environment variables (in order of precedence):

  • REDIS_URL: Redis connection string
  • Default is redis://localhost:6379 if not set

Testing

bun test

Changelog

Please see our releases page for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware

"Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.

Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎

Sponsors

We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.

License

The MIT License (MIT). Please see LICENSE for more information.

Made with 💙

About

A modern & powerful Queue / Messaging system. Optimized for Bun usage. UI coming soon.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

67 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages