0.0.1 • Published 6 months ago

nestjs-pueue v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Description

This library uses PostgreSQL’s SKIP LOCKED feature to handle concurrent jobs without blocking. Each worker process obtains pending jobs with an optimistic row lock using FOR UPDATE SKIP LOCKED, so multiple workers can process jobs simultaneously. Once a job is taken, its status is updated within a transaction, ensuring data integrity and preventing conflicts. By configuring concurrency and batch size, you can scale queue processing effectively and avoid transactional deadlocks.

Installation

npm install nestjs-pueue

Configuration

Import the PueueModule in your main module:

import { Module } from '@nestjs/common'
import { PueueModule } from 'nestjs-pueue'

@Module({
    imports: [
        PueueModule.forRoot({
            type: 'postgres',
            host: 'localhost',
            port: 5432,
            username: 'postgres',
            password: 'postgres',
            database: 'pueue',
            // ...other TypeORM options...
        }),
    ],
})
export class AppModule {}

Usage

  • Add jobs to the queue:
import { Injectable } from '@nestjs/common'
import { Pueue } from 'nestjs-pueue'
export class EmailService {
    constructor(private readonly pueue: Pueue) {}

    async enqueueEmail() {
        await this.pueue.add('send-email', {
            data: { recipient: '...' },
        })
    }
}
  • Create processes with the @Process decorator:
import { Injectable } from '@nestjs/common'
import { Processor, Process } from 'nestjs-pueue'

@Processor()
@Injectable()
export class EmailProcessor {
    @Process('send-email')
    async sendEmail(job: Job) {
        // send email
    }
}
0.0.1

6 months ago