@betterinternship/core 2.5.11 → 2.5.12
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,5 +1,8 @@
|
|
|
1
1
|
import { Task } from '@betterinternship/db';
|
|
2
2
|
import { JsonValue } from '@prisma/client/runtime/client';
|
|
3
|
+
export interface BaseJson {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
3
6
|
export declare class TaskRequest<TaskInputs extends JsonValue> implements Pick<Task, 'inputs' | 'internal_id' | 'handler_id' | 'parent_id' | 'process_id' | 'timestamp'> {
|
|
4
7
|
inputs: TaskInputs;
|
|
5
8
|
internal_id: string;
|
|
@@ -30,7 +33,8 @@ type TaskRequestHandler<TaskInputs extends JsonValue, TaskResult extends JsonVal
|
|
|
30
33
|
export declare class BaseTaskRunner<TaskInputs extends JsonValue, TaskResult extends JsonValue> {
|
|
31
34
|
private requestQueueId;
|
|
32
35
|
private responseQueueId;
|
|
36
|
+
private handler;
|
|
33
37
|
constructor(queueId: string, handler: TaskRequestHandler<TaskInputs, TaskResult>);
|
|
34
|
-
|
|
38
|
+
listen(): Promise<void>;
|
|
35
39
|
}
|
|
36
40
|
export {};
|
|
@@ -52,16 +52,16 @@ export class BaseTaskInitiator {
|
|
|
52
52
|
export class BaseTaskRunner {
|
|
53
53
|
requestQueueId;
|
|
54
54
|
responseQueueId;
|
|
55
|
+
handler;
|
|
55
56
|
constructor(queueId, handler) {
|
|
56
57
|
this.requestQueueId = `request.${queueId}`;
|
|
57
58
|
this.responseQueueId = `response.${queueId}`;
|
|
58
|
-
|
|
59
|
+
this.handler = handler;
|
|
59
60
|
}
|
|
60
|
-
async
|
|
61
|
-
const
|
|
62
|
-
await brokerAPI.addQueueHandler(this.requestQueueId, async (taskId) => {
|
|
61
|
+
async listen() {
|
|
62
|
+
const processTask = async (taskId) => {
|
|
63
63
|
const task = await tasks.findFirst({ where: { id: taskId } });
|
|
64
|
-
const response = await handler(task);
|
|
64
|
+
const response = await this.handler(task);
|
|
65
65
|
await tasks.update({
|
|
66
66
|
where: { id: taskId },
|
|
67
67
|
data: {
|
|
@@ -71,7 +71,9 @@ export class BaseTaskRunner {
|
|
|
71
71
|
},
|
|
72
72
|
});
|
|
73
73
|
await brokerAPI.addToQueue(this.responseQueueId, taskId);
|
|
74
|
-
}
|
|
74
|
+
};
|
|
75
|
+
const brokerAPI = await BrokerAPI.instance();
|
|
76
|
+
await brokerAPI.addQueueHandler(this.requestQueueId, processTask);
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
79
|
//# sourceMappingURL=base.task.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.task.js","sourceRoot":"","sources":["../../../lib/broker/base.task.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,KAAK,EAAQ,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"base.task.js","sourceRoot":"","sources":["../../../lib/broker/base.task.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,KAAK,EAAQ,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAO1B,MAAM,OAAO,WAAW;IAYtB,MAAM,CAAa;IACnB,WAAW,CAAS;IACpB,UAAU,CAAS;IACnB,SAAS,CAAgB;IACzB,UAAU,CAAS;IACnB,SAAS,CAAO;CACjB;AAED,MAAM,OAAO,YAAY;IAGvB,MAAM,CAAa;IACnB,KAAK,CAAS;IACd,MAAM,CAAS;CAChB;AAUD,MAAM,OAAO,iBAAiB;IAIpB,SAAS,CAAS;IAClB,cAAc,CAAS;IACvB,eAAe,CAAS;IAQhC,YAAY,OAAe,EAAE,OAAwC;QACnE,IAAI,CAAC,SAAS,GAAG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estBody {
|
|
4
|
-
taskId: string;
|
|
5
|
-
}
|
|
6
|
-
export declare class BrokerAPI {
|
|
7
|
-
connection: amqp.ChannelModel;
|
|
8
|
-
channels: Record<string, amqp.Channel>;
|
|
9
|
-
private static __instance;
|
|
10
|
-
private constructor();
|
|
11
|
-
static instance(): Promise<BrokerAPI | null>;
|
|
12
|
-
private assertQueue;
|
|
13
|
-
private addToQueueWithHeaders;
|
|
14
|
-
addToQueue(queueId: string, taskId: string, options?: {
|
|
15
|
-
retries?: number;
|
|
16
|
-
maxRetries?: number;
|
|
17
|
-
}): Promise<boolean>;
|
|
18
|
-
addQueueHandler<T>(queueId: string, handler: (taskId: string, resolve: () => void, reject: () => void) => Promise<T>): Promise<void>;
|
|
19
|
-
}
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
export declare const Q: (...args: string[]) => string;
|
|
3
|
+
export interface RequestBody {
|
|
4
|
+
taskId: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class BrokerAPI {
|
|
7
|
+
connection: amqp.ChannelModel;
|
|
8
|
+
channels: Record<string, amqp.Channel>;
|
|
9
|
+
private static __instance;
|
|
10
|
+
private constructor();
|
|
11
|
+
static instance(): Promise<BrokerAPI | null>;
|
|
12
|
+
private assertQueue;
|
|
13
|
+
private addToQueueWithHeaders;
|
|
14
|
+
addToQueue(queueId: string, taskId: string, options?: {
|
|
15
|
+
retries?: number;
|
|
16
|
+
maxRetries?: number;
|
|
17
|
+
}): Promise<boolean>;
|
|
18
|
+
addQueueHandler<T>(queueId: string, handler: (taskId: string, resolve: () => void, reject: () => void) => Promise<T>): Promise<void>;
|
|
19
|
+
}
|
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import * as amqp from 'amqplib';
|
|
2
|
-
import { ENV } from '../env.js';
|
|
3
|
-
const RABBITMQ_ENABLED = ENV.RABBITMQ_ENABLED;
|
|
4
|
-
const RABBITMQ_URL = ENV.RABBITMQ_URL;
|
|
5
|
-
if (!RABBITMQ_URL && RABBITMQ_ENABLED)
|
|
6
|
-
throw new Error('[ERROR:ENV] Missing RabbitMQ configuration.');
|
|
7
|
-
export const Q = (...args) => {
|
|
8
|
-
if (ENV.ENVIRONMENT === 'production')
|
|
9
|
-
return args.join('.');
|
|
10
|
-
else
|
|
11
|
-
return ['dev', ...args].join('.');
|
|
12
|
-
};
|
|
13
|
-
export class BrokerAPI {
|
|
14
|
-
connection;
|
|
15
|
-
channels;
|
|
16
|
-
static __instance = null;
|
|
17
|
-
constructor() { }
|
|
18
|
-
static async instance() {
|
|
19
|
-
if (!RABBITMQ_ENABLED)
|
|
20
|
-
return Promise.resolve(null);
|
|
21
|
-
if (BrokerAPI.__instance)
|
|
22
|
-
return Promise.resolve(BrokerAPI.__instance);
|
|
23
|
-
const instance = new BrokerAPI();
|
|
24
|
-
instance.connection = await amqp.connect(RABBITMQ_URL);
|
|
25
|
-
instance.channels = {
|
|
26
|
-
asserter: await instance.connection.createChannel(),
|
|
27
|
-
reader: await instance.connection.createChannel(),
|
|
28
|
-
writer: await instance.connection.createChannel(),
|
|
29
|
-
};
|
|
30
|
-
BrokerAPI.__instance = instance;
|
|
31
|
-
return instance;
|
|
32
|
-
}
|
|
33
|
-
async assertQueue(queueId, options) {
|
|
34
|
-
const result = await this.channels.asserter.assertQueue(queueId, {
|
|
35
|
-
durable: true,
|
|
36
|
-
...options,
|
|
37
|
-
});
|
|
38
|
-
console.log(`[BROKER] Queue "${queueId}" has been asserted.`);
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
async addToQueueWithHeaders(queueId, taskId, headers, options) {
|
|
42
|
-
await this.assertQueue(queueId);
|
|
43
|
-
const bodyStr = JSON.stringify({ taskId });
|
|
44
|
-
const result = this.channels.writer.sendToQueue(queueId, Buffer.from(bodyStr), {
|
|
45
|
-
persistent: true,
|
|
46
|
-
contentType: 'application/json',
|
|
47
|
-
headers: {
|
|
48
|
-
...headers,
|
|
49
|
-
'x-retries': options?.retries ?? 0,
|
|
50
|
-
'x-max-retries': options?.maxRetries ?? 3,
|
|
51
|
-
'x-first-published': Date.now(),
|
|
52
|
-
},
|
|
53
|
-
messageId: taskId,
|
|
54
|
-
});
|
|
55
|
-
if (result) {
|
|
56
|
-
console.log(`[BROKER] Message sent to queue "${queueId}": ${bodyStr}.`);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
console.log(`[BROKER] Message could not sent to queue "${queueId}": ${bodyStr}.`);
|
|
60
|
-
}
|
|
61
|
-
return Promise.resolve(result);
|
|
62
|
-
}
|
|
63
|
-
async addToQueue(queueId, taskId, options) {
|
|
64
|
-
await this.assertQueue(queueId);
|
|
65
|
-
return this.addToQueueWithHeaders(queueId, taskId, {}, options);
|
|
66
|
-
}
|
|
67
|
-
async addQueueHandler(queueId, handler) {
|
|
68
|
-
await this.assertQueue(queueId);
|
|
69
|
-
const resolve = (message) => () => {
|
|
70
|
-
this.channels.writer.ack(message);
|
|
71
|
-
};
|
|
72
|
-
const reject = (message) => () => {
|
|
73
|
-
const content = message?.content?.toString?.();
|
|
74
|
-
const request = JSON.parse(content);
|
|
75
|
-
const headers = message.properties.headers ?? {};
|
|
76
|
-
const retries = headers['x-retries'] ?? 0;
|
|
77
|
-
const maxRetries = headers['x-max-retries'] ?? 3;
|
|
78
|
-
if (retries >= maxRetries) {
|
|
79
|
-
this.channels.writer.ack(message);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
void this.addToQueueWithHeaders(queueId, request.taskId, headers, {
|
|
83
|
-
retries,
|
|
84
|
-
maxRetries,
|
|
85
|
-
}).then(() => this.channels.writer.ack(message));
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
await this.channels.reader.consume(queueId, (message) => {
|
|
89
|
-
const content = message?.content?.toString?.();
|
|
90
|
-
const request = JSON.parse(content);
|
|
91
|
-
void handler(request.taskId, resolve(message), reject(message));
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import { ENV } from '../env.js';
|
|
3
|
+
const RABBITMQ_ENABLED = ENV.RABBITMQ_ENABLED;
|
|
4
|
+
const RABBITMQ_URL = ENV.RABBITMQ_URL;
|
|
5
|
+
if (!RABBITMQ_URL && RABBITMQ_ENABLED)
|
|
6
|
+
throw new Error('[ERROR:ENV] Missing RabbitMQ configuration.');
|
|
7
|
+
export const Q = (...args) => {
|
|
8
|
+
if (ENV.ENVIRONMENT === 'production')
|
|
9
|
+
return args.join('.');
|
|
10
|
+
else
|
|
11
|
+
return ['dev', ...args].join('.');
|
|
12
|
+
};
|
|
13
|
+
export class BrokerAPI {
|
|
14
|
+
connection;
|
|
15
|
+
channels;
|
|
16
|
+
static __instance = null;
|
|
17
|
+
constructor() { }
|
|
18
|
+
static async instance() {
|
|
19
|
+
if (!RABBITMQ_ENABLED)
|
|
20
|
+
return Promise.resolve(null);
|
|
21
|
+
if (BrokerAPI.__instance)
|
|
22
|
+
return Promise.resolve(BrokerAPI.__instance);
|
|
23
|
+
const instance = new BrokerAPI();
|
|
24
|
+
instance.connection = await amqp.connect(RABBITMQ_URL);
|
|
25
|
+
instance.channels = {
|
|
26
|
+
asserter: await instance.connection.createChannel(),
|
|
27
|
+
reader: await instance.connection.createChannel(),
|
|
28
|
+
writer: await instance.connection.createChannel(),
|
|
29
|
+
};
|
|
30
|
+
BrokerAPI.__instance = instance;
|
|
31
|
+
return instance;
|
|
32
|
+
}
|
|
33
|
+
async assertQueue(queueId, options) {
|
|
34
|
+
const result = await this.channels.asserter.assertQueue(queueId, {
|
|
35
|
+
durable: true,
|
|
36
|
+
...options,
|
|
37
|
+
});
|
|
38
|
+
console.log(`[BROKER] Queue "${queueId}" has been asserted.`);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
async addToQueueWithHeaders(queueId, taskId, headers, options) {
|
|
42
|
+
await this.assertQueue(queueId);
|
|
43
|
+
const bodyStr = JSON.stringify({ taskId });
|
|
44
|
+
const result = this.channels.writer.sendToQueue(queueId, Buffer.from(bodyStr), {
|
|
45
|
+
persistent: true,
|
|
46
|
+
contentType: 'application/json',
|
|
47
|
+
headers: {
|
|
48
|
+
...headers,
|
|
49
|
+
'x-retries': options?.retries ?? 0,
|
|
50
|
+
'x-max-retries': options?.maxRetries ?? 3,
|
|
51
|
+
'x-first-published': Date.now(),
|
|
52
|
+
},
|
|
53
|
+
messageId: taskId,
|
|
54
|
+
});
|
|
55
|
+
if (result) {
|
|
56
|
+
console.log(`[BROKER] Message sent to queue "${queueId}": ${bodyStr}.`);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
console.log(`[BROKER] Message could not sent to queue "${queueId}": ${bodyStr}.`);
|
|
60
|
+
}
|
|
61
|
+
return Promise.resolve(result);
|
|
62
|
+
}
|
|
63
|
+
async addToQueue(queueId, taskId, options) {
|
|
64
|
+
await this.assertQueue(queueId);
|
|
65
|
+
return this.addToQueueWithHeaders(queueId, taskId, {}, options);
|
|
66
|
+
}
|
|
67
|
+
async addQueueHandler(queueId, handler) {
|
|
68
|
+
await this.assertQueue(queueId);
|
|
69
|
+
const resolve = (message) => () => {
|
|
70
|
+
this.channels.writer.ack(message);
|
|
71
|
+
};
|
|
72
|
+
const reject = (message) => () => {
|
|
73
|
+
const content = message?.content?.toString?.();
|
|
74
|
+
const request = JSON.parse(content);
|
|
75
|
+
const headers = message.properties.headers ?? {};
|
|
76
|
+
const retries = headers['x-retries'] ?? 0;
|
|
77
|
+
const maxRetries = headers['x-max-retries'] ?? 3;
|
|
78
|
+
if (retries >= maxRetries) {
|
|
79
|
+
this.channels.writer.ack(message);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
void this.addToQueueWithHeaders(queueId, request.taskId, headers, {
|
|
83
|
+
retries,
|
|
84
|
+
maxRetries,
|
|
85
|
+
}).then(() => this.channels.writer.ack(message));
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
await this.channels.reader.consume(queueId, (message) => {
|
|
89
|
+
const content = message?.content?.toString?.();
|
|
90
|
+
const request = JSON.parse(content);
|
|
91
|
+
void handler(request.taskId, resolve(message), reject(message));
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
95
|
//# sourceMappingURL=broker.js.map
|