-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrabbitmq.ts
More file actions
151 lines (137 loc) · 3.31 KB
/
rabbitmq.ts
File metadata and controls
151 lines (137 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import amqplib, { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
import { Options } from 'amqplib';
import debug from 'debug';
import HawkCatcher from '@hawk.so/nodejs';
const rabbitMQURL = process.env.AMQP_URL;
let channel: ChannelWrapper;
let connection: AmqpConnectionManager;
/**
* Rabbitmq exchanges
*/
export enum Exchanges {
Errors = 'errors',
Notify = 'notify',
Stash = 'stash',
Merchant = 'merchant',
CronTasks = 'cron-tasks',
Empty = '',
}
/**
* Rabbitmq queues
*/
export enum Queues {
Merchant = 'merchant/initialized',
Email = 'sender/email',
Telegram = 'notify/telegram',
Slack = 'notify/slack',
Loop = 'notify/loop',
Limiter = 'cron-tasks/limiter',
}
/**
* Contains a rabbitmq exchange and a queue for the worker
*/
export interface WorkerPath {
/**
* Rabbitmq exchange
*/
exchange: string;
/**
* Rabbitmq queue
*/
queue: string;
}
/**
* Paths for workers to send events to the queue
*/
export const WorkerPaths: Record<string, WorkerPath> = {
/**
* Path to merchant worker
*/
Merchant: {
exchange: Exchanges.Merchant,
queue: Queues.Merchant,
},
/**
* Path to email worker
*/
Email: {
exchange: Exchanges.Empty,
queue: Queues.Email,
},
/**
* Path to telegram worker
*/
Telegram: {
exchange: Exchanges.Notify,
queue: Queues.Telegram,
},
/**
* Path to slack worker
*/
Slack: {
exchange: Exchanges.Notify,
queue: Queues.Slack,
},
/**
* Path to loop worker
*/
Loop: {
exchange: Exchanges.Notify,
queue: Queues.Loop,
},
/**
* Path to limiter worker
*/
Limiter: {
exchange: Exchanges.CronTasks,
queue: Queues.Limiter,
},
};
/**
* Setups connection to the RabbitMQ
*/
export async function setupConnections(): Promise<void> {
if (rabbitMQURL) {
connection = amqplib.connect([ rabbitMQURL ]);
connection.on('connect', () => {
if (!channel) {
const channelWrapper = connection.createChannel({
setup: () => {
channel = channelWrapper;
console.log(`🔗AMQP channel connected: ${rabbitMQURL}`);
},
});
}
});
connection.on('error', (error) => {
HawkCatcher.send(error);
console.error(error);
});
connection.on('disconnect', () => console.log('💥AMQP disconnected. Trying to reconnect...'));
}
}
/**
* Send message to RabbitMQ queue
* @param exchange - exchange to publish message to
* @param route - route to publish message to
* @param message - message to publish
* @param options - rabbitmq task options
*/
export async function publish(exchange: string, route: string, message: string, options?: Options.Publish): Promise<void> {
try {
await channel.publish(exchange, route, Buffer.from(message), options);
debug(`Message sent: ${message}`);
} catch (err) {
HawkCatcher.send(err as Error);
console.log('Message was rejected:', (err as Error).stack);
}
}
/**
* Put a background task into the queue on rabbitmq
*
* @param workerPath - worker rabbitmq path: exchange and queue
* @param task - anything that we can stringify
*/
export async function enqueue(workerPath: WorkerPath, task: object, options?: Options.Publish): Promise<void> {
await publish(workerPath.exchange, workerPath.queue, JSON.stringify(task), options);
}