@domusjs/jobs 0.1.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.
- package/README.md +101 -0
- package/dist/core/domus-job-client-connection.interface.d.ts +3 -0
- package/dist/core/domus-job-client-connection.interface.d.ts.map +1 -0
- package/dist/core/domus-job-client-connection.interface.js +2 -0
- package/dist/core/job.interface.d.ts +10 -0
- package/dist/core/job.interface.d.ts.map +1 -0
- package/dist/core/job.interface.js +2 -0
- package/dist/core/queue.interface.d.ts +5 -0
- package/dist/core/queue.interface.d.ts.map +1 -0
- package/dist/core/queue.interface.js +2 -0
- package/dist/core/worker.interface.d.ts +7 -0
- package/dist/core/worker.interface.d.ts.map +1 -0
- package/dist/core/worker.interface.js +2 -0
- package/dist/examples/hello-world.example.d.ts +2 -0
- package/dist/examples/hello-world.example.d.ts.map +1 -0
- package/dist/examples/hello-world.example.js +32 -0
- package/dist/examples/hello-world.job.d.ts +12 -0
- package/dist/examples/hello-world.job.d.ts.map +1 -0
- package/dist/examples/hello-world.job.js +16 -0
- package/dist/examples/simple-adder.job.d.ts +13 -0
- package/dist/examples/simple-adder.job.d.ts.map +1 -0
- package/dist/examples/simple-adder.job.js +19 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/queue/domus-client.d.ts +16 -0
- package/dist/queue/domus-client.d.ts.map +1 -0
- package/dist/queue/domus-client.js +35 -0
- package/dist/queue/domus-queue.d.ts +10 -0
- package/dist/queue/domus-queue.d.ts.map +1 -0
- package/dist/queue/domus-queue.js +19 -0
- package/dist/queue/domus-worker.d.ts +11 -0
- package/dist/queue/domus-worker.d.ts.map +1 -0
- package/dist/queue/domus-worker.js +38 -0
- package/dist/queue/job-task.d.ts +7 -0
- package/dist/queue/job-task.d.ts.map +1 -0
- package/dist/queue/job-task.js +9 -0
- package/package.json +22 -0
package/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# @domusjs/jobs
|
2
|
+
|
3
|
+
The `@domusjs/jobs` package offers a structured and scalable solution for managing **background jobs** and **recurring tasks** in distributed applications. It operates with [BullMQ](https://github.com/taskforcesh/bullmq) under the hood.
|
4
|
+
|
5
|
+
## ✨ Features
|
6
|
+
|
7
|
+
- 🎯 Job execution abstraction
|
8
|
+
- 📦 Uses [BullMQ](https://github.com/taskforcesh/bullmq) under the hood
|
9
|
+
- 🧪 Decoupled from infrastructure, perfect for testing
|
10
|
+
- 🧩 Modular design: define jobs, workers, and queues separately
|
11
|
+
|
12
|
+
## 🚦 When to Use
|
13
|
+
|
14
|
+
Use this module when:
|
15
|
+
|
16
|
+
- You need to run asynchronous or long-running tasks off the main HTTP request cycle
|
17
|
+
- You need job retries, failure handling, or queueing logic
|
18
|
+
|
19
|
+
## Install
|
20
|
+
|
21
|
+
```bash
|
22
|
+
npm install @domusjs/jobs
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### 1. Define a Job
|
28
|
+
|
29
|
+
```ts
|
30
|
+
// hello-world.job.ts
|
31
|
+
import { JobTask } from '@domusjs/jobs';
|
32
|
+
|
33
|
+
type HelloWorldJobData = {
|
34
|
+
name: string;
|
35
|
+
};
|
36
|
+
|
37
|
+
export class HelloWorldJob extends JobTask {
|
38
|
+
static readonly jobName = 'hello_world';
|
39
|
+
|
40
|
+
constructor(public readonly data: HelloWorldJobData) {
|
41
|
+
super(data);
|
42
|
+
}
|
43
|
+
|
44
|
+
async execute(): Promise<string> {
|
45
|
+
const ret = `[HelloWorldJob] Hello ${this.data.name}!`;
|
46
|
+
return ret;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
✅ This defines a reusable job by extending `JobTask`, with a clear `jobName` identifier and its own `execute()` logic.
|
52
|
+
|
53
|
+
---
|
54
|
+
|
55
|
+
### 2. Set Up Client, Queue, Worker, and Events
|
56
|
+
|
57
|
+
```ts
|
58
|
+
import 'reflect-metadata';
|
59
|
+
|
60
|
+
import { DomusJobClient } from '@domusjs/jobs';
|
61
|
+
import { HelloWorldJob } from './hello-world.job';
|
62
|
+
|
63
|
+
// Create a client with Redis connection settings
|
64
|
+
const client = new DomusJobClient({
|
65
|
+
host: 'localhost',
|
66
|
+
port: 6379,
|
67
|
+
password: 'your_password',
|
68
|
+
});
|
69
|
+
|
70
|
+
// Create a queue
|
71
|
+
const queue = client.createQueue('basic_queue');
|
72
|
+
|
73
|
+
// Add a worker for the queue
|
74
|
+
const worker = client.createWorker(queue, [HelloWorldJob]);
|
75
|
+
|
76
|
+
// Enqueue a job
|
77
|
+
await queue.add(new HelloWorldJob({ name: 'DomusJS' }));
|
78
|
+
|
79
|
+
// Handle worker events
|
80
|
+
worker.onFailed((job, error) => {
|
81
|
+
console.error('Job failed:', {
|
82
|
+
jobId: job?.id,
|
83
|
+
error: error.message,
|
84
|
+
});
|
85
|
+
});
|
86
|
+
|
87
|
+
worker.onCompleted((job, result) => {
|
88
|
+
console.info('Job completed:', {
|
89
|
+
jobId: job?.id,
|
90
|
+
result,
|
91
|
+
});
|
92
|
+
});
|
93
|
+
```
|
94
|
+
|
95
|
+
---
|
96
|
+
|
97
|
+
## 🔗 Learn More
|
98
|
+
|
99
|
+
For advanced aspects, check out the full documentation:
|
100
|
+
|
101
|
+
👉 [https://docs.domusjs.com](https://docs.domusjs.com)
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"domus-job-client-connection.interface.d.ts","sourceRoot":"","sources":["../../src/core/domus-job-client-connection.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,MAAM,MAAM,wBAAwB,GAAG,YAAY,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"job.interface.d.ts","sourceRoot":"","sources":["../../src/core/job.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,GAAG,CAAC,CAAC,GAAG,GAAG;IAC1B,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,CAAC,CAAC;IACR,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"queue.interface.d.ts","sourceRoot":"","sources":["../../src/core/queue.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,MAAM,WAAW,KAAK;IACpB,GAAG,CAAC,CAAC,SAAS,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnD"}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { Job } from './job.interface';
|
2
|
+
export interface Worker {
|
3
|
+
onFailed(callback: (job: Job, error: Error) => void): void;
|
4
|
+
onCompleted(callback: (job: Job, result: unknown) => void): void;
|
5
|
+
close(): Promise<void>;
|
6
|
+
}
|
7
|
+
//# sourceMappingURL=worker.interface.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"worker.interface.d.ts","sourceRoot":"","sources":["../../src/core/worker.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,IAAI,CAAC;IAC3D,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IACjE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"hello-world.example.d.ts","sourceRoot":"","sources":["../../src/examples/hello-world.example.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
require("reflect-metadata");
|
4
|
+
const domus_client_1 = require("../queue/domus-client");
|
5
|
+
const hello_world_job_1 = require("./hello-world.job");
|
6
|
+
const simple_adder_job_1 = require("./simple-adder.job");
|
7
|
+
// Create a client
|
8
|
+
const client = new domus_client_1.DomusJobClient({
|
9
|
+
host: 'localhost',
|
10
|
+
port: 6379,
|
11
|
+
password: 'password',
|
12
|
+
});
|
13
|
+
// Create queue
|
14
|
+
const queue = client.createQueue('basic_queue');
|
15
|
+
// Create worker
|
16
|
+
const worker = client.createWorker(queue, [hello_world_job_1.HelloWorldJob, simple_adder_job_1.SimpleAdderJob]);
|
17
|
+
// Enqueue jobs
|
18
|
+
queue.add(new hello_world_job_1.HelloWorldJob({ name: 'DomusJS' }));
|
19
|
+
queue.add(new simple_adder_job_1.SimpleAdderJob({ a: 1000, b: 337 }));
|
20
|
+
// Handle worker events
|
21
|
+
worker.onFailed((job, error) => {
|
22
|
+
console.error('Job failed', {
|
23
|
+
jobId: job?.id,
|
24
|
+
error: error.message,
|
25
|
+
});
|
26
|
+
});
|
27
|
+
worker.onCompleted((job, result) => {
|
28
|
+
console.info('Job completed', {
|
29
|
+
jobId: job?.id,
|
30
|
+
result,
|
31
|
+
});
|
32
|
+
});
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { JobTask } from '../queue/job-task';
|
2
|
+
type HelloWorldJobData = {
|
3
|
+
name: string;
|
4
|
+
};
|
5
|
+
export declare class HelloWorldJob extends JobTask {
|
6
|
+
readonly data: HelloWorldJobData;
|
7
|
+
static readonly jobName = "hello_world";
|
8
|
+
constructor(data: HelloWorldJobData);
|
9
|
+
execute(): Promise<string>;
|
10
|
+
}
|
11
|
+
export {};
|
12
|
+
//# sourceMappingURL=hello-world.job.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"hello-world.job.d.ts","sourceRoot":"","sources":["../../src/examples/hello-world.job.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qBAAa,aAAc,SAAQ,OAAO;aAGZ,IAAI,EAAE,iBAAiB;IAFnD,MAAM,CAAC,QAAQ,CAAC,OAAO,iBAAiB;gBAEZ,IAAI,EAAE,iBAAiB;IAI7C,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;CAKjC"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.HelloWorldJob = void 0;
|
4
|
+
const job_task_1 = require("../queue/job-task");
|
5
|
+
class HelloWorldJob extends job_task_1.JobTask {
|
6
|
+
constructor(data) {
|
7
|
+
super(data);
|
8
|
+
this.data = data;
|
9
|
+
}
|
10
|
+
async execute() {
|
11
|
+
const ret = `[HelloWorldJob] Hello ${this.data.name}!`;
|
12
|
+
return ret;
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.HelloWorldJob = HelloWorldJob;
|
16
|
+
HelloWorldJob.jobName = 'hello_world';
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { JobTask } from '../queue/job-task';
|
2
|
+
type SimpleAdderJobData = {
|
3
|
+
a: number;
|
4
|
+
b: number;
|
5
|
+
};
|
6
|
+
export declare class SimpleAdderJob extends JobTask {
|
7
|
+
readonly data: SimpleAdderJobData;
|
8
|
+
static readonly jobName = "simple_adder";
|
9
|
+
constructor(data: SimpleAdderJobData);
|
10
|
+
execute(): Promise<string>;
|
11
|
+
}
|
12
|
+
export {};
|
13
|
+
//# sourceMappingURL=simple-adder.job.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"simple-adder.job.d.ts","sourceRoot":"","sources":["../../src/examples/simple-adder.job.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,KAAK,kBAAkB,GAAG;IACxB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX,CAAC;AAEF,qBAAa,cAAe,SAAQ,OAAO;aAGb,IAAI,EAAE,kBAAkB;IAFpD,MAAM,CAAC,QAAQ,CAAC,OAAO,kBAAkB;gBAEb,IAAI,EAAE,kBAAkB;IAI9C,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;CAOjC"}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.SimpleAdderJob = void 0;
|
4
|
+
const job_task_1 = require("../queue/job-task");
|
5
|
+
class SimpleAdderJob extends job_task_1.JobTask {
|
6
|
+
constructor(data) {
|
7
|
+
super(data);
|
8
|
+
this.data = data;
|
9
|
+
}
|
10
|
+
async execute() {
|
11
|
+
return new Promise((resolve) => {
|
12
|
+
setTimeout(() => {
|
13
|
+
resolve(`[SimpleAdderJob] ${this.data.a} + ${this.data.b} = ${this.data.a + this.data.b}`);
|
14
|
+
}, 1000);
|
15
|
+
});
|
16
|
+
}
|
17
|
+
}
|
18
|
+
exports.SimpleAdderJob = SimpleAdderJob;
|
19
|
+
SimpleAdderJob.jobName = 'simple_adder';
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./queue/domus-client"), exports);
|
18
|
+
__exportStar(require("./queue/job-task"), exports);
|
19
|
+
__exportStar(require("./queue/domus-queue"), exports);
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { JobTask } from './job-task';
|
2
|
+
import { Worker as IDomusWorker } from '../core/worker.interface';
|
3
|
+
import { DomusQueue } from './domus-queue';
|
4
|
+
import { DomusJobClientConnection } from '../core/domus-job-client-connection.interface';
|
5
|
+
interface JobTaskConstructor<T extends JobTask = any> {
|
6
|
+
new (data: any): T;
|
7
|
+
jobName: string;
|
8
|
+
}
|
9
|
+
export declare class DomusJobClient {
|
10
|
+
private readonly connection;
|
11
|
+
constructor(connection: DomusJobClientConnection);
|
12
|
+
createQueue(name: string): DomusQueue;
|
13
|
+
createWorker(queue: DomusQueue, jobs: JobTaskConstructor[]): IDomusWorker;
|
14
|
+
}
|
15
|
+
export {};
|
16
|
+
//# sourceMappingURL=domus-client.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"domus-client.d.ts","sourceRoot":"","sources":["../../src/queue/domus-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,+CAA+C,CAAC;AAEzF,UAAU,kBAAkB,CAAC,CAAC,SAAS,OAAO,GAAG,GAAG;IAClD,KAAK,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,UAAU;gBAAV,UAAU,EAAE,wBAAwB;IAEjE,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU;IAKrC,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,kBAAkB,EAAE,GAAG,YAAY;CA2B1E"}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.DomusJobClient = void 0;
|
4
|
+
const bullmq_1 = require("bullmq");
|
5
|
+
const domus_worker_1 = require("./domus-worker");
|
6
|
+
const domus_queue_1 = require("./domus-queue");
|
7
|
+
class DomusJobClient {
|
8
|
+
constructor(connection) {
|
9
|
+
this.connection = connection;
|
10
|
+
}
|
11
|
+
createQueue(name) {
|
12
|
+
const queue = new bullmq_1.Queue(name, { connection: this.connection });
|
13
|
+
return new domus_queue_1.DomusQueue(queue);
|
14
|
+
}
|
15
|
+
createWorker(queue, jobs) {
|
16
|
+
const jobMap = {};
|
17
|
+
for (const jobClass of jobs) {
|
18
|
+
const jobName = jobClass.jobName;
|
19
|
+
if (!jobName) {
|
20
|
+
throw new Error(`Job class ${jobClass.name} missing static jobName`);
|
21
|
+
}
|
22
|
+
jobMap[jobName] = jobClass;
|
23
|
+
}
|
24
|
+
const worker = new bullmq_1.Worker(queue.name, async (job) => {
|
25
|
+
const JobClass = jobMap[job.name];
|
26
|
+
if (!JobClass) {
|
27
|
+
throw new Error(`No job class found for job: ${job.name}`);
|
28
|
+
}
|
29
|
+
const task = new JobClass(job.data);
|
30
|
+
return await task.execute();
|
31
|
+
}, { connection: this.connection });
|
32
|
+
return new domus_worker_1.DomusWorker(worker);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.DomusJobClient = DomusJobClient;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { Queue } from 'bullmq';
|
2
|
+
import { JobTask } from './job-task';
|
3
|
+
import { Queue as IDomusQueue } from '../core/queue.interface';
|
4
|
+
export declare class DomusQueue implements IDomusQueue {
|
5
|
+
private readonly queue;
|
6
|
+
readonly name: string;
|
7
|
+
constructor(queue: Queue);
|
8
|
+
add<T extends JobTask>(payload: T): Promise<void>;
|
9
|
+
}
|
10
|
+
//# sourceMappingURL=domus-queue.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"domus-queue.d.ts","sourceRoot":"","sources":["../../src/queue/domus-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE/D,qBAAa,UAAW,YAAW,WAAW;IAGhC,OAAO,CAAC,QAAQ,CAAC,KAAK;IAFlC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEO,KAAK,EAAE,KAAK;IAInC,GAAG,CAAC,CAAC,SAAS,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAYxD"}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.DomusQueue = void 0;
|
4
|
+
class DomusQueue {
|
5
|
+
constructor(queue) {
|
6
|
+
this.queue = queue;
|
7
|
+
this.name = queue.name;
|
8
|
+
}
|
9
|
+
async add(payload) {
|
10
|
+
// Infer the jobName from the JobTask class via its constructor.
|
11
|
+
const jobClass = payload.constructor;
|
12
|
+
const jobName = jobClass.jobName;
|
13
|
+
if (!jobName) {
|
14
|
+
throw new Error('Job class is missing static jobName');
|
15
|
+
}
|
16
|
+
await this.queue.add(jobName, payload.data);
|
17
|
+
}
|
18
|
+
}
|
19
|
+
exports.DomusQueue = DomusQueue;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { Worker } from 'bullmq';
|
2
|
+
import { Job as DomusJob } from '../core/job.interface';
|
3
|
+
import { Worker as IDomusWorker } from '../core/worker.interface';
|
4
|
+
export declare class DomusWorker implements IDomusWorker {
|
5
|
+
private readonly worker;
|
6
|
+
constructor(worker: Worker);
|
7
|
+
onFailed(callback: (job: DomusJob, error: Error) => void): void;
|
8
|
+
onCompleted(callback: (job: DomusJob, result: unknown) => void): void;
|
9
|
+
close(): Promise<void>;
|
10
|
+
}
|
11
|
+
//# sourceMappingURL=domus-worker.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"domus-worker.d.ts","sourceRoot":"","sources":["../../src/queue/domus-worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAO,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,GAAG,IAAI,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAclE,qBAAa,WAAY,YAAW,YAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAE3C,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,IAAI;IAU/D,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI;IAM/D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.DomusWorker = void 0;
|
4
|
+
function toDomusJob(job) {
|
5
|
+
return {
|
6
|
+
id: job.id ?? null,
|
7
|
+
name: job.name,
|
8
|
+
data: job.data,
|
9
|
+
attemptsMade: job.attemptsMade,
|
10
|
+
failedReason: job.failedReason,
|
11
|
+
returnvalue: job.returnvalue,
|
12
|
+
timestamp: job.timestamp,
|
13
|
+
};
|
14
|
+
}
|
15
|
+
class DomusWorker {
|
16
|
+
constructor(worker) {
|
17
|
+
this.worker = worker;
|
18
|
+
}
|
19
|
+
onFailed(callback) {
|
20
|
+
this.worker.on('failed', (job, error) => {
|
21
|
+
if (job) {
|
22
|
+
callback(toDomusJob(job), error);
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
callback({}, error);
|
26
|
+
}
|
27
|
+
});
|
28
|
+
}
|
29
|
+
onCompleted(callback) {
|
30
|
+
this.worker.on('completed', (job, result) => {
|
31
|
+
callback(toDomusJob(job), result);
|
32
|
+
});
|
33
|
+
}
|
34
|
+
async close() {
|
35
|
+
await this.worker.close();
|
36
|
+
}
|
37
|
+
}
|
38
|
+
exports.DomusWorker = DomusWorker;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"job-task.d.ts","sourceRoot":"","sources":["../../src/queue/job-task.ts"],"names":[],"mappings":"AAAA,8BAAsB,OAAO,CAAC,CAAC,GAAG,GAAG;aAEP,IAAI,EAAE,GAAG;IADrC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBACJ,IAAI,EAAE,GAAG;IAErC,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;CAC/B"}
|
package/package.json
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
{
|
2
|
+
"name": "@domusjs/jobs",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"main": "dist/index.js",
|
5
|
+
"types": "dist/index.d.ts",
|
6
|
+
"files": [
|
7
|
+
"dist"
|
8
|
+
],
|
9
|
+
"scripts": {
|
10
|
+
"build": "tsc -b",
|
11
|
+
"clean": "rm -rf dist && rm -rf tsconfig.tsbuildinfo && rm -rf node_modules"
|
12
|
+
},
|
13
|
+
"license": "MIT",
|
14
|
+
"author": "Sergio Salcedo",
|
15
|
+
"dependencies": {
|
16
|
+
"bullmq": "^5.49.0",
|
17
|
+
"reflect-metadata": "^0.2.2"
|
18
|
+
},
|
19
|
+
"peerDependencies": {
|
20
|
+
"tsyringe": "^4.9.1"
|
21
|
+
}
|
22
|
+
}
|