1.2.7 • Published 12 days ago

@anchan828/nest-bull v1.2.7

Weekly downloads
252
License
MIT
Repository
github
Last release
12 days ago

@anchan828/nest-bull

npm NPM

Description

The Bull module for Nest.

Installation

$ npm i --save @anchan828/nest-bull bull
$ npm i --save-dev @types/bull

Quick Start

Importing BullModule and Queue component

import { BullModule } from "@anchan828/nest-bull";
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppQueue } from "./app.queue";
import { AppService } from "./app.service";

@Module({
  imports: [
    BullModule.forRoot({
      queues: [__dirname + "/**/*.queue{.ts,.js}"],
      options: {
        redis: {
          host: "127.0.0.1",
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService, AppQueue],
})
export class AppModule {}

Creating queue class

import { BullQueue, BullQueueProcess } from "@anchan828/nest-bull";
import { Job } from "bull";
import { APP_QUEUE } from "./app.constants";
import { AppService } from "./app.service";

@BullQueue({ name: APP_QUEUE })
export class AppQueue {
  constructor(private readonly service: AppService) {}

  @BullQueueProcess()
  public async process(job: Job) {
    console.log("called process", job.data, this.service.root());
  }
}

Adding job

import { Controller, Get, Inject } from "@nestjs/common";
import { JobId, Queue } from "bull";
import { APP_QUEUE } from "./app.constants";
import { BullQueueInject } from "@anchan828/nest-bull";

@Controller()
export class AppController {
  constructor(
    @BullQueueInject(APP_QUEUE)
    private readonly queue: Queue,
  ) {}

  @Get()
  async root(): Promise<JobId> {
    const job = await this.queue.add({ text: "text" });
    return job.id;
  }
}

Override queue settings per queue

@BullQueue({
  name: APP_QUEUE,
  options: {
    redis: {
      db: 3,
    },
  },
})
export class AppQueue {
  // queue.add('processorName1', data);
  @BullQueueProcess({
    name: "processorName1",
    concurrency: 3,
  })
  async process1(job: Job) {
    throw new Error(`throw error ${JSON.stringify(job.data)}`);
  }

  // queue.add('processorName2', data);
  @BullQueueProcess({
    name: "processorName2",
  })
  async process2(job: Job) {
    throw new Error(`throw error ${JSON.stringify(job.data)}`);
  }
}

Handling events

@BullQueue({ name: APP_QUEUE })
export class AppQueue {
  constructor(private readonly service: AppService) {}

  @BullQueueProcess()
  public async process(job: Job) {
    console.log("called process", job.data, this.service.root());
  }

  @BullQueueEventProgress()
  public async progress(job: Job, progress: number) {
    console.log("progress", job.id, progress);
  }

  @BullQueueEventCompleted()
  public async completed(job: Job, result: any) {
    console.log("completed", job.id, result);
  }

  @BullQueueEventFailed()
  public async failed(job: Job, error: Error) {
    console.error("failed", job.id, error);
  }
}

Getting Queue using BullService

import { Controller, Get, Inject } from "@nestjs/common";
import { JobId, Queue } from "bull";
import { APP_QUEUE } from "./app.constants";
import { BullService, BULL_MODULE_SERVICE } from "@anchan828/nest-bull";

@Controller()
export class AppController {
  constructor(
    @Inject(BULL_MODULE_SERVICE)
    private readonly service: BullService,
  ) {}

  @Get()
  async root(): Promise<JobId> {
    const job = await this.service.getQueue(APP_QUEUE).add({ text: "text" });
    return job.id;
  }
}

forRootAsync

This package supports forRootAsync. However, you can only BullService if you want to forRootAsync.

More examples...

See example app: https://github.com/anchan828/nest-bull-example

And more: https://github.com/anchan828/nest-bull/tree/master/src/examples

Extra

There are extra options.

export interface BullQueueExtraOptions {
  defaultProcessorOptions?: {
    /**
     * Bull will then call your handler in parallel respecting this maximum value.
     */
    concurrency?: number;

    /**
     * Skip call this processor if true.
     */
    skip?: boolean;
  };

  defaultJobOptions?: {
    /**
     * Set TTL when job in the completed. (Default: -1)
     */
    setTTLOnComplete?: number;
    /**
     * Set TTL when job in the failed. (Default: -1)
     */
    setTTLOnFail?: number;
  };
}

You can set options to module and per queue.

@Module({
  imports: [
    BullModule.forRoot({
      queues: [__dirname + "/**/*.queue{.ts,.js}"],
      options: {
        redis: {
          host: "127.0.0.1",
        },
      },
      extra: {
        defaultProcessorOptions: {
          concurrency: 3,
        },
        defaultJobOptions: {
          setTTLOnComplete: 30,
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService, AppQueue],
})
export class AppModule {}
@BullQueue({
  name: APP_QUEUE,
  extra: {
    defaultJobOptions: {
      setTTLOnComplete: 300,
    },
  },
})
export class AppQueue {
  @BullQueueProcess()
  public async process(job: Job) {
    return Promise.resolve();
  }
}

Testing

Example for TestingModule

Set mock: true if you don't want to create Queue instance. BullModule create mock instance instead of Queue.

@Module({
  imports: [
    BullModule.forRoot({
      queues: [__filename],
      mock: true,
    }),
  ],
})
export class ApplicationModule {}

Or you can use createTestBullProvider

import { BullQueueInject } from "@anchan828/nest-bull";

@Injectable()
export class Service {
  constructor(
    @BullQueueInject("Queue name")
    private readonly queue: Queue,
  ) {}

  public async someMethod() {
    await this.queue.add({ key: "value" });
  }
}
import { createTestBullProvider } from "@anchan828/nest-bull/dist/testing";
const app: TestingModule = await Test.createTestingModule({
  providers: [Service, createTestBullProvider("Queue name")],
}).compile();

License

MIT.

1.2.7

12 days ago

1.2.6

19 days ago

1.2.5

26 days ago

1.2.4

1 month ago

1.2.0

2 months ago

1.2.3

1 month ago

1.2.2

2 months ago

1.2.1

2 months ago

1.1.8

2 months ago

1.1.7

2 months ago

1.1.6

2 months ago

1.1.5

2 months ago

1.1.4

3 months ago

1.1.3

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.1.2

3 months ago

1.0.22

6 months ago

1.0.26

5 months ago

1.0.25

5 months ago

1.0.24

5 months ago

1.0.23

6 months ago

1.0.29

4 months ago

1.0.28

4 months ago

1.0.27

5 months ago

1.0.31

4 months ago

1.0.30

4 months ago

1.0.19

6 months ago

1.0.18

7 months ago

1.0.21

6 months ago

1.0.20

6 months ago

1.0.17

7 months ago

1.0.16

7 months ago

1.0.15

7 months ago

1.0.14

8 months ago

1.0.13

8 months ago

1.0.12

8 months ago

1.0.11

8 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.5.33

11 months ago

0.5.32

11 months ago

0.5.31

11 months ago

0.5.30

12 months ago

0.5.29

12 months ago

0.5.28

12 months ago

0.5.27

1 year ago

0.5.26

1 year ago

0.5.25

1 year ago

0.5.23

1 year ago

0.5.24

1 year ago

0.5.22

1 year ago

0.5.21

1 year ago

0.5.20

1 year ago

0.5.19

1 year ago

0.5.18

1 year ago

0.5.17

1 year ago

0.5.16

1 year ago

0.5.15

1 year ago

0.5.14

1 year ago

0.5.13

1 year ago

0.5.12

1 year ago

0.5.11

1 year ago

0.5.10

1 year ago

0.5.9

1 year ago

0.5.8

1 year ago

0.5.7

1 year ago

0.5.6

1 year ago

0.5.5

1 year ago

0.5.4

1 year ago

0.5.0

1 year ago

0.5.2

1 year ago

0.4.18

1 year ago

0.4.17

1 year ago

0.4.16

1 year ago

0.4.15

2 years ago

0.4.14

2 years ago

0.4.13

2 years ago

0.4.12

2 years ago

0.4.11

2 years ago

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.40

2 years ago

0.3.38

2 years ago

0.3.37

2 years ago

0.3.35

3 years ago

0.3.34

3 years ago

0.3.33

3 years ago

0.3.32

3 years ago

0.3.31

3 years ago

0.3.30

3 years ago

0.3.29

3 years ago

0.3.28

3 years ago

0.3.27

3 years ago

0.3.26

3 years ago

0.3.25

3 years ago

0.3.24

3 years ago

0.3.23

3 years ago

0.3.22

3 years ago

0.3.20

3 years ago

0.3.19

3 years ago

0.3.18

3 years ago

0.3.17

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.10

3 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0-rc1

3 years ago

0.3.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1-rc3

3 years ago

0.1.1-rc2

3 years ago

0.1.1-rc1

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1-rc10

3 years ago

0.0.1-rc9

3 years ago

0.0.1-rc8

3 years ago

0.0.1-rc7

3 years ago

0.0.1-rc6

3 years ago

0.0.1-rc5

3 years ago

0.0.1-rc4

3 years ago

0.0.1-rc3

3 years ago

0.0.1-rc2

3 years ago

0.0.1-rc1

3 years ago

0.0.1

3 years ago