tspace-mysql 1.9.2 → 1.9.3-beta.2
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 +142 -17
- package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +0 -1
- package/dist/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/dist/lib/core/Blueprint.d.ts +43 -10
- package/dist/lib/core/Blueprint.js +43 -2
- package/dist/lib/core/Blueprint.js.map +1 -1
- package/dist/lib/core/Builder.d.ts +5 -5
- package/dist/lib/core/Builder.js +15 -12
- package/dist/lib/core/Builder.js.map +1 -1
- package/dist/lib/core/Cache/MemoryCache.js +9 -4
- package/dist/lib/core/Cache/MemoryCache.js.map +1 -1
- package/dist/lib/core/Contracts/Audit.d.ts +2 -2
- package/dist/lib/core/Contracts/Logger.d.ts +2 -2
- package/dist/lib/core/Model.d.ts +169 -29
- package/dist/lib/core/Model.js +153 -4
- package/dist/lib/core/Model.js.map +1 -1
- package/dist/lib/core/{Queue.d.ts → Queue/index.d.ts} +115 -91
- package/dist/lib/core/Queue/index.js +282 -0
- package/dist/lib/core/Queue/index.js.map +1 -0
- package/dist/lib/core/Queue/types.d.ts +39 -0
- package/dist/lib/core/Queue/types.js +3 -0
- package/dist/lib/core/Queue/types.js.map +1 -0
- package/dist/lib/core/Queue/worker.d.ts +102 -0
- package/dist/lib/core/{Queue.js → Queue/worker.js} +133 -268
- package/dist/lib/core/Queue/worker.js.map +1 -0
- package/dist/lib/core/Repository.d.ts +40 -20
- package/dist/lib/core/Repository.js +35 -14
- package/dist/lib/core/Repository.js.map +1 -1
- package/dist/lib/core/UtilityTypes.d.ts +9 -7
- package/dist/lib/core/UtilityTypes.js.map +1 -1
- package/dist/lib/types/decorator/index.d.ts +3 -0
- package/dist/lib/types/index.d.ts +17 -16
- package/dist/lib/types/repository/index.d.ts +3 -3
- package/dist/lib/utils/index.d.ts +4 -2
- package/dist/lib/utils/index.js +17 -1
- package/dist/lib/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/lib/core/Queue.js.map +0 -1
|
@@ -1,90 +1,42 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { T } from "..";
|
|
2
|
+
import { Worker } from "./worker";
|
|
3
|
+
import type { EventJobName, EventName, Handler, JobName, JobStatus, QueueAddOptions, QueueProcessOptions } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a job managed by the queue.
|
|
6
|
+
*
|
|
7
|
+
* @template T The type of the job payload.
|
|
8
|
+
*/
|
|
4
9
|
export type Job<T = any> = {
|
|
10
|
+
/** Unique identifier of the job. */
|
|
5
11
|
id: number;
|
|
12
|
+
/** Name of the job. */
|
|
6
13
|
name: string;
|
|
7
|
-
|
|
14
|
+
/** Current status of the job. */
|
|
15
|
+
status: JobStatus;
|
|
16
|
+
/** Data associated with the job. */
|
|
8
17
|
payload: T;
|
|
9
18
|
};
|
|
10
|
-
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
attempts: Blueprint<T.Default<number>>;
|
|
32
|
-
max_attempts: Blueprint<T.Default<number>>;
|
|
33
|
-
delay_ms: Blueprint<T.Default<number>>;
|
|
34
|
-
locked_by: Blueprint<string | null>;
|
|
35
|
-
locked_at: Blueprint<string | Date | null>;
|
|
36
|
-
available_at: Blueprint<NonNullable<string | Date>>;
|
|
37
|
-
completed_at: Blueprint<string | Date | null>;
|
|
38
|
-
created_at: Blueprint<string | Date | null>;
|
|
39
|
-
updated_at: Blueprint<string | Date | null>;
|
|
40
|
-
};
|
|
41
|
-
type TS = T.Schema<typeof schema>;
|
|
42
|
-
declare class Worker extends Model<TS> {
|
|
43
|
-
private HOSTNAME;
|
|
44
|
-
private INSPECT_EXEC;
|
|
45
|
-
private STOPPING;
|
|
46
|
-
private IS_FLUSHING;
|
|
47
|
-
private LIMIT_CONNECTIONS;
|
|
48
|
-
private MAX_IDLE_RETRIES;
|
|
49
|
-
private ACTIVE_JOBS;
|
|
50
|
-
private BATCH_SIZE;
|
|
51
|
-
private MAX_WAIT_MS;
|
|
52
|
-
private BUFFER;
|
|
53
|
-
private WORKER_STATE;
|
|
54
|
-
protected boot(): void;
|
|
55
|
-
initialize(opts?: {
|
|
56
|
-
inspect?: boolean;
|
|
57
|
-
flush?: boolean;
|
|
58
|
-
hostname?: string;
|
|
59
|
-
maxIdleRetries?: number;
|
|
60
|
-
limitConnections?: number;
|
|
61
|
-
}): Promise<this>;
|
|
62
|
-
shutdown(): Promise<void>;
|
|
63
|
-
flush(): Promise<void>;
|
|
64
|
-
getJobOverallStats(name?: string): Promise<{
|
|
65
|
-
total: number;
|
|
66
|
-
completed: number;
|
|
67
|
-
active: number;
|
|
68
|
-
pending: number;
|
|
69
|
-
failed: number;
|
|
70
|
-
}>;
|
|
71
|
-
getJobStats(name?: string): Promise<{
|
|
72
|
-
name: string;
|
|
73
|
-
completed: number;
|
|
74
|
-
active: number;
|
|
75
|
-
pending: number;
|
|
76
|
-
failed: number;
|
|
77
|
-
}[]>;
|
|
78
|
-
getJobs(name?: string): Promise<T.Result<Worker, unknown>[]>;
|
|
79
|
-
getNames(): Promise<any[]>;
|
|
80
|
-
add(name: string, payload: any, opts?: QueueAddOptions): Promise<void>;
|
|
81
|
-
process(name: string, handler: Handler, opts?: QueueProcessOptions): Promise<void>;
|
|
82
|
-
private _runJob;
|
|
83
|
-
private _dequeueMany;
|
|
84
|
-
private _flushBuffer;
|
|
85
|
-
private _wakeWorker;
|
|
86
|
-
private safeJsonParse;
|
|
87
|
-
private safeJsonStringify;
|
|
19
|
+
/**
|
|
20
|
+
* Defines the type contract for queue events and jobs.
|
|
21
|
+
*
|
|
22
|
+
* This interface is intentionally empty and can be extended
|
|
23
|
+
* using TypeScript module augmentation to provide type-safe
|
|
24
|
+
* event and job names.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* declare module 'tspace-mysql' {
|
|
29
|
+
* interface QueueContract {
|
|
30
|
+
* events: {
|
|
31
|
+
* 'user.created': ['send-email', 'send-push'];
|
|
32
|
+
* };
|
|
33
|
+
*
|
|
34
|
+
* jobs: ['send-email', 'send-push'];
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export interface QueueContract {
|
|
88
40
|
}
|
|
89
41
|
/**
|
|
90
42
|
* Queue facade class (static API wrapper)
|
|
@@ -118,11 +70,13 @@ declare class Queue {
|
|
|
118
70
|
/**
|
|
119
71
|
* The 'start' method is used to initialize the Queue system.
|
|
120
72
|
* Creates and prepares the underlying Worker instance.
|
|
73
|
+
*
|
|
121
74
|
* @param {Object} [opts] - options (inspect, flush)
|
|
122
|
-
* @property {boolean} opts.inspect queue work flow
|
|
123
|
-
* @property {boolean} opts.flush remove all queue
|
|
124
|
-
* @property {number} opts.maxIdleRetries - Maximum idle time () when no jobs are available
|
|
125
|
-
* @property {
|
|
75
|
+
* @property {boolean?} opts.inspect queue work flow
|
|
76
|
+
* @property {boolean?} opts.flush remove all queue
|
|
77
|
+
* @property {number?} opts.maxIdleRetries - Maximum idle time () when no jobs are available
|
|
78
|
+
* @property {boolean} [opts.poll.enabled] - Enable or disable worker job polling.
|
|
79
|
+
* @property {number} [opts.poll.timeout] - Polling interval in milliseconds.
|
|
126
80
|
* @returns {Promise<void>}
|
|
127
81
|
*/
|
|
128
82
|
static start(opts?: {
|
|
@@ -130,7 +84,10 @@ declare class Queue {
|
|
|
130
84
|
flush?: boolean;
|
|
131
85
|
hostname?: string;
|
|
132
86
|
maxIdleRetries?: number;
|
|
133
|
-
|
|
87
|
+
poll?: {
|
|
88
|
+
enabled?: boolean;
|
|
89
|
+
timeout?: number;
|
|
90
|
+
};
|
|
134
91
|
}): Promise<void>;
|
|
135
92
|
/**
|
|
136
93
|
* The 'end' method is used to shutdown the Queue system.
|
|
@@ -211,7 +168,7 @@ declare class Queue {
|
|
|
211
168
|
* return await helloWorld(job)
|
|
212
169
|
* }, { concurrency : 3 });
|
|
213
170
|
*/
|
|
214
|
-
static process(name:
|
|
171
|
+
static process(name: JobName, handler: Handler, opts?: QueueProcessOptions): Promise<void>;
|
|
215
172
|
/**
|
|
216
173
|
* Start a worker for processing jobs of a specific name.
|
|
217
174
|
*
|
|
@@ -228,12 +185,12 @@ declare class Queue {
|
|
|
228
185
|
* return await helloWorld(job)
|
|
229
186
|
* }, { concurrency : 3 });
|
|
230
187
|
*/
|
|
231
|
-
static on(name:
|
|
188
|
+
static on(name: JobName, handler: Handler, opts?: QueueProcessOptions): Promise<void>;
|
|
232
189
|
/**
|
|
233
190
|
* Add a new job into the queue.
|
|
234
191
|
*
|
|
235
192
|
* @param {string} name - Queue name / job type.
|
|
236
|
-
* @param {any} payload - Job payload data
|
|
193
|
+
* @param {any} payload - Job payload data, send to process
|
|
237
194
|
* @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.)
|
|
238
195
|
* @throws {Error} If Queue is not initialized.
|
|
239
196
|
* @returns {Promise<T.Result<Worker>>}
|
|
@@ -241,9 +198,76 @@ declare class Queue {
|
|
|
241
198
|
* @example
|
|
242
199
|
* ```ts
|
|
243
200
|
* Queue.add("send-email", { email: "test@gmail.com" });
|
|
201
|
+
* Queue.add("send-email", { email: "test2@gmail.com" }, {
|
|
202
|
+
* metadata : 'first priority',
|
|
203
|
+
* priority : 9999
|
|
204
|
+
* delayMs : 100
|
|
205
|
+
* maxAttempts : 1
|
|
206
|
+
* });
|
|
244
207
|
* ```
|
|
245
208
|
*/
|
|
246
|
-
static add(name:
|
|
209
|
+
static add(name: JobName, payload: any, opts?: QueueAddOptions): Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* Publishes an event to all registered subscribers.
|
|
212
|
+
*
|
|
213
|
+
* Each subscriber receives the event payload as an individual job.
|
|
214
|
+
*
|
|
215
|
+
* @param event - The event name to publish.
|
|
216
|
+
* @param payload - The data associated with the event.
|
|
217
|
+
* @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.)
|
|
218
|
+
* @throws {Error} If the queue worker has not been initialized.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* Queue.publish('user.created', {
|
|
223
|
+
* userId: 123,
|
|
224
|
+
* email: 'user@example.com',
|
|
225
|
+
* });
|
|
226
|
+
*
|
|
227
|
+
* Queue.publish('user.created', {
|
|
228
|
+
* userId: 123,
|
|
229
|
+
* email: 'user@example.com',
|
|
230
|
+
* }, {
|
|
231
|
+
* metadata : 'first priority',
|
|
232
|
+
* priority : 9999
|
|
233
|
+
* delayMs : 100
|
|
234
|
+
* maxAttempts : 1
|
|
235
|
+
* });
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
static publish<E extends EventName>(event: E, payload: any, opts?: QueueAddOptions): Promise<void>;
|
|
239
|
+
/**
|
|
240
|
+
* Subscribes a handler to an event.
|
|
241
|
+
*
|
|
242
|
+
* The subscriber name is combined with the event name to create
|
|
243
|
+
* a unique job name in the format `{event}.{name}`.
|
|
244
|
+
*
|
|
245
|
+
* @param event - The event name to subscribe to.
|
|
246
|
+
* @param name - The unique name of the subscriber.
|
|
247
|
+
* @param handler - The function executed when the event is received.
|
|
248
|
+
* @param opts - Options controlling job processing behavior.
|
|
249
|
+
*
|
|
250
|
+
* @throws {Error} If the queue worker has not been initialized.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* await Queue.subscribe(
|
|
255
|
+
* 'user.created',
|
|
256
|
+
* 'email',
|
|
257
|
+
* async (job) => {
|
|
258
|
+
* await sendEmail(job.payload);
|
|
259
|
+
* },
|
|
260
|
+
* {
|
|
261
|
+
* concurrency: 5,
|
|
262
|
+
* }
|
|
263
|
+
* );
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* @remarks
|
|
267
|
+
* A subscription to `user.created` with the name `email`
|
|
268
|
+
* creates the job queue `user.created.email`.
|
|
269
|
+
*/
|
|
270
|
+
static subscribe<E extends EventName>(event: E, name: EventJobName<E>, handler: Handler, opts?: QueueProcessOptions): Promise<void>;
|
|
247
271
|
}
|
|
248
272
|
export { Queue };
|
|
249
273
|
export default Queue;
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queue = void 0;
|
|
4
|
+
const worker_1 = require("./worker");
|
|
5
|
+
/**
|
|
6
|
+
* Queue facade class (static API wrapper)
|
|
7
|
+
*
|
|
8
|
+
* This class provides a singleton-style interface over the underlying Worker instance.
|
|
9
|
+
* It must be initialized before use via `Queue.start()`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const sendEmail = (job) => console.log('send mail :' + job.payload.email)
|
|
14
|
+
*
|
|
15
|
+
* await Queue.start({ inspect : true, flush : true // **remove all jobs });
|
|
16
|
+
*
|
|
17
|
+
* // register
|
|
18
|
+
* Queue.progress("send-email", async (job) => {
|
|
19
|
+
* return await sendEmail(job);
|
|
20
|
+
* }, { concurrency : 3 });
|
|
21
|
+
*
|
|
22
|
+
* // add
|
|
23
|
+
* Queue.add("send-email", { email: "test@gmail.com" });
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class Queue {
|
|
28
|
+
/**
|
|
29
|
+
* Internal Worker instance used for all queue operations.
|
|
30
|
+
* @type {Worker | null}
|
|
31
|
+
*/
|
|
32
|
+
static WORKER;
|
|
33
|
+
static MESSAGE = {
|
|
34
|
+
INIT_ERROR: `Queue is not initialized. Please call 'await Queue.start()' before using it.`
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The 'start' method is used to initialize the Queue system.
|
|
38
|
+
* Creates and prepares the underlying Worker instance.
|
|
39
|
+
*
|
|
40
|
+
* @param {Object} [opts] - options (inspect, flush)
|
|
41
|
+
* @property {boolean?} opts.inspect queue work flow
|
|
42
|
+
* @property {boolean?} opts.flush remove all queue
|
|
43
|
+
* @property {number?} opts.maxIdleRetries - Maximum idle time () when no jobs are available
|
|
44
|
+
* @property {boolean} [opts.poll.enabled] - Enable or disable worker job polling.
|
|
45
|
+
* @property {number} [opts.poll.timeout] - Polling interval in milliseconds.
|
|
46
|
+
* @returns {Promise<void>}
|
|
47
|
+
*/
|
|
48
|
+
static async start(opts = {}) {
|
|
49
|
+
this.WORKER = await new worker_1.Worker().initialize(opts);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The 'end' method is used to shutdown the Queue system.
|
|
54
|
+
*
|
|
55
|
+
* @returns {Promise<void>}
|
|
56
|
+
*/
|
|
57
|
+
static async end() {
|
|
58
|
+
if (this.WORKER == null) {
|
|
59
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
60
|
+
}
|
|
61
|
+
await this.WORKER.shutdown();
|
|
62
|
+
this.WORKER = null;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The 'flush' method is used to flush all jobs in the queue (dangerous operation).
|
|
67
|
+
*
|
|
68
|
+
* @throws {Error} If Queue is not initialized.
|
|
69
|
+
* @returns {Promise<void>}
|
|
70
|
+
*/
|
|
71
|
+
static async flush() {
|
|
72
|
+
if (this.WORKER == null) {
|
|
73
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
74
|
+
}
|
|
75
|
+
await this.WORKER.flush();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The 'getJobOverallStats' method is used to get aggregated queue statistics.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} [name] - Optional queue name filter.
|
|
81
|
+
* @throws {Error} If Queue is not initialized.
|
|
82
|
+
* @returns {Promise<any>}
|
|
83
|
+
*/
|
|
84
|
+
static async getJobOverallStats(name) {
|
|
85
|
+
if (this.WORKER == null) {
|
|
86
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
87
|
+
}
|
|
88
|
+
return await this.WORKER.getJobOverallStats(name);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The 'getJobStats' method is used to Get jobs statistics grouped by name.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} [name] - Optional queue name filter.
|
|
94
|
+
* @throws {Error} If Queue is not initialized.
|
|
95
|
+
* @returns {Promise<any>}
|
|
96
|
+
*/
|
|
97
|
+
static async getJobStats(name) {
|
|
98
|
+
if (this.WORKER == null) {
|
|
99
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
100
|
+
}
|
|
101
|
+
return await this.WORKER.getJobStats(name);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The 'getJobs' method is used to Get jobs.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} [name] - Optional queue name filter.
|
|
107
|
+
* @throws {Error} If Queue is not initialized.
|
|
108
|
+
* @returns {Promise<T.Result<Worker>[]>}
|
|
109
|
+
*/
|
|
110
|
+
static async getJobs(name) {
|
|
111
|
+
if (this.WORKER == null) {
|
|
112
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
113
|
+
}
|
|
114
|
+
return await this.WORKER.getJobs(name);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get all unique queue names.
|
|
118
|
+
*
|
|
119
|
+
* @throws {Error} If Queue is not initialized.
|
|
120
|
+
* @returns {Promise<string[]>}
|
|
121
|
+
*/
|
|
122
|
+
static async getNames() {
|
|
123
|
+
if (this.WORKER == null) {
|
|
124
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
125
|
+
}
|
|
126
|
+
return await this.WORKER.getNames();
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Access raw Worker instance safely.
|
|
130
|
+
*
|
|
131
|
+
* @param {(worker: Worker) => any} cb - Callback with Worker instance.
|
|
132
|
+
* @throws {Error} If Queue is not initialized.
|
|
133
|
+
* @returns {Promise<Work>}
|
|
134
|
+
*/
|
|
135
|
+
static async worker(cb) {
|
|
136
|
+
if (this.WORKER == null) {
|
|
137
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
138
|
+
}
|
|
139
|
+
return await cb(this.WORKER);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Start a worker for processing jobs of a specific name.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} name - Queue name to process.
|
|
145
|
+
* @param {Handler} handler - Job handler function.
|
|
146
|
+
* @param {QueueProcessOptions} [opts] - Job options (interval, concurrency)
|
|
147
|
+
* @throws {Error} If Queue is not initialized.
|
|
148
|
+
* @returns {Promise<void>}
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* const helloWorld = (job) => console.log('hello world :' + job.id);
|
|
152
|
+
*
|
|
153
|
+
* Queue.progress("hello", async (job) => {
|
|
154
|
+
* return await helloWorld(job)
|
|
155
|
+
* }, { concurrency : 3 });
|
|
156
|
+
*/
|
|
157
|
+
static async process(name, handler, opts = { interval: 1_000, concurrency: 1 }) {
|
|
158
|
+
if (this.WORKER == null) {
|
|
159
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
160
|
+
}
|
|
161
|
+
return await this.WORKER.process(name, handler, opts);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Start a worker for processing jobs of a specific name.
|
|
165
|
+
*
|
|
166
|
+
* @param {string} name - Queue name to process.
|
|
167
|
+
* @param {Handler} handler - Job handler function.
|
|
168
|
+
* @param {QueueProcessOptions} [opts] - Job options (interval, concurrency)
|
|
169
|
+
* @throws {Error} If Queue is not initialized.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* const helloWorld = (job) => console.log('hello world :' + job.id);
|
|
174
|
+
*
|
|
175
|
+
* Queue.on("hello", async (job) => {
|
|
176
|
+
* return await helloWorld(job)
|
|
177
|
+
* }, { concurrency : 3 });
|
|
178
|
+
*/
|
|
179
|
+
static async on(name, handler, opts = { interval: 1_000, concurrency: 1 }) {
|
|
180
|
+
return await this.process(name, handler, opts);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Add a new job into the queue.
|
|
184
|
+
*
|
|
185
|
+
* @param {string} name - Queue name / job type.
|
|
186
|
+
* @param {any} payload - Job payload data, send to process
|
|
187
|
+
* @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.)
|
|
188
|
+
* @throws {Error} If Queue is not initialized.
|
|
189
|
+
* @returns {Promise<T.Result<Worker>>}
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* Queue.add("send-email", { email: "test@gmail.com" });
|
|
194
|
+
* Queue.add("send-email", { email: "test2@gmail.com" }, {
|
|
195
|
+
* metadata : 'first priority',
|
|
196
|
+
* priority : 9999
|
|
197
|
+
* delayMs : 100
|
|
198
|
+
* maxAttempts : 1
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
static async add(name, payload, opts = {}) {
|
|
203
|
+
if (this.WORKER == null) {
|
|
204
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
205
|
+
}
|
|
206
|
+
return await this.WORKER.add(name, payload, opts);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Publishes an event to all registered subscribers.
|
|
210
|
+
*
|
|
211
|
+
* Each subscriber receives the event payload as an individual job.
|
|
212
|
+
*
|
|
213
|
+
* @param event - The event name to publish.
|
|
214
|
+
* @param payload - The data associated with the event.
|
|
215
|
+
* @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.)
|
|
216
|
+
* @throws {Error} If the queue worker has not been initialized.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* Queue.publish('user.created', {
|
|
221
|
+
* userId: 123,
|
|
222
|
+
* email: 'user@example.com',
|
|
223
|
+
* });
|
|
224
|
+
*
|
|
225
|
+
* Queue.publish('user.created', {
|
|
226
|
+
* userId: 123,
|
|
227
|
+
* email: 'user@example.com',
|
|
228
|
+
* }, {
|
|
229
|
+
* metadata : 'first priority',
|
|
230
|
+
* priority : 9999
|
|
231
|
+
* delayMs : 100
|
|
232
|
+
* maxAttempts : 1
|
|
233
|
+
* });
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
static async publish(event, payload, opts = {}) {
|
|
237
|
+
if (this.WORKER == null) {
|
|
238
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
239
|
+
}
|
|
240
|
+
return await this.WORKER.publish(event, payload, opts);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Subscribes a handler to an event.
|
|
244
|
+
*
|
|
245
|
+
* The subscriber name is combined with the event name to create
|
|
246
|
+
* a unique job name in the format `{event}.{name}`.
|
|
247
|
+
*
|
|
248
|
+
* @param event - The event name to subscribe to.
|
|
249
|
+
* @param name - The unique name of the subscriber.
|
|
250
|
+
* @param handler - The function executed when the event is received.
|
|
251
|
+
* @param opts - Options controlling job processing behavior.
|
|
252
|
+
*
|
|
253
|
+
* @throws {Error} If the queue worker has not been initialized.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* await Queue.subscribe(
|
|
258
|
+
* 'user.created',
|
|
259
|
+
* 'email',
|
|
260
|
+
* async (job) => {
|
|
261
|
+
* await sendEmail(job.payload);
|
|
262
|
+
* },
|
|
263
|
+
* {
|
|
264
|
+
* concurrency: 5,
|
|
265
|
+
* }
|
|
266
|
+
* );
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @remarks
|
|
270
|
+
* A subscription to `user.created` with the name `email`
|
|
271
|
+
* creates the job queue `user.created.email`.
|
|
272
|
+
*/
|
|
273
|
+
static async subscribe(event, name, handler, opts = { interval: 1_000, concurrency: 1 }) {
|
|
274
|
+
if (this.WORKER == null) {
|
|
275
|
+
throw new Error(this.MESSAGE.INIT_ERROR);
|
|
276
|
+
}
|
|
277
|
+
return await this.WORKER.subscribe(event, name, handler, opts);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
exports.Queue = Queue;
|
|
281
|
+
exports.default = Queue;
|
|
282
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/lib/core/Queue/index.ts"],"names":[],"mappings":";;;AACA,qCAAkC;AAqDlC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,KAAK;IACP;;;OAGG;IACK,MAAM,CAAC,MAAM,CAAgB;IAE7B,MAAM,CAAC,OAAO,GAAG;QACrB,UAAU,EAAE,8EAA8E;KAC7F,CAAC;IAEF;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OASf,EAAE;QAEJ,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,eAAM,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAElD,OAAO;IACT,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,GAAG;QAEd,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC3C,CAAC;QAED,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;QAE7B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QAEnB,OAAO;IACT,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK;QAEd,IAAI,IAAI,CAAC,MAAM,IAAI,IA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,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,MAAM,CAAC,KAAK,CAAC,OAAO,CAGhB,KAAW,EACX,OAAa,EACb,OAA4B,EAAE;QAG9B,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAC,OAAO,EAAC,IAAI,CAAC,CAAC;IACzD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,MAAM,CAAC,KAAK,CAAC,SAAS,CAGlB,KAAW,EACX,IAAyB,EACzB,OAAiB,EACjB,OAAgC,EAAE,QAAQ,EAAG,KAAK,EAAE,WAAW,EAAG,CAAC,EAAE;QAGrE,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAC,IAAI,EAAC,OAAO,EAAC,IAAI,CAAC,CAAC;IAChE,CAAC;;AAGI,sBAAK;AACd,kBAAe,KAAK,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Job, QueueContract, T } from "..";
|
|
2
|
+
import { Worker } from "./worker";
|
|
3
|
+
export type JobInternal = Job & {
|
|
4
|
+
__job: T.Result<Worker>;
|
|
5
|
+
};
|
|
6
|
+
export type Handler = (job: Job) => any | Promise<any>;
|
|
7
|
+
export type State = {
|
|
8
|
+
handler: Handler;
|
|
9
|
+
idle: number;
|
|
10
|
+
sleeping: boolean;
|
|
11
|
+
running: number;
|
|
12
|
+
opts: Required<QueueProcessOptions>;
|
|
13
|
+
};
|
|
14
|
+
export type QueueAddOptions = {
|
|
15
|
+
delayMs?: number;
|
|
16
|
+
priority?: number;
|
|
17
|
+
metadata?: Record<string, any>;
|
|
18
|
+
maxAttempts?: number;
|
|
19
|
+
};
|
|
20
|
+
export type QueueProcessOptions = {
|
|
21
|
+
interval?: number;
|
|
22
|
+
concurrency?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 20 | 25 | 30;
|
|
23
|
+
};
|
|
24
|
+
export type BufferedJob = {
|
|
25
|
+
jobData: T.Result<Worker>;
|
|
26
|
+
resolve: (value: any) => void;
|
|
27
|
+
reject: (reason?: any) => void;
|
|
28
|
+
};
|
|
29
|
+
export type JobStatus = 'pending' | 'active' | 'completed' | 'failed';
|
|
30
|
+
type QueueEvents = QueueContract extends {
|
|
31
|
+
events: infer E;
|
|
32
|
+
} ? E : never;
|
|
33
|
+
type QueueJobs = QueueContract extends {
|
|
34
|
+
jobs: infer J;
|
|
35
|
+
} ? J : never;
|
|
36
|
+
export type EventName = QueueEvents extends Record<string, readonly string[]> ? keyof QueueEvents & string : string;
|
|
37
|
+
export type EventJobName<E extends EventName> = QueueEvents extends Record<string, readonly string[]> ? E extends keyof QueueEvents ? QueueEvents[E][number] extends never ? string : QueueEvents[E][number] : string : string;
|
|
38
|
+
export type JobName = QueueJobs extends readonly string[] ? QueueJobs[number] : string;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/lib/core/Queue/types.ts"],"names":[],"mappings":""}
|