@tsed/bullmq 7.49.0 → 7.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- import {FallbackJobController, JobController} from "./JobController";
2
- import {Store} from "@tsed/core";
3
- import {GlobalProviders} from "@tsed/di";
4
-
5
- describe("JobController", () => {
6
- afterEach(() => GlobalProviders.clear());
7
-
8
- it("should set the proper store data", () => {
9
- @JobController("testjob")
10
- class TestJob {}
11
-
12
- const store = Store.from(TestJob).get("bullmq");
13
-
14
- expect(store.name).toBe("testjob");
15
- expect(store.queue).toBe("default");
16
- expect(store.opts).toStrictEqual({});
17
- });
18
-
19
- it("should allow settings a custom queue", () => {
20
- @JobController("testjob", "custom")
21
- class CustomQueueJob {}
22
-
23
- const store = Store.from(CustomQueueJob).get("bullmq");
24
-
25
- expect(store.queue).toBe("custom");
26
- });
27
-
28
- it("should allow custom options to be set", () => {
29
- @JobController("testjob", "default", {
30
- backoff: 69,
31
- attempts: 42
32
- })
33
- class CustomOptionsJob {}
34
-
35
- const store = Store.from(CustomOptionsJob).get("bullmq");
36
-
37
- expect(store.opts).toStrictEqual({
38
- backoff: 69,
39
- attempts: 42
40
- });
41
- });
42
-
43
- it("should set type for cron jobs", () => {
44
- @JobController("testjob", "default", {
45
- repeat: {count: 42}
46
- })
47
- class CustomCronJob {}
48
-
49
- const store = Store.from(CustomCronJob).get("bullmq");
50
-
51
- expect(store);
52
- });
53
- });
54
-
55
- describe("FallbackJobController", () => {
56
- afterEach(() => GlobalProviders.clear());
57
-
58
- describe("with queue", () => {
59
- it("should set the proper store data", () => {
60
- @FallbackJobController("default")
61
- class FallbackJob {}
62
-
63
- const store = Store.from(FallbackJob).get("bullmq");
64
- expect(store.queue).toBe("default");
65
-
66
- const fallbackControllerForQueue = GlobalProviders.get("bullmq.fallback-job.default");
67
- expect(fallbackControllerForQueue).not.toBeUndefined();
68
- expect(fallbackControllerForQueue.type).toEqual("bullmq:fallback-job");
69
- expect(fallbackControllerForQueue.useClass).toEqual(FallbackJob);
70
-
71
- const fallbackController = GlobalProviders.get("bullmq.fallback-job");
72
- expect(fallbackController).toBeUndefined();
73
- });
74
- });
75
-
76
- describe("without queue", () => {
77
- it("should set the proper store data", () => {
78
- @FallbackJobController()
79
- class FallbackJob {}
80
-
81
- const store = Store.from(FallbackJob).get("bullmq");
82
- expect(store.queue).toBeUndefined();
83
-
84
- const fallbackController = GlobalProviders.get("bullmq.fallback-job");
85
- expect(fallbackController).not.toBeUndefined();
86
- expect(fallbackController.type).toEqual("bullmq:fallback-job");
87
- expect(fallbackController.useClass).toEqual(FallbackJob);
88
-
89
- const fallbackControllerForQueue = GlobalProviders.get("bullmq.fallback-job.default");
90
- expect(fallbackControllerForQueue).toBeUndefined();
91
- });
92
- });
93
- });
@@ -1,29 +0,0 @@
1
- import {StoreMerge, useDecorators} from "@tsed/core";
2
- import {Injectable} from "@tsed/di";
3
- import {JobsOptions} from "bullmq";
4
-
5
- export function JobController(name: string, queue: string = "default", opts: JobsOptions = {}) {
6
- return useDecorators(
7
- StoreMerge("bullmq", {
8
- name,
9
- queue,
10
- opts
11
- }),
12
- Injectable({
13
- provide: `bullmq.job.${queue}.${name}`,
14
- type: opts.repeat ? "bullmq:cron" : "bullmq:job"
15
- })
16
- );
17
- }
18
-
19
- export function FallbackJobController(queue?: string) {
20
- return useDecorators(
21
- StoreMerge("bullmq", {
22
- queue
23
- }),
24
- Injectable({
25
- provide: "bullmq.fallback-job" + (queue ? `.${queue}` : ""),
26
- type: "bullmq:fallback-job"
27
- })
28
- );
29
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * @file Automatically generated by barrelsby.
3
- */
4
-
5
- export * from "./JobController";
@@ -1,132 +0,0 @@
1
- import {InjectorService} from "@tsed/di";
2
- import {JobDispatcher} from "./JobDispatcher";
3
- import {JobMethods} from "../contracts";
4
- import {JobController} from "../decorators";
5
- import {Queue} from "bullmq";
6
- import {instance, mock, verify, when, objectContaining} from "ts-mockito";
7
-
8
- @JobController("example-job", "default", {
9
- backoff: 69
10
- })
11
- class ExampleTestJob implements JobMethods {
12
- handle(payload: {msg: string}) {}
13
- }
14
-
15
- @JobController("queue-not-configured", "not-configured")
16
- class NotConfiguredQueueTestJob implements JobMethods {
17
- handle() {}
18
- }
19
-
20
- describe("JobDispatcher", () => {
21
- it("should throw an exception when a queue is not configured", () => {
22
- const injector = mock(InjectorService);
23
- when(injector.get("bullmq.queue.not-configured")).thenReturn();
24
-
25
- const dispatcher = new JobDispatcher(instance(injector));
26
-
27
- expect(dispatcher.dispatch(NotConfiguredQueueTestJob)).rejects.toThrow(new Error("Queue(not-configured) not defined"));
28
- verify(injector.get("bullmq.queue.not-configured")).once();
29
- });
30
-
31
- it("should dispatch job as type", async () => {
32
- const injector = mock(InjectorService);
33
- const queue = mock(Queue);
34
- when(queue.name).thenReturn("default");
35
-
36
- when(injector.get("bullmq.queue.default")).thenReturn(instance(queue));
37
-
38
- const dispatcher = new JobDispatcher(instance(injector));
39
-
40
- await dispatcher.dispatch(ExampleTestJob, {msg: "hello test"});
41
-
42
- verify(
43
- queue.add(
44
- "example-job",
45
- objectContaining({msg: "hello test"}),
46
- objectContaining({
47
- backoff: 69
48
- })
49
- )
50
- ).once();
51
- });
52
-
53
- it("should dispatch job as options", async () => {
54
- const injector = mock(InjectorService);
55
- const queue = mock(Queue);
56
- when(queue.name).thenReturn("special");
57
-
58
- when(injector.get("bullmq.queue.special")).thenReturn(instance(queue));
59
-
60
- const dispatcher = new JobDispatcher(instance(injector));
61
-
62
- await dispatcher.dispatch(
63
- {
64
- queue: "special",
65
- name: "some-name"
66
- },
67
- {msg: "hello test"}
68
- );
69
-
70
- verify(queue.add("some-name", objectContaining({msg: "hello test"}), objectContaining({}))).once();
71
- });
72
-
73
- it("should dispatch job as string", async () => {
74
- const injector = mock(InjectorService);
75
- const queue = mock(Queue);
76
- when(queue.name).thenReturn("default");
77
-
78
- when(injector.get("bullmq.queue.default")).thenReturn(instance(queue));
79
-
80
- const dispatcher = new JobDispatcher(instance(injector));
81
-
82
- await dispatcher.dispatch("some-name", {msg: "hello test"});
83
-
84
- verify(queue.add("some-name", objectContaining({msg: "hello test"}), objectContaining({}))).once();
85
- });
86
-
87
- it("should overwrite job options defined by the job", async () => {
88
- const injector = mock(InjectorService);
89
- const queue = mock(Queue);
90
- when(queue.name).thenReturn("default");
91
-
92
- when(injector.get("bullmq.queue.default")).thenReturn(instance(queue));
93
-
94
- const dispatcher = new JobDispatcher(instance(injector));
95
-
96
- await dispatcher.dispatch(ExampleTestJob, {msg: "hello test"}, {backoff: 42, jobId: "ffeeaa"});
97
-
98
- verify(
99
- queue.add(
100
- "example-job",
101
- objectContaining({msg: "hello test"}),
102
- objectContaining({
103
- backoff: 42,
104
- jobId: "ffeeaa"
105
- })
106
- )
107
- ).once();
108
- });
109
-
110
- it("should keep existing options and add new ones", async () => {
111
- const injector = mock(InjectorService);
112
- const queue = mock(Queue);
113
- when(queue.name).thenReturn("default");
114
-
115
- when(injector.get("bullmq.queue.default")).thenReturn(instance(queue));
116
-
117
- const dispatcher = new JobDispatcher(instance(injector));
118
-
119
- await dispatcher.dispatch(ExampleTestJob, {msg: "hello test"}, {jobId: "ffeeaa"});
120
-
121
- verify(
122
- queue.add(
123
- "example-job",
124
- objectContaining({msg: "hello test"}),
125
- objectContaining({
126
- backoff: 69,
127
- jobId: "ffeeaa"
128
- })
129
- )
130
- ).once();
131
- });
132
- });
@@ -1,61 +0,0 @@
1
- /* eslint-disable require-await */
2
- import {InjectorService, Service} from "@tsed/di";
3
- import {JobMethods, type JobStore} from "../contracts";
4
- import {Store, Type} from "@tsed/core";
5
- import {Job as BullMQJob, JobsOptions, Queue} from "bullmq";
6
- import type {JobDispatcherOptions} from "./JobDispatcherOptions";
7
-
8
- @Service()
9
- export class JobDispatcher {
10
- constructor(private readonly injector: InjectorService) {}
11
-
12
- public async dispatch<T extends JobMethods>(
13
- job: Type<T>,
14
- payload?: Parameters<T["handle"]>[0],
15
- options?: JobsOptions
16
- ): Promise<BullMQJob>;
17
- public async dispatch<T = any>(job: JobDispatcherOptions, payload?: T, options?: JobsOptions): Promise<BullMQJob>;
18
- public async dispatch<T = any>(job: string, payload?: T, options?: JobsOptions): Promise<BullMQJob>;
19
- public async dispatch(job: Type | JobDispatcherOptions | string, payload: any, options: JobsOptions = {}): Promise<BullMQJob> {
20
- const {queueName, jobName, defaultJobOptions} = this.resolveDispatchArgs(job);
21
-
22
- const queue = this.injector.get<Queue>(`bullmq.queue.${queueName}`);
23
-
24
- if (!queue) {
25
- throw new Error(`Queue(${queueName}) not defined`);
26
- }
27
-
28
- return queue.add(jobName, payload, {
29
- ...defaultJobOptions,
30
- ...options
31
- });
32
- }
33
-
34
- private resolveDispatchArgs(job: Type | JobDispatcherOptions | string) {
35
- let queueName: string;
36
- let jobName: string;
37
- let defaultJobOptions: JobsOptions | undefined;
38
-
39
- if (typeof job === "function") {
40
- // job is passed as a Type
41
- const store = Store.from(job).get<JobStore>("bullmq");
42
- queueName = store.queue;
43
- jobName = store.name;
44
- defaultJobOptions = store.opts;
45
- } else if (typeof job === "object") {
46
- // job is passed as JobDispatcherOptions
47
- queueName = job.queue;
48
- jobName = job.name;
49
- } else {
50
- // job is passed as a string
51
- queueName = "default";
52
- jobName = job;
53
- }
54
-
55
- return {
56
- queueName,
57
- jobName,
58
- defaultJobOptions
59
- };
60
- }
61
- }
@@ -1,4 +0,0 @@
1
- export interface JobDispatcherOptions {
2
- queue: string;
3
- name: string;
4
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * @file Automatically generated by barrelsby.
3
- */
4
-
5
- export * from "./JobDispatcher";
6
- export * from "./JobDispatcherOptions";
package/src/index.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * @file Automatically generated by barrelsby.
3
- */
4
-
5
- export * from "./BullMQModule";
6
- export * from "./config/config";
7
- export * from "./contracts/JobMethods";
8
- export * from "./contracts/JobStore";
9
- export * from "./decorators/JobController";
10
- export * from "./dispatchers/JobDispatcher";
11
- export * from "./dispatchers/JobDispatcherOptions";
package/tsconfig.esm.json DELETED
@@ -1,39 +0,0 @@
1
- {
2
- "extends": "@tsed/typescript/tsconfig.node.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "module": "ES2020",
6
- "rootDir": "src",
7
- "outDir": "./lib/esm",
8
- "declaration": true,
9
- "declarationDir": "./lib/types",
10
- "composite": true,
11
- "noEmit": false
12
- },
13
- "include": ["src", "src/**/*.json"],
14
- "exclude": [
15
- "node_modules",
16
- "test",
17
- "lib",
18
- "benchmark",
19
- "coverage",
20
- "spec",
21
- "**/*.benchmark.ts",
22
- "**/*.spec.ts",
23
- "keys",
24
- "jest.config.js",
25
- "**/__mock__/**",
26
- "webpack.config.js"
27
- ],
28
- "references": [
29
- {
30
- "path": "../../platform/common"
31
- },
32
- {
33
- "path": "../../core"
34
- },
35
- {
36
- "path": "../../di"
37
- }
38
- ]
39
- }
package/tsconfig.json DELETED
@@ -1,39 +0,0 @@
1
- {
2
- "extends": "@tsed/typescript/tsconfig.node.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "module": "commonjs",
6
- "rootDir": "src",
7
- "outDir": "./lib/cjs",
8
- "declaration": true,
9
- "declarationDir": "./lib/types",
10
- "composite": true,
11
- "noEmit": false
12
- },
13
- "include": ["src", "src/**/*.json"],
14
- "exclude": [
15
- "node_modules",
16
- "test",
17
- "lib",
18
- "benchmark",
19
- "coverage",
20
- "spec",
21
- "**/*.benchmark.ts",
22
- "**/*.spec.ts",
23
- "keys",
24
- "jest.config.js",
25
- "**/__mock__/**",
26
- "webpack.config.js"
27
- ],
28
- "references": [
29
- {
30
- "path": "../../platform/common"
31
- },
32
- {
33
- "path": "../../core"
34
- },
35
- {
36
- "path": "../../di"
37
- }
38
- ]
39
- }