@alanszp/queue 8.0.0 → 9.2.3
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/dist/helpers/appIdentifier.d.ts +1 -0
- package/dist/helpers/appIdentifier.js +10 -0
- package/dist/helpers/appIdentifier.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/queue/createQueue.d.ts +2 -1
- package/dist/queue/createQueue.js +2 -2
- package/dist/queue/createQueue.js.map +1 -1
- package/dist/queue/queue.d.ts +11 -3
- package/dist/queue/queue.js +20 -3
- package/dist/queue/queue.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/dist/worker/worker.d.ts +9 -0
- package/dist/worker/worker.js +30 -9
- package/dist/worker/worker.js.map +1 -1
- package/dist/wrappers/withContext.d.ts +3 -0
- package/dist/wrappers/withContext.js +33 -0
- package/dist/wrappers/withContext.js.map +1 -0
- package/package.json +5 -3
- package/src/helpers/appIdentifier.ts +5 -0
- package/src/index.ts +1 -0
- package/src/queue/createQueue.ts +13 -2
- package/src/queue/queue.ts +34 -4
- package/src/types.ts +1 -0
- package/src/worker/worker.ts +43 -10
- package/src/wrappers/withContext.ts +25 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function appIdentifier(): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.appIdentifier = void 0;
|
|
4
|
+
function appIdentifier() {
|
|
5
|
+
const appName = process.env.API_ORIGIN_NAME || "undefined";
|
|
6
|
+
const env = process.env.NODE_ENV || "development";
|
|
7
|
+
return `${appName}:${env}`;
|
|
8
|
+
}
|
|
9
|
+
exports.appIdentifier = appIdentifier;
|
|
10
|
+
//# sourceMappingURL=appIdentifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appIdentifier.js","sourceRoot":"","sources":["../../src/helpers/appIdentifier.ts"],"names":[],"mappings":";;;AAAA,SAAgB,aAAa;IAC3B,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,WAAW,CAAC;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;IAClD,OAAO,GAAG,OAAO,IAAI,GAAG,EAAE,CAAC;AAC7B,CAAC;AAJD,sCAIC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./queue/createQueue"), exports);
|
|
14
|
+
__exportStar(require("./queue/queue"), exports);
|
|
14
15
|
__exportStar(require("./connectionManager"), exports);
|
|
15
16
|
__exportStar(require("./shutdownQueue"), exports);
|
|
16
17
|
__exportStar(require("./types"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sDAAoC;AACpC,sDAAoC;AACpC,kDAAgC;AAChC,0CAAwB;AACxB,2CAAyB;AACzB,kDAAgC;AAChC,4DAA0C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sDAAoC;AACpC,gDAA8B;AAC9B,sDAAoC;AACpC,kDAAgC;AAChC,0CAAwB;AACxB,2CAAyB;AACzB,kDAAgC;AAChC,4DAA0C"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Queue } from "./queue";
|
|
2
2
|
import { JobData, QueueOptions } from "../types";
|
|
3
|
-
|
|
3
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
4
|
+
export declare function createQueue<JobType = JobData>(name: string, getContext: () => SharedContext, queueOptions?: QueueOptions): Queue;
|
|
@@ -4,9 +4,9 @@ exports.createQueue = void 0;
|
|
|
4
4
|
const queue_1 = require("./queue");
|
|
5
5
|
const queueRepository_1 = require("./queueRepository");
|
|
6
6
|
const connectionManager_1 = require("../connectionManager");
|
|
7
|
-
function createQueue(name, queueOptions) {
|
|
7
|
+
function createQueue(name, getContext, queueOptions) {
|
|
8
8
|
const connection = connectionManager_1.ConnectionManager.getInstance().getConnection();
|
|
9
|
-
const queue = new queue_1.Queue(connection, name, connectionManager_1.ConnectionManager.getInstance().getServiceName(), queueOptions);
|
|
9
|
+
const queue = new queue_1.Queue(connection, name, connectionManager_1.ConnectionManager.getInstance().getServiceName(), getContext, queueOptions);
|
|
10
10
|
queueRepository_1.QueueRepository.Instance.registerQueue(queue);
|
|
11
11
|
return queue;
|
|
12
12
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueue.js","sourceRoot":"","sources":["../../src/queue/createQueue.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAChC,uDAAoD;AACpD,4DAAyD;
|
|
1
|
+
{"version":3,"file":"createQueue.js","sourceRoot":"","sources":["../../src/queue/createQueue.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAChC,uDAAoD;AACpD,4DAAyD;AAIzD,SAAgB,WAAW,CACzB,IAAY,EACZ,UAA+B,EAC/B,YAA2B;IAE3B,MAAM,UAAU,GAAG,qCAAiB,CAAC,WAAW,EAAE,CAAC,aAAa,EAAE,CAAC;IACnE,MAAM,KAAK,GAAG,IAAI,aAAK,CACrB,UAAU,EACV,IAAI,EACJ,qCAAiB,CAAC,WAAW,EAAE,CAAC,cAAc,EAAE,EAChD,UAAU,EACV,YAAY,CACb,CAAC;IACF,iCAAe,CAAC,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC9C,OAAO,KAAK,CAAC;AACf,CAAC;AAfD,kCAeC"}
|
package/dist/queue/queue.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
import { Job, JobsOptions } from "bullmq";
|
|
2
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
1
3
|
import { ConnectionOptions, JobData, QueueOptions } from "../types";
|
|
2
4
|
export declare class Queue<JobType = JobData> {
|
|
3
5
|
private _queue;
|
|
4
6
|
private name;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
private getSharedContext;
|
|
8
|
+
constructor(connection: ConnectionOptions, name: string, prefix: string, getSharedContext: () => SharedContext, queueOptions?: QueueOptions);
|
|
9
|
+
getName(): string;
|
|
10
|
+
publishJob(job: JobType, opts?: JobsOptions): Promise<Job<JobType>>;
|
|
11
|
+
publishBulkJob(jobDatas: JobType[]): Promise<Job<JobType>[]>;
|
|
12
|
+
publishBulkJobWithOptions(jobDefinitions: {
|
|
13
|
+
jobData: JobType;
|
|
14
|
+
opts: JobsOptions;
|
|
15
|
+
}[]): Promise<void>;
|
|
8
16
|
close(): Promise<void>;
|
|
9
17
|
private get queue();
|
|
10
18
|
}
|
package/dist/queue/queue.js
CHANGED
|
@@ -16,8 +16,9 @@ const BULL_PREFIX = "b";
|
|
|
16
16
|
const DEFAULT_COMPLETED_JOB_MAX_AGE_IN_SECONDS = 60 * 60 * 24 * 30;
|
|
17
17
|
const DEFAULT_COMPLETED_JOB_MAX_COUNT = 500;
|
|
18
18
|
class Queue {
|
|
19
|
-
constructor(connection, name, prefix, queueOptions) {
|
|
19
|
+
constructor(connection, name, prefix, getSharedContext, queueOptions) {
|
|
20
20
|
this.name = name;
|
|
21
|
+
this.getSharedContext = getSharedContext;
|
|
21
22
|
this._queue = new types_1.RawQueue(name, Object.assign(Object.assign({}, (0, lodash_1.merge)({
|
|
22
23
|
defaultJobOptions: {
|
|
23
24
|
removeOnComplete: {
|
|
@@ -33,14 +34,30 @@ class Queue {
|
|
|
33
34
|
connection,
|
|
34
35
|
}, queueOptions || {})), { prefix: `{${prefix}}:${BULL_PREFIX}` }));
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
+
getName() {
|
|
38
|
+
return this.name;
|
|
39
|
+
}
|
|
40
|
+
publishJob(job, opts) {
|
|
37
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
|
|
42
|
+
const context = this.getSharedContext();
|
|
43
|
+
const lid = context.getLifecycleId();
|
|
44
|
+
const lch = context.getLifecycleChain();
|
|
45
|
+
return this.queue.add(this.name, Object.assign(Object.assign({}, job), { lid, lch }), opts);
|
|
39
46
|
});
|
|
40
47
|
}
|
|
41
48
|
publishBulkJob(jobDatas) {
|
|
42
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
50
|
const jobs = jobDatas.map((data) => ({ name: this.name, data }));
|
|
51
|
+
return this.queue.addBulk(jobs);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
publishBulkJobWithOptions(jobDefinitions) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const jobs = jobDefinitions.map(({ jobData: data, opts }) => ({
|
|
57
|
+
name: this.name,
|
|
58
|
+
data,
|
|
59
|
+
opts,
|
|
60
|
+
}));
|
|
44
61
|
yield this.queue.addBulk(jobs);
|
|
45
62
|
});
|
|
46
63
|
}
|
package/dist/queue/queue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/queue/queue.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA+B;
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/queue/queue.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA+B;AAG/B,oCAA8E;AAE9E,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB,MAAM,wCAAwC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AACnE,MAAM,+BAA+B,GAAG,GAAG,CAAC;AAE5C,MAAa,KAAK;IAO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AC,CAAC;QAClC,CAAC;KAAA;IAEK,yBAAyB,CAC7B,cAAyD;;YAEzD,MAAM,IAAI,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;gBAC5D,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,IAAI;gBACJ,IAAI;aACL,CAAC,CAAC,CAAC;YACJ,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC;KAAA;IAEK,KAAK;;YACT,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;KAAA;IAED,IAAY,KAAK;QACf,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AA1ED,sBA0EC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ConnectionOptions, RedisOptions, Job, Worker as RawWorker, WorkerOptions, Queue as RawQueue, QueueOptions, } from "bullmq";
|
|
1
|
+
export { ConnectionOptions, RedisOptions, Job, Worker as RawWorker, WorkerOptions, Queue as RawQueue, QueueOptions, JobsOptions, } from "bullmq";
|
|
2
2
|
export declare type JobData = any;
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,iCASgB;AANd,6FAAA,GAAG,OAAA;AACH,mGAAA,MAAM,OAAa;AAEnB,kGAAA,KAAK,OAAY"}
|
package/dist/worker/worker.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { ILogger } from "@alanszp/logger";
|
|
1
2
|
import { JobData, Job, RawWorker, WorkerOptions } from "../types";
|
|
3
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
4
|
+
import { Audit } from "@alanszp/audit";
|
|
2
5
|
export interface WorkerStatus {
|
|
3
6
|
running: boolean;
|
|
4
7
|
paused: boolean;
|
|
@@ -7,6 +10,11 @@ export interface WorkerSetup {
|
|
|
7
10
|
queueName: string;
|
|
8
11
|
workerOptions?: WorkerOptions;
|
|
9
12
|
}
|
|
13
|
+
export interface WorkerContext {
|
|
14
|
+
sharedContext: SharedContext;
|
|
15
|
+
baseLogger: ILogger;
|
|
16
|
+
audit: Audit;
|
|
17
|
+
}
|
|
10
18
|
interface Worker<T = JobData> {
|
|
11
19
|
handleJobFailed?(job: Job<T>, error: Error): Promise<void> | void;
|
|
12
20
|
handleJobCompleted?(job: Job<T>): Promise<void> | void;
|
|
@@ -19,6 +27,7 @@ declare abstract class Worker<T = JobData> {
|
|
|
19
27
|
constructor();
|
|
20
28
|
abstract process(job: Job<T>): Promise<void>;
|
|
21
29
|
abstract setup(): WorkerSetup;
|
|
30
|
+
abstract getContext(): WorkerContext;
|
|
22
31
|
close(): Promise<void>;
|
|
23
32
|
get status(): WorkerStatus;
|
|
24
33
|
get id(): RawWorker["id"];
|
package/dist/worker/worker.js
CHANGED
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.Worker = void 0;
|
|
13
13
|
const connectionManager_1 = require("../connectionManager");
|
|
14
14
|
const types_1 = require("../types");
|
|
15
|
+
const withContext_1 = require("../wrappers/withContext");
|
|
15
16
|
const BULL_PREFIX = "b";
|
|
16
17
|
class Worker {
|
|
17
18
|
constructor() {
|
|
@@ -34,14 +35,21 @@ class Worker {
|
|
|
34
35
|
return this.worker.id;
|
|
35
36
|
}
|
|
36
37
|
processJob() {
|
|
37
|
-
return (job) => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
this.getLogger().info(`worker.process.job_received`, {
|
|
38
|
+
return (0, withContext_1.withContext)(this.getContext(), (job) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
this.getLogger().info(`worker.process.job_received`, {
|
|
40
|
+
queue: this.queueFullName,
|
|
41
|
+
job,
|
|
42
|
+
});
|
|
39
43
|
yield this.process(job);
|
|
40
|
-
});
|
|
44
|
+
}));
|
|
41
45
|
}
|
|
42
46
|
processFailed(job, error) {
|
|
43
47
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
this.getLogger().warn("worker.job.failed", {
|
|
48
|
+
this.getLogger().warn("worker.job.failed", {
|
|
49
|
+
queue: this.queueFullName,
|
|
50
|
+
job,
|
|
51
|
+
error,
|
|
52
|
+
});
|
|
45
53
|
if (this.handleJobFailed) {
|
|
46
54
|
yield this.handleJobFailed(job, error);
|
|
47
55
|
}
|
|
@@ -49,7 +57,10 @@ class Worker {
|
|
|
49
57
|
}
|
|
50
58
|
processCompleted(job) {
|
|
51
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
this.getLogger().info("worker.job.completed", {
|
|
60
|
+
this.getLogger().info("worker.job.completed", {
|
|
61
|
+
queue: this.queueFullName,
|
|
62
|
+
job,
|
|
63
|
+
});
|
|
53
64
|
if (this.handleJobCompleted) {
|
|
54
65
|
yield this.handleJobCompleted(job);
|
|
55
66
|
}
|
|
@@ -57,7 +68,10 @@ class Worker {
|
|
|
57
68
|
}
|
|
58
69
|
processError(error) {
|
|
59
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
this.getLogger().error("worker.job.unhandled_exception", {
|
|
71
|
+
this.getLogger().error("worker.job.unhandled_exception", {
|
|
72
|
+
queue: this.queueFullName,
|
|
73
|
+
error,
|
|
74
|
+
});
|
|
61
75
|
if (this.handleJobError) {
|
|
62
76
|
yield this.handleJobError(error);
|
|
63
77
|
}
|
|
@@ -66,12 +80,19 @@ class Worker {
|
|
|
66
80
|
run() {
|
|
67
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
82
|
try {
|
|
69
|
-
this.getLogger().info("worker.run.starting", {
|
|
83
|
+
this.getLogger().info("worker.run.starting", {
|
|
84
|
+
queue: this.queueFullName,
|
|
85
|
+
});
|
|
70
86
|
yield this.worker.run();
|
|
71
|
-
this.getLogger().info("worker.run.started", {
|
|
87
|
+
this.getLogger().info("worker.run.started", {
|
|
88
|
+
queue: this.queueFullName,
|
|
89
|
+
});
|
|
72
90
|
}
|
|
73
91
|
catch (error) {
|
|
74
|
-
this.getLogger().error("worker.run.error", {
|
|
92
|
+
this.getLogger().error("worker.run.error", {
|
|
93
|
+
queue: this.queueFullName,
|
|
94
|
+
error,
|
|
95
|
+
});
|
|
75
96
|
throw error;
|
|
76
97
|
}
|
|
77
98
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/worker/worker.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,4DAAyD;AACzD,oCAAkE;
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/worker/worker.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,4DAAyD;AACzD,oCAAkE;AAGlE,yDAAsD;AA4BtD,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB,MAAe,MAAM;IAOnB;QACE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QAExD,MAAM,iBAAiB,GAAG,qCAAiB,CAAC,WAAW,EAAE,CAAC;QAC1D,IAAI,CAAC,SAAS,GAAG,iBAAiB,CAAC,SAAS,CAAC;QAE7C,MAAM,MAAM,GAAG,iBAAiB,CAAC,cAAc,EAAE,CAAC;QAClD,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC;QACvD,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAS,CAAI,IAAI,EAAE,IAAI,CAAC,UAAU,EAAE,kBACrD,OAAO,EAAE,KAAK,EACd,UAAU,EAAE,iBAA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,CAAC,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC;QACzE,sGAAsG;QACtG,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAW,EAAE,KAAY,EAAE,EAAE,CACrD,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,CAAC,CAC/B,CAAC;IACJ,CAAC;IAED,IAAc,MAAM;QAClB,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,aAAa;QACf,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QAChD,OAAO,IAAI,MAAM,KAAK,SAAS,IAAI,IAAI,EAAE,CAAC;IAC5C,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IAC1B,CAAC;CACF;AAGQ,wBAAM"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.withContext = void 0;
|
|
16
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
17
|
+
const appIdentifier_1 = require("../helpers/appIdentifier");
|
|
18
|
+
function withContext(workerContext, executor) {
|
|
19
|
+
return (job) => {
|
|
20
|
+
const { lid, lch } = job.data;
|
|
21
|
+
return workerContext.sharedContext.run(() => __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return executor(job);
|
|
23
|
+
}), {
|
|
24
|
+
logger: workerContext.baseLogger,
|
|
25
|
+
audit: workerContext.audit.withState(),
|
|
26
|
+
lifecycleId: lid || (0, cuid_1.default)(),
|
|
27
|
+
lifecycleChain: lch || (0, appIdentifier_1.appIdentifier)(),
|
|
28
|
+
contextId: (0, cuid_1.default)(),
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.withContext = withContext;
|
|
33
|
+
//# sourceMappingURL=withContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withContext.js","sourceRoot":"","sources":["../../src/wrappers/withContext.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,gDAAwB;AACxB,4DAAyD;AAIzD,SAAgB,WAAW,CACzB,aAA4B,EAC5B,QAAwC;IAExC,OAAO,CAAC,GAAG,EAAE,EAAE;QACb,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,IAAsC,CAAC;QAChE,OAAO,aAAa,CAAC,aAAa,CAAC,GAAG,CACpC,GAAS,EAAE;YACT,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC,CAAA,EACD;YACE,MAAM,EAAE,aAAa,CAAC,UAAU;YAChC,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,SAAS,EAAE;YACtC,WAAW,EAAE,GAAG,IAAI,IAAA,cAAI,GAAE;YAC1B,cAAc,EAAE,GAAG,IAAI,IAAA,6BAAa,GAAE;YACtC,SAAS,EAAE,IAAA,cAAI,GAAE;SAClB,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAnBD,kCAmBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/queue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.2.3",
|
|
4
4
|
"description": "Workers and queues",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -25,10 +25,12 @@
|
|
|
25
25
|
"typescript": "^4.3.4"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@alanszp/
|
|
28
|
+
"@alanszp/audit": "^9.0.0",
|
|
29
|
+
"@alanszp/logger": "^9.0.0",
|
|
30
|
+
"@alanszp/shared-context": "^9.0.0",
|
|
29
31
|
"bullmq": "^2.1.2",
|
|
30
32
|
"ioredis": "^5.2.3",
|
|
31
33
|
"lodash": "^4.17.21"
|
|
32
34
|
},
|
|
33
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "dcb2a6f5d6489dbcb67197d26b8da716ea3c9c97"
|
|
34
36
|
}
|
package/src/index.ts
CHANGED
package/src/queue/createQueue.ts
CHANGED
|
@@ -2,10 +2,21 @@ import { Queue } from "./queue";
|
|
|
2
2
|
import { QueueRepository } from "./queueRepository";
|
|
3
3
|
import { ConnectionManager } from "../connectionManager";
|
|
4
4
|
import { JobData, QueueOptions } from "../types";
|
|
5
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
5
6
|
|
|
6
|
-
export function createQueue<JobType = JobData>(
|
|
7
|
+
export function createQueue<JobType = JobData>(
|
|
8
|
+
name: string,
|
|
9
|
+
getContext: () => SharedContext,
|
|
10
|
+
queueOptions?: QueueOptions
|
|
11
|
+
): Queue {
|
|
7
12
|
const connection = ConnectionManager.getInstance().getConnection();
|
|
8
|
-
const queue = new Queue<JobType>(
|
|
13
|
+
const queue = new Queue<JobType>(
|
|
14
|
+
connection,
|
|
15
|
+
name,
|
|
16
|
+
ConnectionManager.getInstance().getServiceName(),
|
|
17
|
+
getContext,
|
|
18
|
+
queueOptions
|
|
19
|
+
);
|
|
9
20
|
QueueRepository.Instance.registerQueue(queue);
|
|
10
21
|
return queue;
|
|
11
22
|
}
|
package/src/queue/queue.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { merge } from "lodash";
|
|
2
|
+
import { Job, JobsOptions } from "bullmq";
|
|
3
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
2
4
|
import { ConnectionOptions, JobData, QueueOptions, RawQueue } from "../types";
|
|
3
5
|
|
|
4
6
|
const BULL_PREFIX = "b";
|
|
@@ -11,9 +13,19 @@ export class Queue<JobType = JobData> {
|
|
|
11
13
|
|
|
12
14
|
private name: string;
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
private getSharedContext: () => SharedContext;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
connection: ConnectionOptions,
|
|
20
|
+
name: string,
|
|
21
|
+
prefix: string,
|
|
22
|
+
getSharedContext: () => SharedContext,
|
|
23
|
+
queueOptions?: QueueOptions
|
|
24
|
+
) {
|
|
15
25
|
this.name = name;
|
|
16
26
|
|
|
27
|
+
this.getSharedContext = getSharedContext;
|
|
28
|
+
|
|
17
29
|
this._queue = new RawQueue<JobType>(name, {
|
|
18
30
|
...merge(
|
|
19
31
|
{
|
|
@@ -36,12 +48,30 @@ export class Queue<JobType = JobData> {
|
|
|
36
48
|
});
|
|
37
49
|
}
|
|
38
50
|
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
public getName(): string {
|
|
52
|
+
return this.name;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async publishJob(job: JobType, opts?: JobsOptions): Promise<Job<JobType>> {
|
|
56
|
+
const context = this.getSharedContext();
|
|
57
|
+
const lid = context.getLifecycleId();
|
|
58
|
+
const lch = context.getLifecycleChain();
|
|
59
|
+
return this.queue.add(this.name, { ...job, lid, lch }, opts);
|
|
41
60
|
}
|
|
42
61
|
|
|
43
|
-
async publishBulkJob(jobDatas: JobType[]): Promise<
|
|
62
|
+
async publishBulkJob(jobDatas: JobType[]): Promise<Job<JobType>[]> {
|
|
44
63
|
const jobs = jobDatas.map((data) => ({ name: this.name, data }));
|
|
64
|
+
return this.queue.addBulk(jobs);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async publishBulkJobWithOptions(
|
|
68
|
+
jobDefinitions: { jobData: JobType; opts: JobsOptions }[]
|
|
69
|
+
): Promise<void> {
|
|
70
|
+
const jobs = jobDefinitions.map(({ jobData: data, opts }) => ({
|
|
71
|
+
name: this.name,
|
|
72
|
+
data,
|
|
73
|
+
opts,
|
|
74
|
+
}));
|
|
45
75
|
await this.queue.addBulk(jobs);
|
|
46
76
|
}
|
|
47
77
|
|
package/src/types.ts
CHANGED
package/src/worker/worker.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ILogger } from "@alanszp/logger";
|
|
2
2
|
import { ConnectionManager } from "../connectionManager";
|
|
3
3
|
import { JobData, Job, RawWorker, WorkerOptions } from "../types";
|
|
4
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
5
|
+
import { Audit } from "@alanszp/audit";
|
|
6
|
+
import { withContext } from "../wrappers/withContext";
|
|
4
7
|
|
|
5
8
|
export interface WorkerStatus {
|
|
6
9
|
running: boolean;
|
|
@@ -12,6 +15,12 @@ export interface WorkerSetup {
|
|
|
12
15
|
workerOptions?: WorkerOptions;
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
export interface WorkerContext {
|
|
19
|
+
sharedContext: SharedContext;
|
|
20
|
+
baseLogger: ILogger;
|
|
21
|
+
audit: Audit;
|
|
22
|
+
}
|
|
23
|
+
|
|
15
24
|
// optional methods
|
|
16
25
|
|
|
17
26
|
interface Worker<T = JobData> {
|
|
@@ -55,6 +64,8 @@ abstract class Worker<T = JobData> {
|
|
|
55
64
|
|
|
56
65
|
abstract setup(): WorkerSetup;
|
|
57
66
|
|
|
67
|
+
abstract getContext(): WorkerContext;
|
|
68
|
+
|
|
58
69
|
public close(): Promise<void> {
|
|
59
70
|
return this.worker.close();
|
|
60
71
|
}
|
|
@@ -68,28 +79,41 @@ abstract class Worker<T = JobData> {
|
|
|
68
79
|
}
|
|
69
80
|
|
|
70
81
|
private processJob(): (job: Job<T>) => Promise<void> {
|
|
71
|
-
return async (job) => {
|
|
72
|
-
this.getLogger().info(`worker.process.job_received`, {
|
|
82
|
+
return withContext(this.getContext(), async (job) => {
|
|
83
|
+
this.getLogger().info(`worker.process.job_received`, {
|
|
84
|
+
queue: this.queueFullName,
|
|
85
|
+
job,
|
|
86
|
+
});
|
|
73
87
|
await this.process(job);
|
|
74
|
-
};
|
|
88
|
+
});
|
|
75
89
|
}
|
|
76
90
|
|
|
77
91
|
async processFailed(job: Job<T>, error: Error): Promise<void> {
|
|
78
|
-
this.getLogger().warn("worker.job.failed", {
|
|
92
|
+
this.getLogger().warn("worker.job.failed", {
|
|
93
|
+
queue: this.queueFullName,
|
|
94
|
+
job,
|
|
95
|
+
error,
|
|
96
|
+
});
|
|
79
97
|
if (this.handleJobFailed) {
|
|
80
98
|
await this.handleJobFailed(job, error);
|
|
81
99
|
}
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
async processCompleted(job: Job<T>): Promise<void> {
|
|
85
|
-
this.getLogger().info("worker.job.completed", {
|
|
103
|
+
this.getLogger().info("worker.job.completed", {
|
|
104
|
+
queue: this.queueFullName,
|
|
105
|
+
job,
|
|
106
|
+
});
|
|
86
107
|
if (this.handleJobCompleted) {
|
|
87
108
|
await this.handleJobCompleted(job);
|
|
88
109
|
}
|
|
89
110
|
}
|
|
90
111
|
|
|
91
112
|
async processError(error: Error): Promise<void> {
|
|
92
|
-
this.getLogger().error("worker.job.unhandled_exception", {
|
|
113
|
+
this.getLogger().error("worker.job.unhandled_exception", {
|
|
114
|
+
queue: this.queueFullName,
|
|
115
|
+
error,
|
|
116
|
+
});
|
|
93
117
|
if (this.handleJobError) {
|
|
94
118
|
await this.handleJobError(error);
|
|
95
119
|
}
|
|
@@ -97,12 +121,19 @@ abstract class Worker<T = JobData> {
|
|
|
97
121
|
|
|
98
122
|
async run(): Promise<void> {
|
|
99
123
|
try {
|
|
100
|
-
this.getLogger().info("worker.run.starting", {
|
|
124
|
+
this.getLogger().info("worker.run.starting", {
|
|
125
|
+
queue: this.queueFullName,
|
|
126
|
+
});
|
|
101
127
|
|
|
102
128
|
await this.worker.run();
|
|
103
|
-
this.getLogger().info("worker.run.started", {
|
|
129
|
+
this.getLogger().info("worker.run.started", {
|
|
130
|
+
queue: this.queueFullName,
|
|
131
|
+
});
|
|
104
132
|
} catch (error: unknown) {
|
|
105
|
-
this.getLogger().error("worker.run.error", {
|
|
133
|
+
this.getLogger().error("worker.run.error", {
|
|
134
|
+
queue: this.queueFullName,
|
|
135
|
+
error,
|
|
136
|
+
});
|
|
106
137
|
throw error;
|
|
107
138
|
}
|
|
108
139
|
}
|
|
@@ -113,7 +144,9 @@ abstract class Worker<T = JobData> {
|
|
|
113
144
|
// on completed: allow to do something else after a job is completed
|
|
114
145
|
this.worker.on("completed", (job: Job<T>) => this.processCompleted(job));
|
|
115
146
|
// on failed: when the process fails with an exception it is possible to listen for the "failed" event
|
|
116
|
-
this.worker.on("failed", (job: Job<T>, error: Error) =>
|
|
147
|
+
this.worker.on("failed", (job: Job<T>, error: Error) =>
|
|
148
|
+
this.processFailed(job, error)
|
|
149
|
+
);
|
|
117
150
|
}
|
|
118
151
|
|
|
119
152
|
protected get worker(): RawWorker {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import cuid from "cuid";
|
|
2
|
+
import { appIdentifier } from "../helpers/appIdentifier";
|
|
3
|
+
import { Job, JobData } from "bullmq";
|
|
4
|
+
import { WorkerContext } from "../worker/worker";
|
|
5
|
+
|
|
6
|
+
export function withContext<T = JobData>(
|
|
7
|
+
workerContext: WorkerContext,
|
|
8
|
+
executor: (job: Job<T>) => Promise<void>
|
|
9
|
+
): (job: Job<T>) => Promise<void> {
|
|
10
|
+
return (job) => {
|
|
11
|
+
const { lid, lch } = job.data as { lid?: string; lch?: string };
|
|
12
|
+
return workerContext.sharedContext.run(
|
|
13
|
+
async () => {
|
|
14
|
+
return executor(job);
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
logger: workerContext.baseLogger,
|
|
18
|
+
audit: workerContext.audit.withState(),
|
|
19
|
+
lifecycleId: lid || cuid(),
|
|
20
|
+
lifecycleChain: lch || appIdentifier(),
|
|
21
|
+
contextId: cuid(),
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
}
|