@autofleet/rabbit 3.2.21-beta.0.1 → 3.2.21-beta.0.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/index.d.ts +2 -1
- package/dist/index.js +3 -41
- package/dist/lib/celery.d.ts +5 -0
- package/dist/lib/celery.js +54 -0
- package/package.json +1 -1
- package/src/index.ts +6 -54
- package/src/lib/celery.ts +78 -0
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-c
|
|
|
4
4
|
import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
|
|
5
5
|
import { RedisConfig } from './lib/redis';
|
|
6
6
|
import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary, AssertExchangePromisesDictionary } from './lib/types';
|
|
7
|
+
import { sendCeleryTaskViaHttp } from './lib/celery';
|
|
7
8
|
export interface IAfRabbitMq {
|
|
8
9
|
ack: any;
|
|
9
10
|
nack: any;
|
|
@@ -94,8 +95,8 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
94
95
|
consumeFromExchange(queue: string, exchange: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
|
|
95
96
|
publish(exchange: string, content: any, customHeaders?: any): Promise<boolean>;
|
|
96
97
|
sendToQueue(queue: string, content: any, options?: any, customHeaders?: any): Promise<boolean | undefined>;
|
|
97
|
-
sendToCeleryQueue(queue: string, taskName: string, data: any, options?: any, customHeaders?: any): Promise<boolean | undefined>;
|
|
98
98
|
isConnected(): Promise<boolean>;
|
|
99
99
|
gracefulShutdown(signal: string): Promise<void>;
|
|
100
100
|
}
|
|
101
101
|
export default RabbitMq;
|
|
102
|
+
export { sendCeleryTaskViaHttp, };
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.sendCeleryTaskViaHttp = void 0;
|
|
7
8
|
const events_1 = require("events");
|
|
8
9
|
const util_1 = require("util");
|
|
9
10
|
const moment_1 = __importDefault(require("moment"));
|
|
@@ -17,6 +18,8 @@ const redis_1 = __importDefault(require("./lib/redis"));
|
|
|
17
18
|
const utils_1 = require("./lib/utils");
|
|
18
19
|
const consts_1 = require("./lib/consts");
|
|
19
20
|
const types_1 = require("./lib/types");
|
|
21
|
+
const celery_1 = require("./lib/celery");
|
|
22
|
+
Object.defineProperty(exports, "sendCeleryTaskViaHttp", { enumerable: true, get: function () { return celery_1.sendCeleryTaskViaHttp; } });
|
|
20
23
|
// const debug = nodeDebug('af-rabbitmq')
|
|
21
24
|
const debug = logger_1.default.debug.bind(logger_1.default);
|
|
22
25
|
const PUBLISH_TIMEOUT = 1000 * 10;
|
|
@@ -505,47 +508,6 @@ class RabbitMq {
|
|
|
505
508
|
throw e;
|
|
506
509
|
}
|
|
507
510
|
}
|
|
508
|
-
async sendToCeleryQueue(queue, taskName, data, options, customHeaders) {
|
|
509
|
-
try {
|
|
510
|
-
await this.assertChannel();
|
|
511
|
-
}
|
|
512
|
-
catch (e) {
|
|
513
|
-
logger_1.default.error(`rabbit sendToCeleryQueue: failed to assert channel when sending to queue ${queue}`, { e });
|
|
514
|
-
throw e;
|
|
515
|
-
}
|
|
516
|
-
try {
|
|
517
|
-
RabbitMq.validateName('queue', queue);
|
|
518
|
-
await this.assertQueue(queue, options);
|
|
519
|
-
}
|
|
520
|
-
catch (e) {
|
|
521
|
-
logger_1.default.error(`rabbit sendToCeleryQueue: failed to assert queue ${queue}`, { e });
|
|
522
|
-
throw e;
|
|
523
|
-
}
|
|
524
|
-
try {
|
|
525
|
-
// Create Celery-compatible message format
|
|
526
|
-
const celeryMessage = {
|
|
527
|
-
task: taskName,
|
|
528
|
-
id: (0, node_crypto_1.randomUUID)(),
|
|
529
|
-
args: [data],
|
|
530
|
-
};
|
|
531
|
-
const celeryHeaders = {
|
|
532
|
-
...RabbitMq.getPublishOptions(customHeaders),
|
|
533
|
-
properties: {
|
|
534
|
-
delivery_mode: 2,
|
|
535
|
-
content_type: 'application/json',
|
|
536
|
-
content_encoding: 'utf-8',
|
|
537
|
-
},
|
|
538
|
-
};
|
|
539
|
-
const res = await this.channel?.sendToQueue(queue, Buffer.from(JSON.stringify(celeryMessage)), celeryHeaders);
|
|
540
|
-
debug(`rabbit: sending to celery queue ${queue}`, { res });
|
|
541
|
-
return res;
|
|
542
|
-
}
|
|
543
|
-
catch (e) {
|
|
544
|
-
const isConnected = await this.isConnected();
|
|
545
|
-
logger_1.default.error(`rabbit sendToCeleryQueue: failed to send to queue ${queue}, isConnected: ${isConnected}`, { e });
|
|
546
|
-
throw e;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
511
|
async isConnected() {
|
|
550
512
|
const connection = await this.getConnection();
|
|
551
513
|
const isConnected = connection.isConnected();
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.sendCeleryTaskViaHttp = void 0;
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
9
|
+
// Environment configuration
|
|
10
|
+
const config = {
|
|
11
|
+
host: process.env.RABBITMQ_SERVICE_HOST || 'localhost',
|
|
12
|
+
username: process.env.RABBITMQ_USERNAME || 'guest',
|
|
13
|
+
password: process.env.RABBITMQ_PASSWORD || 'guest',
|
|
14
|
+
};
|
|
15
|
+
async function sendCeleryTaskViaHttp(data) {
|
|
16
|
+
const apiUrl = `http://${config.host}:15672/api/exchanges/%2f/amq.default/publish`;
|
|
17
|
+
const message = {
|
|
18
|
+
task: 'planning_tasks.process_planning_task',
|
|
19
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
20
|
+
args: [data],
|
|
21
|
+
};
|
|
22
|
+
const payload = {
|
|
23
|
+
properties: {
|
|
24
|
+
delivery_mode: 2,
|
|
25
|
+
content_type: 'application/json',
|
|
26
|
+
},
|
|
27
|
+
routing_key: 'planning-task',
|
|
28
|
+
payload: JSON.stringify(message),
|
|
29
|
+
payload_encoding: 'string',
|
|
30
|
+
};
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetch(apiUrl, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
'Content-Type': 'application/json',
|
|
36
|
+
Authorization: `Basic ${Buffer.from(`${config.username}:${config.password}`).toString('base64')}`,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify(payload),
|
|
39
|
+
});
|
|
40
|
+
if (response.ok) {
|
|
41
|
+
const result = await response.json();
|
|
42
|
+
logger_1.default.info('Successfully published message:', result);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
logger_1.default.error(`Failed to publish message. Status code: ${response.status}`);
|
|
46
|
+
logger_1.default.error(`Response: ${await response.text()}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
logger_1.default.error('Error sending request:', error instanceof Error ? error.message : String(error));
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.sendCeleryTaskViaHttp = sendCeleryTaskViaHttp;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -41,6 +41,8 @@ import {
|
|
|
41
41
|
AssertExchangePromisesDictionary,
|
|
42
42
|
} from './lib/types';
|
|
43
43
|
|
|
44
|
+
import { sendCeleryTaskViaHttp } from './lib/celery';
|
|
45
|
+
|
|
44
46
|
// const debug = nodeDebug('af-rabbitmq')
|
|
45
47
|
const debug = logger.debug.bind(logger);
|
|
46
48
|
|
|
@@ -697,60 +699,6 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
697
699
|
}
|
|
698
700
|
}
|
|
699
701
|
|
|
700
|
-
async sendToCeleryQueue(
|
|
701
|
-
queue: string,
|
|
702
|
-
taskName: string,
|
|
703
|
-
data: any,
|
|
704
|
-
options?: any,
|
|
705
|
-
customHeaders?: any,
|
|
706
|
-
): Promise<boolean | undefined> {
|
|
707
|
-
try {
|
|
708
|
-
await this.assertChannel();
|
|
709
|
-
} catch (e) {
|
|
710
|
-
logger.error(`rabbit sendToCeleryQueue: failed to assert channel when sending to queue ${queue}`, { e });
|
|
711
|
-
throw e;
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
try {
|
|
715
|
-
RabbitMq.validateName('queue', queue);
|
|
716
|
-
await this.assertQueue(queue, options);
|
|
717
|
-
} catch (e) {
|
|
718
|
-
logger.error(`rabbit sendToCeleryQueue: failed to assert queue ${queue}`, { e });
|
|
719
|
-
throw e;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
try {
|
|
723
|
-
// Create Celery-compatible message format
|
|
724
|
-
const celeryMessage = {
|
|
725
|
-
task: taskName,
|
|
726
|
-
id: randomUUID(),
|
|
727
|
-
args: [data],
|
|
728
|
-
};
|
|
729
|
-
|
|
730
|
-
const celeryHeaders = {
|
|
731
|
-
...RabbitMq.getPublishOptions(customHeaders),
|
|
732
|
-
properties: {
|
|
733
|
-
delivery_mode: 2,
|
|
734
|
-
content_type: 'application/json',
|
|
735
|
-
content_encoding: 'utf-8',
|
|
736
|
-
},
|
|
737
|
-
};
|
|
738
|
-
|
|
739
|
-
const res = await this.channel?.sendToQueue(
|
|
740
|
-
queue,
|
|
741
|
-
Buffer.from(JSON.stringify(celeryMessage)),
|
|
742
|
-
celeryHeaders,
|
|
743
|
-
);
|
|
744
|
-
|
|
745
|
-
debug(`rabbit: sending to celery queue ${queue}`, { res });
|
|
746
|
-
return res;
|
|
747
|
-
} catch (e) {
|
|
748
|
-
const isConnected = await this.isConnected();
|
|
749
|
-
logger.error(`rabbit sendToCeleryQueue: failed to send to queue ${queue}, isConnected: ${isConnected}`, { e });
|
|
750
|
-
throw e;
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
702
|
async isConnected() : Promise<boolean> {
|
|
755
703
|
const connection = await this.getConnection();
|
|
756
704
|
const isConnected = connection.isConnected();
|
|
@@ -789,3 +737,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
789
737
|
}
|
|
790
738
|
|
|
791
739
|
export default RabbitMq;
|
|
740
|
+
|
|
741
|
+
export {
|
|
742
|
+
sendCeleryTaskViaHttp,
|
|
743
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import logger from '../logger';
|
|
3
|
+
|
|
4
|
+
// Environment configuration
|
|
5
|
+
const config = {
|
|
6
|
+
host: process.env.RABBITMQ_SERVICE_HOST || 'localhost',
|
|
7
|
+
username: process.env.RABBITMQ_USERNAME || 'guest',
|
|
8
|
+
password: process.env.RABBITMQ_PASSWORD || 'guest',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// Type definitions
|
|
12
|
+
interface TaskData {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface TaskMessage {
|
|
17
|
+
task: string;
|
|
18
|
+
id: string;
|
|
19
|
+
args: TaskData[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface PublishPayload {
|
|
23
|
+
properties: {
|
|
24
|
+
delivery_mode: number;
|
|
25
|
+
content_type: string;
|
|
26
|
+
};
|
|
27
|
+
routing_key: string;
|
|
28
|
+
payload: string;
|
|
29
|
+
payload_encoding: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface PublishResponse {
|
|
33
|
+
routed: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function sendCeleryTaskViaHttp(data: TaskData): Promise<void> {
|
|
37
|
+
const apiUrl = `http://${config.host}:15672/api/exchanges/%2f/amq.default/publish`;
|
|
38
|
+
|
|
39
|
+
const message: TaskMessage = {
|
|
40
|
+
task: 'planning_tasks.process_planning_task',
|
|
41
|
+
id: randomUUID(),
|
|
42
|
+
args: [data],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const payload: PublishPayload = {
|
|
46
|
+
properties: {
|
|
47
|
+
delivery_mode: 2,
|
|
48
|
+
content_type: 'application/json',
|
|
49
|
+
},
|
|
50
|
+
routing_key: 'planning-task',
|
|
51
|
+
payload: JSON.stringify(message),
|
|
52
|
+
payload_encoding: 'string',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch(apiUrl, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
Authorization: `Basic ${Buffer.from(`${config.username}:${config.password}`).toString('base64')}`,
|
|
61
|
+
},
|
|
62
|
+
body: JSON.stringify(payload),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (response.ok) {
|
|
66
|
+
const result: PublishResponse = await response.json();
|
|
67
|
+
logger.info('Successfully published message:', result);
|
|
68
|
+
} else {
|
|
69
|
+
logger.error(`Failed to publish message. Status code: ${response.status}`);
|
|
70
|
+
logger.error(`Response: ${await response.text()}`);
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {
|
|
73
|
+
logger.error('Error sending request:', error instanceof Error ? error.message : String(error));
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { sendCeleryTaskViaHttp, TaskData };
|