-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueueOperations.js
More file actions
executable file
·218 lines (182 loc) · 7.82 KB
/
Copy pathqueueOperations.js
File metadata and controls
executable file
·218 lines (182 loc) · 7.82 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const fs = require('fs');
const { ServiceBusClient, ServiceBusAdministrationClient } = require("@azure/service-bus");
const BuildCommandBatchesFromJson = require("./BuildCommandsFromJson.js");
const _uniqBy = require("lodash.uniqby");
const buildOperationOptions = ({ operationArgs }) => {
let operationOptions = {};
for (let i = 0; i < operationArgs.length; i = i + 2) {
let option = operationArgs[i];
let value = operationArgs[i + 1];
operationOptions[option] = JSON.parse(value);
}
return operationOptions;
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
class QueueOperations {
constructor(serviceBusConnectionString) {
this.serviceBusAdministrationClient = new ServiceBusAdministrationClient(serviceBusConnectionString);
this.serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
}
async Monitor({ operationArgs }){
const operationOptions = buildOperationOptions({ operationArgs });
const refreshSpeed = operationOptions.refresh ? (operationOptions.refresh * 1000) : 2000;
const argsForMonitor = ["showQueueCounts", "true", "streamMode", "true"];
do{
const queueDetails = await this.ListQueues({operationArgs: argsForMonitor});
console.clear();
this._outPutDataToScreen({dataToShowOnScreen: queueDetails});
await sleep(refreshSpeed);
}while(true);
}
async ListQueues({ operationArgs }) {
const operationOptions = buildOperationOptions({ operationArgs });
const queuesIterator = await this.serviceBusAdministrationClient.listQueuesRuntimeProperties();
const streamMode = operationOptions.streamMode ? operationOptions.streamMode : false;
const queueDetailsToPrint = [];
for await (const q of queuesIterator) {
let queueDetails = `${q.name}`
if (operationOptions.showQueueCounts) {
queueDetails = ` ${queueDetails}: active: ${q.activeMessageCount} | DLQ: ${q.deadLetterMessageCount}`;
}
queueDetailsToPrint.push(queueDetails);
}
if(streamMode){
return queueDetailsToPrint;
}
this._outPutDataToScreen({dataToShowOnScreen: queueDetailsToPrint});
return queueDetailsToPrint;
}
async SendMessages({ operationArgs }) {
const [queueId, filePathToMessages] = operationArgs
const operationOptions = buildOperationOptions({ operationArgs: operationArgs.slice(2) });
const delayBetweenSends = operationOptions.delayBetweenSends ? (operationOptions.delayBetweenSends * 1000) : 10000;
let messagesToSend = {};
try {
if (fs.existsSync(filePathToMessages)) {
let fileData = fs.readFileSync(filePathToMessages);
messagesToSend = JSON.parse(fileData);
}
} catch (err) {
console.log(`Could not find file ${filePathToMessages}`);
return;
}
const commandsFromFiles = await BuildCommandBatchesFromJson({ messagesToSend, queueName: queueId });
console.log("sending messages");
for (let i = 0; i < commandsFromFiles.length; i = i + 1) {
const commandsInstructions = commandsFromFiles[i];
await this._sendCommandBatches({ commandsInstructions });
console.log(`Chunk ${i + 1} of ${commandsFromFiles.length} sent`)
await sleep(delayBetweenSends);
}
}
async PeekMessageQueue({ operationArgs }) {
const [queueId] = operationArgs
const operationOptions = buildOperationOptions({ operationArgs: operationArgs.slice(1) });
const numberOfMessages = operationOptions.numberOfMessages ? operationOptions.numberOfMessages : 10;
const messagesFromIndex = operationOptions.messagesFromIndex ? operationOptions.messagesFromIndex : 0;
const submessageQueue = operationOptions.subMesageQueue ? operationOptions.subMesageQueue : "deadLetter";
const formatForResend = operationOptions.formatForResend ? operationOptions.formatForResend : false;
const filterSubject = operationOptions.filterSubject ? operationOptions.filterSubject : false;
const outputToFile = operationOptions.outputToFile;
const messagesInSubqueue = [];
const receiver = this.serviceBusClient.createReceiver(queueId, {
receiveMode: "peekLock",
subQueueType: submessageQueue
});
const messages = await this._getMessagesFromSubQueue({ receiver, numberOfMessages, messagesFromIndex })
for (let i = 0; i < messages.length; i = i + 1) {
const message = messages[i];
if(filterSubject !== false) {
const {subject} = message;
if(subject.includes(filterSubject) === false){
continue;
}
}
messagesInSubqueue.push(message);
}
const outputObject = {};
outputObject[queueId] = {}
outputObject[queueId][submessageQueue] = messagesInSubqueue;
console.log(JSON.stringify(outputObject, null, 4));
if (outputToFile) {
try {
let dataToFileWrite = formatForResend ? this._formatForResend({ messagesForFormatting: messagesInSubqueue }) : outputObject;
fs.writeFileSync(outputToFile, JSON.stringify(dataToFileWrite, null, 4));
} catch (err) {
console.log(`coudld not output data: ${err}`);
}
}
}
_outPutDataToScreen({dataToShowOnScreen}){
for(let i = 0; i < dataToShowOnScreen.length; i = i + 1){
const dataScreenLine = dataToShowOnScreen[i];
console.log(dataScreenLine);
}
}
async _getMessagesFromSubQueue({ receiver, numberOfMessages, messagesFromIndex }) {
let messages = [];
let peekBatch = [];
// this is goofy because it does not work the way the docs describe it and M$ has no examples.
// https://docs.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusreceiver?view=azure-node-latest#peekMessages_number__PeekMessagesOptions_
do {
peekBatch = await receiver.peekMessages(numberOfMessages, {
messagesFromIndex
});
messages = messages.concat(peekBatch);
} while (messages.length < numberOfMessages && peekBatch.length !== 0);
messages = _uniqBy(messages, 'messageId');
return messages;
}
_formatForResend({ messagesForFormatting }) {
let resendableOutPut = {
messages: []
}
for (let i = 0; i < messagesForFormatting.length; i = i + 1) {
const { body, subject} = messagesForFormatting[i];
const resendMessage = {
commandSubject: subject,
commandBody: body
};
resendableOutPut.messages.push(resendMessage);
}
return resendableOutPut;
}
async _sendCommandBatches({ commandsInstructions }) {
const { queueName, commandsToSend } = commandsInstructions;
const sender = this.serviceBusClient.createSender(queueName);
try {
// Tries to send all messages in a single batch.
// Will fail if the messages cannot fit in a batch.
// await sender.sendMessages(messages);
// create a batch object
let batch = await sender.createMessageBatch();
for (let i = 0; i < commandsToSend.length; i++) {
console.log()
// for each message in the array
// try to add the message to the batch
if (!batch.tryAddMessage(commandsToSend[i])) {
// if it fails to add the message to the current batch
// send the current batch as it is full
await sender.sendMessages(batch);
// then, create a new batch
batch = await sender.createMessageBatch();
// now, add the message failed to be added to the previous batch to this batch
if (!batch.tryAddMessage(commandsToSend[i])) {
// if it still can't be added to the batch, the message is probably too big to fit in a batch
throw new Error("Message too big to fit in a batch");
}
}
}
// Send the last created batch of messages to the queue
await sender.sendMessages(batch);
await sender.close();
} catch (err) {
console.log(`Error Sending data to ${queueName}: ${err}`);
}
}
}
module.exports = QueueOperations;