Code Monkey home page Code Monkey logo

bullmq_dynamic-queue_handling's Introduction

README

Follow this medium blog for detailed understanding about how bullMQ works.

This is a simple backend implementation of message queue and it's powerful features. I am using bullMQ, which is a nodejs library built on top of redis.

The application will have

  • 3+2 queues
  • 3 producers
  • 3+2 workers

Each producer will generate a new task and add it in one of the queues every 3 seconds, the workers will pick a task from the queues, take a random time between 5-10s, which will be an estimated time taken.

Each worker can listen to one queue only.

If the random number is greater than 8, we will assume that it is some kind of error, and it will be pushed into a error_recovery queue. One worker will work on the error_recovery queue, and following the same random number generation logic, it will perform the task, this queue will have a 20% chance of success, and the rest 80% will be stashed into a dead_queue.

Installation

    # basic setup
    npm init
    tsc --init
    npm i bullmq ioredis
  • Running redis and redis-cli on docker
    docker run --name redis_container -p 6379:6379 redis
    docker exec -it <redis_container_id> redis-cli

Facts

I found out that,

    You can reuse a processor that goes into the worker across different queues but not the worker itself.
  • Whenever a worker picks a job, it locks it to prevent any other worker from accessing it and till now there is no way to bypass it.

So, I will be utilizing the concept of processor and allotting them to workers depending on job.queueName.

Workers

A worker is instantiated with the Worker class, and the work itself will be performed in the process function. Process functions are meant to be asynchronous, using either the async keyword or returning a promise.

    const worker = new Worker('queue_name', processor, connection);

Job Lifecycle

Jobs in BullMQ go through various states in their lifecycle:

  • waiting: Added to queue, waiting to be processed
  • delayed: Job is scheduled for a later time
  • active: Being processed by worker
  • completed: Successfully processed
  • failed: Failed processing due to error
  • delayedRetry: Failed job is waiting for retry after delay
  • paused: Queue/job paused and not active

Job Options

While adding jobs, we can pass job options to control lifecycle behavior:

    await queue.add(jobName, data, options)

Some common options are:

  • delay: Delay job by x ms before processing
  • attempts: Number of times to retry a failed job (3 is default)
  • backoff: Backoff strategy on job failure
  • lifo: Use LIFO order instead of FIFO
  • priority: Numeric priority value. Higher is processed sooner
  • repeat: Repeat job on a cron schedule

Job Events

Each job emits events during its lifecycle that we can listen to:

Common job events:

  • waiting
  • active
  • stalled (job touched but not done)
  • progress (progress updated)
  • completed
  • failed
  • paused
  • resumed
  • removed
    job.on('completed', () => {
        // Job completed
    })

    job.on('failed', (err) => {
        // Job failed with error
    })

bullmq_dynamic-queue_handling's People

Contributors

keshavjha2002 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.