3.2.23 • Published 2 years ago

@anchan828/nest-bull v3.2.23

Weekly downloads
252
License
MIT
Repository
github
Last release
2 years 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.

3.2.23

2 years ago

3.2.22

2 years ago

3.2.21

2 years ago

3.2.20

2 years ago

3.2.19

2 years ago

3.2.18

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.2.9

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.2.13

2 years ago

3.2.12

2 years ago

3.2.15

2 years ago

3.2.14

2 years ago

3.2.17

2 years ago

3.2.16

2 years ago

3.2.11

2 years ago

3.2.10

2 years ago

3.2.0

2 years ago

3.1.30

2 years ago

3.1.29

2 years ago

3.1.28

2 years ago

3.1.25

2 years ago

3.1.27

2 years ago

3.1.26

2 years ago

3.1.24

2 years ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.18

2 years ago

3.1.17

2 years ago

3.1.23

2 years ago

3.1.22

2 years ago

3.1.21

2 years ago

3.1.20

2 years ago

3.1.19

2 years ago

3.1.12

2 years ago

3.1.11

3 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.10

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.1.7

3 years ago

3.1.6

3 years ago

3.1.5

3 years ago

3.1.4

3 years ago

3.1.9

3 years ago

3.1.8

3 years ago

3.0.4

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

1.2.8

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.12

3 years ago

1.2.13

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.14

3 years ago

1.2.15

3 years ago

1.2.9

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.22

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

4 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.33

4 years ago

0.5.32

4 years ago

0.5.31

4 years ago

0.5.30

4 years ago

0.5.29

4 years ago

0.5.28

4 years ago

0.5.27

4 years ago

0.5.26

4 years ago

0.5.25

4 years ago

0.5.23

4 years ago

0.5.24

4 years ago

0.5.22

4 years ago

0.5.21

4 years ago

0.5.20

4 years ago

0.5.19

4 years ago

0.5.18

4 years ago

0.5.17

4 years ago

0.5.16

4 years ago

0.5.15

4 years ago

0.5.14

4 years ago

0.5.13

4 years ago

0.5.12

4 years ago

0.5.11

4 years ago

0.5.10

4 years ago

0.5.9

4 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.0

5 years ago

0.5.2

5 years ago

0.4.18

5 years ago

0.4.17

5 years ago

0.4.16

5 years ago

0.4.15

5 years ago

0.4.14

5 years ago

0.4.13

5 years ago

0.4.12

5 years ago

0.4.11

5 years ago

0.4.10

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.40

6 years ago

0.3.38

6 years ago

0.3.37

6 years ago

0.3.35

6 years ago

0.3.34

6 years ago

0.3.33

6 years ago

0.3.32

6 years ago

0.3.31

6 years ago

0.3.30

6 years ago

0.3.29

6 years ago

0.3.28

6 years ago

0.3.27

6 years ago

0.3.26

6 years ago

0.3.25

6 years ago

0.3.24

6 years ago

0.3.23

6 years ago

0.3.22

6 years ago

0.3.20

6 years ago

0.3.19

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.16

6 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0-rc1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1-rc3

7 years ago

0.1.1-rc2

7 years ago

0.1.1-rc1

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1-rc10

7 years ago

0.0.1-rc9

7 years ago

0.0.1-rc8

7 years ago

0.0.1-rc7

7 years ago

0.0.1-rc6

7 years ago

0.0.1-rc5

7 years ago

0.0.1-rc4

7 years ago

0.0.1-rc3

7 years ago

0.0.1-rc2

7 years ago

0.0.1-rc1

7 years ago

0.0.1

7 years ago