nestjs-kue
TypeScript icon, indicating that this package has built-in type declarations

0.4.3 • Public • Published

Kue wrapper for NestJS framework

Description

Kue is a priority job queue backed by redis, built for nodeJS.

This library provide facilities and utilities to use Kue with NestJS.

Installation

$ npm install --save nestjs-kue

This module uses REDIS to operate, and utilizes the following environment variables and the default values as configurations:

  • KUE_REDIS_PREFIX # Default 'q'
  • KUE_REDIS_HOST # Default 'localhost'
  • KUE_REDIS_PORT # Default 6379
  • KUE_REDIS_DB # Default 0

As of 0.2.0 version, you are able to use connection URI using KUE_REDIS_URI variable like:

redis://example.com:1234?redis_option=value&redis_option=value

Usage

Defining tasks:

Tasks are defined in files like:

src/modules/users/tasks/users.tasks.ts

You can define multiple tasks as a single injectable:

import { Injectable } from '@nestjs/common';
import { Job, JobCallback, DoneCallback } from 'kue';
import { Task } from 'nestjs-kue';
 
@Injectable()
export class UsersTasks {
    @Task({ name: 'justATest' })
    justATest(job: Job, done: DoneCallback) {
        const result: string = 'Ended just fine!';
        done(null, result);
    }
}

Options when defining a task:

@Task({
    name: 'justATest',
    concurrency: 3,
    attempts: 3,
    ttl: 3000,
    backoff: { delay: 5 * 1000, type: 'fixed' }
})

To setup the module, include KueModule and the KueTaskRegisterService in modules where you will use tasks, then register the tasks using the method register():

import { ModuleRef } from '@nestjs/core';
import { KueModule, KueTaskRegisterService } from 'nestjs-kue';
import { UsersTasks } from './tasks/users.tasks';
 
@Module({
  imports: [KueModule],
  controllers: [UsersController],
  providers: [UsersTasks],
})
export class UsersModule implements OnModuleInit {
    constructor(
        private readonly moduleRef: ModuleRef,
        private readonly taskRegister: KueTaskRegisterService
    ) {}
 
    onModuleInit() {
        this.taskRegister.setModuleRef(this.moduleRef);
        this.taskRegister.register(UsersTasks);
    }
}

Firing a previously defined task:

Add the KueServive and the injectable with the task on your controller

import { Get, Controller } from '@nestjs/common';
import { UsersTasks } from './tasks/users.tasks';
import { KueService } from 'nestjs-kue';
 
@Controller()
export class AppController {
    constructor(
        private readonly kueService: KueService,
        private readonly tasks: UsersTasks
    ) {}
}

Firing the task with { a: 'b' } as argument:

@Get('task')
createTask() {
    const job = this.kueService.createJob(this.tasks.justATest, { a: 'b' }).save();
}

A task can emit some events when fired:

https://github.com/Automattic/kue#job-events

@Get('task')
createJob(@Res() res) {
    const job = this.kueService.createJob(this.tasks.justATest, { a: 'b' }).save();
    job.on('complete', (result) => res.send(result));
    job.on('failed', (err) => res.status(HttpStatus.INTERNAL_SERVER_ERROR).send(err));
}

For more options and details, please check Kue docs

Kue

Debug

You can enable some debug logs with KUE_DEBUG environment variable:

KUE_DEBUG=true

Kue daemon autostart

By default Kue daemon will start when you register one task. If you want to disable Kue Daemon on any process/container, set the KUE_START_PROCESSING environment variable:

KUE_START_PROCESSING=false

Redis sentinel configuration

This module support redis sentinel, you need to setup env variable like this

# SENTINEL CONFIG
KUE_REDIS_SENTINEL=true
KUE_REDIS_SENTINEL_HOST=redis-sentinel-0,redis-sentinel-1,redis-sentinel-2 // NAME OF SENTINEL IN YOUR CLUSTER
KUE_REDIS_SENTINEL_PORT=26379,26379,26379 // PORT OF EACH SENTINEL IN YOUR CLUSTER
KUE_REDIS_SENTINEL_MASTER=name-of-master

You can set the KUE_REDIS_SENTINEL at false for disabled redis sentinel

Datadog integration

From version 0.3.0 and above, there are the ability to send execution stats to datadog monitoring service: Datadog

To setup datadog, all you need to do is call the method setDDTracer(tracer) on taskRegister service with your Datadog tracer object as the parameter.

this.taskRegister.setDDTracer(tracer);

Kue UI

It's possible to view info about tasks being executed with the default Kue UI

To enable it, set KUE_UI_ENABLED environment variable to true:

WARNING: The UI will be deployed using express!

KUE_UI_ENABLED=true 

The default UI port is 3050, but if you like to change it use KUE_UI_PORT environment variable:

KUE_UI_PORT=3050

People

Readme

Keywords

none

Package Sidebar

Install

npm i nestjs-kue

Weekly Downloads

131

Version

0.4.3

License

MIT

Unpacked Size

26.5 kB

Total Files

20

Last publish

Collaborators

  • eventflit