@natlibfi/melinda-rest-api-commons 2.3.2-alpha.6 → 2.3.2-alpha.8

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/amqp.js CHANGED
@@ -4,32 +4,23 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = _default;
7
-
8
7
  var _amqplib = _interopRequireDefault(require("amqplib"));
9
-
10
8
  var _marcRecord = require("@natlibfi/marc-record");
11
-
12
9
  var _melindaCommons = require("@natlibfi/melinda-commons");
13
-
14
10
  var _melindaBackendCommons = require("@natlibfi/melinda-backend-commons");
15
-
11
+ var _debug = _interopRequireDefault(require("debug"));
16
12
  var _constants = require("./constants");
17
-
18
- var _utils = require("./utils");
19
-
20
13
  var _util = require("util");
21
-
22
14
  var _httpStatus = _interopRequireDefault(require("http-status"));
23
-
15
+ var _utils = require("./utils");
24
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
17
  /**
27
18
  *
28
19
  * @licstart The following is the entire license notice for the JavaScript code in this file.
29
20
  *
30
21
  * Shared modules for microservices of Melinda rest api batch import system
31
22
  *
32
- * Copyright (C) 2020-2021 University Of Helsinki (The National Library Of Finland)
23
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
33
24
  *
34
25
  * This file is part of melinda-rest-api-commons
35
26
  *
@@ -50,10 +41,20 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
50
41
  * for the JavaScript code in this file.
51
42
  *
52
43
  */
53
- async function _default(AMQP_URL) {
44
+
45
+ //import {logError} from './utils';
46
+
47
+ async function _default(AMQP_URL, runHealthCheck = false) {
48
+ const logger = (0, _melindaBackendCommons.createLogger)();
49
+ const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:amqp');
50
+ const debugHC = debug.extend('HC');
51
+ debug(`Creating an AMQP operator to ${AMQP_URL}`);
54
52
  const connection = await _amqplib.default.connect(AMQP_URL);
55
53
  const channel = await connection.createChannel();
56
- const logger = (0, _melindaBackendCommons.createLogger)();
54
+ const healthCheckLoop = runHealthCheck ? healthCheck() : 'Not running health check';
55
+ debug(`Connection: ${connection}`);
56
+ debug(`Channel: ${channel}`);
57
+ debug(`HealthCheckLoop ${healthCheckLoop}`);
57
58
  return {
58
59
  checkQueue,
59
60
  consumeChunk,
@@ -62,82 +63,108 @@ async function _default(AMQP_URL) {
62
63
  nackMessages,
63
64
  sendToQueue,
64
65
  removeQueue,
65
- messagesToRecords
66
- }; // eslint-disable-next-line max-statements
66
+ messagesToRecords,
67
+ closeChannel,
68
+ closeConnection
69
+ };
70
+ async function closeChannel() {
71
+ debug(`Closing channel`);
72
+ await channel.close();
73
+ debug(`Channel: ${channel}`);
74
+ }
75
+ async function closeConnection() {
76
+ debug(`Closing connection`);
77
+ await connection.close();
78
+ debug(`Connection: ${connection}`);
79
+ }
80
+ async function healthCheck(wait = false) {
81
+ const setTimeoutPromise = (0, _util.promisify)(setTimeout);
82
+ if (wait) {
83
+ await setTimeoutPromise(wait);
84
+ return healthCheck(false);
85
+ }
86
+ try {
87
+ debugHC(`Health checking amqp by asserting a queue`);
88
+ await channel.assertQueue('HEALTHCHECK', {
89
+ durable: false
90
+ });
91
+ debugHC(`Waiting 200ms before running healthCheck next`);
92
+ return healthCheck('200');
93
+ } catch (error) {
94
+ const errorToThrow = error;
95
+ debugHC(`HealthCheck error ${JSON.stringify(error)}`);
96
+ handleAmqpErrors(errorToThrow);
97
+ }
98
+ }
67
99
 
100
+ // eslint-disable-next-line max-statements
68
101
  async function checkQueue({
69
102
  queue,
70
103
  style = 'basic',
71
104
  toRecord = true,
72
105
  purge = false
73
106
  }) {
74
- logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}`);
75
-
107
+ debug(`checkQueue: ${queue}, Style: ${style}, toRecord: ${toRecord}, Purge: ${purge}`);
76
108
  try {
77
109
  errorUndefinedQueue(queue);
78
110
  const channelInfo = await channel.assertQueue(queue, {
79
111
  durable: true
80
112
  });
81
-
82
113
  if (purge) {
83
114
  await purgeQueue(purge);
84
115
  logger.verbose(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);
116
+ debug(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);
85
117
  return checkQueue({
86
118
  queue,
87
119
  style
88
120
  });
89
121
  }
90
-
122
+ if (style === 'messages') {
123
+ return channelInfo.messageCount;
124
+ }
91
125
  if (channelInfo.messageCount < 1) {
92
- logger.silly(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);
126
+ debug(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);
93
127
  return false;
94
128
  }
129
+ debug(`Queue ${queue} has ${channelInfo.messageCount} messages`);
95
130
 
96
- logger.silly(`Queue ${queue} has ${channelInfo.messageCount} messages`);
97
-
98
- if (style === 'messages') {
99
- return channelInfo.messageCount;
100
- } // Note: returns one message (+ record, of toRecord: true)
131
+ // Note: returns one message (+ record, of toRecord: true)
101
132
  // note: if toRecord is false returns just plain message / false
102
133
  // note: if toRecord is true returns {headers, records, messages} -object / false
103
134
  // should this be more consistent?
104
-
105
-
106
135
  if (style === 'one') {
107
136
  return consumeOne(queue, toRecord);
108
- } // Note: returns a chunk of (100) messages (+ records, if toRecord: true)
109
- // returns {headers, records, messages} object or {headers, messages} object depending on toRecord
110
-
137
+ }
111
138
 
139
+ // Note: returns a chunk of (100) messages (+ records, if toRecord: true)
140
+ // returns {headers, records, messages} object or {headers, messages} object depending on toRecord
112
141
  if (style === 'basic') {
113
142
  return consumeChunk(queue, toRecord);
114
- } // Defaults:
115
-
143
+ }
116
144
 
145
+ // Defaults:
117
146
  throw new _melindaCommons.Error(_httpStatus.default.UNPROCESSABLE_ENTITY);
118
147
  } catch (error) {
119
- (0, _utils.logError)(error);
148
+ handleAmqpErrors(error);
120
149
  }
121
-
122
150
  function purgeQueue(purge) {
151
+ debug(`Purging queue: ${queue}, purge: ${purge}`);
123
152
  if (purge) {
124
153
  return channel.purgeQueue(queue);
125
154
  }
126
155
  }
127
156
  }
128
-
129
157
  async function consumeChunk(queue, toRecord) {
130
- logger.silly(`Prepared to consumeChunk from queue: ${queue}`);
131
-
158
+ debug(`Prepared to consumeChunk from queue: ${queue}`);
132
159
  try {
133
160
  await channel.assertQueue(queue, {
134
161
  durable: true
135
- }); // getData: next chunk (100) messages
162
+ });
136
163
 
164
+ // getData: next chunk (100) messages
137
165
  const messages = await getData(queue);
138
166
  const headers = getHeaderInfo(messages[0]);
139
- logger.debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);
140
-
167
+ debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);
141
168
  if (toRecord) {
142
169
  const records = await messagesToRecords(messages);
143
170
  return {
@@ -146,33 +173,30 @@ async function _default(AMQP_URL) {
146
173
  messages
147
174
  };
148
175
  }
149
-
150
176
  return {
151
177
  headers,
152
178
  messages
153
179
  };
154
180
  } catch (error) {
155
- (0, _utils.logError)(error);
181
+ handleAmqpErrors(error);
156
182
  }
157
183
  }
158
-
159
184
  async function consumeOne(queue, toRecord) {
160
- logger.silly(`Prepared to consumeOne from queue: ${queue}`);
161
-
185
+ debug(`Prepared to consumeOne from queue: ${queue}`);
162
186
  try {
163
187
  await channel.assertQueue(queue, {
164
188
  durable: true
165
- }); // Returns false if 0 items in queue
189
+ });
166
190
 
191
+ // Returns false if 0 items in queue
167
192
  const message = await channel.get(queue);
168
- logger.silly(`Message: ${(0, _util.inspect)(message, {
193
+ debug(`Message: ${(0, _util.inspect)(message, {
169
194
  colors: true,
170
195
  maxArrayLength: 3,
171
196
  depth: 3
172
- })}`); // Do not spam the logs
173
-
174
- logger.debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);
175
-
197
+ })}`);
198
+ // Do not spam the logs
199
+ debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);
176
200
  if (message) {
177
201
  if (toRecord) {
178
202
  const headers = getHeaderInfo(message);
@@ -183,127 +207,134 @@ async function _default(AMQP_URL) {
183
207
  messages: [message]
184
208
  };
185
209
  }
186
-
187
210
  return message;
188
211
  }
189
-
190
212
  return false;
191
213
  } catch (error) {
192
- (0, _utils.logError)(error);
214
+ handleAmqpErrors(error);
193
215
  }
194
216
  }
195
-
196
217
  function ackMessages(messages) {
197
218
  messages.forEach(message => {
198
- logger.silly(`Ack message ${message.properties.correlationId}`);
219
+ debug(`Ack message ${message.properties.correlationId}`);
199
220
  channel.ack(message);
200
221
  });
201
222
  }
202
-
203
223
  function nackMessages(messages) {
204
224
  messages.forEach(message => {
205
- logger.silly(`Nack message ${message.properties.correlationId}`);
225
+ debug(`Nack message ${message.properties.correlationId}`);
206
226
  channel.nack(message);
207
227
  });
208
228
  }
209
-
210
229
  async function sendToQueue({
211
230
  queue,
212
231
  correlationId,
213
232
  headers,
214
233
  data
215
234
  }) {
235
+ debug(`sendToQueue`);
236
+ // eslint-disable-next-line no-useless-catch
216
237
  try {
217
- logger.silly(`Queue ${queue}`);
218
- logger.silly(`CorrelationId ${correlationId}`);
219
- logger.silly(`Data ${JSON.stringify(data)}`);
220
- logger.silly(`Headers ${JSON.stringify(headers)}`);
238
+ debug(`Queue ${queue}`);
239
+ debug(`CorrelationId ${correlationId}`);
240
+ debug(`Data ${JSON.stringify(data)}`);
241
+ debug(`Headers ${JSON.stringify(headers)}`);
221
242
  errorUndefinedQueue(queue);
243
+ debug(`Asserting queue: ${queue}`);
222
244
  await channel.assertQueue(queue, {
223
245
  durable: true
224
246
  });
225
- channel.sendToQueue(queue, Buffer.from(JSON.stringify({
247
+ debug(`Actually sendToQueue: ${queue}`);
248
+ await channel.sendToQueue(queue, Buffer.from(JSON.stringify({
226
249
  data
227
250
  })), {
228
251
  correlationId,
229
252
  persistent: true,
230
253
  headers
231
254
  });
232
- logger.silly(`Send message for ${correlationId} to queue: ${queue}`);
255
+ debug(`Send message for ${correlationId} to queue: ${queue}`);
233
256
  } catch (error) {
234
- (0, _utils.logError)(error);
257
+ const errorToThrow = error;
258
+ //if (error instanceof ApiError) {
259
+ // throw error;
260
+ //}
261
+ debug(`SendToQueue errored: ${JSON.stringify(error)}`);
262
+ handleAmqpErrors(errorToThrow);
235
263
  }
236
264
  }
237
-
238
265
  async function removeQueue(queue) {
239
266
  // deleteQueue borks the channel if the queue does not exist
240
267
  // -> use throwaway tempChannel to avoid killing actual channel in use
241
268
  // this might be doable also with assertQueue before deleteQueue
242
269
  const tempChannel = await connection.createChannel();
243
270
  logger.verbose(`Removing queue ${queue}.`);
271
+ debug(`Removing queue ${queue}.`);
244
272
  await tempChannel.deleteQueue(queue);
245
-
246
273
  if (tempChannel) {
247
- // eslint-disable-line functional/no-conditional-statement
248
274
  await tempChannel.close();
275
+ return;
249
276
  }
250
- } // ----------------
277
+ return;
278
+ }
279
+
280
+ // ----------------
251
281
  // Helper functions
252
282
  // ----------------
253
283
 
254
-
255
284
  function messagesToRecords(messages) {
256
- logger.debug(`Parsing messages (${messages.length}) to records`);
285
+ debug(`Parsing messages (${messages.length}) to records`);
257
286
  return messages.map(message => {
258
287
  const content = JSON.parse(message.content.toString());
259
288
  return new _marcRecord.MarcRecord(content.data);
260
289
  });
261
290
  }
262
-
263
291
  async function getData(queue) {
264
- logger.debug(`Getting queue data from ${queue}`);
265
-
292
+ debug(`Getting queue data from ${queue}`);
266
293
  try {
267
294
  const {
268
295
  messageCount
269
296
  } = await channel.checkQueue(queue);
270
- logger.silly(`There is ${messageCount} messages in queue ${queue}`);
297
+ debug(`There is ${messageCount} messages in queue ${queue}`);
271
298
  const messagesToGet = messageCount >= _constants.CHUNK_SIZE ? _constants.CHUNK_SIZE : messageCount;
272
- logger.silly(`Getting ${messagesToGet} messages from queue ${queue}`);
299
+ debug(`Getting ${messagesToGet} messages from queue ${queue}`);
273
300
  const messages = await pump(messagesToGet);
274
- logger.debug(`Returning ${messages.length} unique messages`);
301
+ debug(`Returning ${messages.length} unique messages`);
275
302
  return messages;
276
303
  } catch (error) {
277
- (0, _utils.logError)(error);
304
+ handleAmqpErrors(error);
278
305
  }
279
-
280
306
  async function pump(count, results = [], identifiers = []) {
281
307
  if (count === 0) {
282
308
  return results;
283
309
  }
284
-
285
310
  const message = await channel.get(queue);
286
311
  const identifier = {
287
312
  correlationId: message.properties.correlationId,
288
313
  deliveryTag: message.fields.deliveryTag
289
- }; // Filter not unique messages
290
-
314
+ };
315
+ // Filter not unique messages
291
316
  if (identifiers.includes(identifier)) {
292
317
  return pump(count - 1, results, identifiers);
293
318
  }
294
-
295
319
  return pump(count - 1, results.concat(message), identifiers.concat(identifier));
296
320
  }
297
321
  }
298
-
299
322
  function getHeaderInfo(data) {
300
323
  return data.properties.headers;
301
324
  }
302
-
303
325
  function errorUndefinedQueue(queue) {
304
326
  if (queue === undefined || queue === '' || queue === null) {
305
327
  throw new _melindaCommons.Error(_httpStatus.default.INTERNAL_SERVER_ERROR, `Undefined queue!`);
306
328
  }
307
329
  }
330
+ function handleAmqpErrors(error) {
331
+ const newError = error;
332
+ //debug(`HandleAmqpErrors got an error: ${JSON.stringify(error)}`);
333
+ if (error instanceof _melindaCommons.Error) {
334
+ throw new _melindaCommons.Error(newError.status, newError.payload);
335
+ }
336
+ (0, _utils.logError)(error);
337
+ throw newError;
338
+ }
308
339
  }
309
340
  //# sourceMappingURL=amqp.js.map
package/dist/amqp.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"amqp.js","names":["AMQP_URL","connection","amqplib","connect","channel","createChannel","logger","createLogger","checkQueue","consumeChunk","consumeOne","ackMessages","nackMessages","sendToQueue","removeQueue","messagesToRecords","queue","style","toRecord","purge","silly","errorUndefinedQueue","channelInfo","assertQueue","durable","purgeQueue","verbose","messageCount","ApiError","httpStatus","UNPROCESSABLE_ENTITY","error","logError","messages","getData","headers","getHeaderInfo","debug","length","records","message","get","inspect","colors","maxArrayLength","depth","forEach","properties","correlationId","ack","nack","data","JSON","stringify","Buffer","from","persistent","tempChannel","deleteQueue","close","map","content","parse","toString","MarcRecord","messagesToGet","CHUNK_SIZE","pump","count","results","identifiers","identifier","deliveryTag","fields","includes","concat","undefined","INTERNAL_SERVER_ERROR"],"sources":["../src/amqp.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2021 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport amqplib from 'amqplib';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {CHUNK_SIZE} from './constants';\nimport {logError} from './utils';\nimport {inspect} from 'util';\nimport httpStatus from 'http-status';\n\nexport default async function (AMQP_URL) {\n const connection = await amqplib.connect(AMQP_URL);\n const channel = await connection.createChannel();\n const logger = createLogger();\n\n return {checkQueue, consumeChunk, consumeOne, ackMessages, nackMessages, sendToQueue, removeQueue, messagesToRecords};\n\n // eslint-disable-next-line max-statements\n async function checkQueue({queue, style = 'basic', toRecord = true, purge = false}) {\n logger.silly(`checkQueue: ${queue}, Style: ${style}: toRecord: ${toRecord}, Purge: ${purge}`);\n\n try {\n\n errorUndefinedQueue(queue);\n const channelInfo = await channel.assertQueue(queue, {durable: true});\n\n if (purge) {\n await purgeQueue(purge);\n logger.verbose(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);\n return checkQueue({queue, style});\n }\n\n if (channelInfo.messageCount < 1) {\n logger.silly(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);\n return false;\n }\n logger.silly(`Queue ${queue} has ${channelInfo.messageCount} messages`);\n\n if (style === 'messages') {\n return channelInfo.messageCount;\n }\n\n // Note: returns one message (+ record, of toRecord: true)\n // note: if toRecord is false returns just plain message / false\n // note: if toRecord is true returns {headers, records, messages} -object / false\n // should this be more consistent?\n if (style === 'one') {\n return consumeOne(queue, toRecord);\n }\n\n // Note: returns a chunk of (100) messages (+ records, if toRecord: true)\n // returns {headers, records, messages} object or {headers, messages} object depending on toRecord\n if (style === 'basic') {\n return consumeChunk(queue, toRecord);\n }\n\n // Defaults:\n throw new ApiError(httpStatus.UNPROCESSABLE_ENTITY);\n } catch (error) {\n logError(error);\n }\n\n function purgeQueue(purge) {\n if (purge) {\n return channel.purgeQueue(queue);\n }\n }\n }\n\n async function consumeChunk(queue, toRecord) {\n logger.silly(`Prepared to consumeChunk from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // getData: next chunk (100) messages\n const messages = await getData(queue);\n const headers = getHeaderInfo(messages[0]);\n logger.debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);\n\n if (toRecord) {\n const records = await messagesToRecords(messages);\n return {headers, records, messages};\n }\n\n return {headers, messages};\n } catch (error) {\n logError(error);\n }\n }\n\n async function consumeOne(queue, toRecord) {\n logger.silly(`Prepared to consumeOne from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // Returns false if 0 items in queue\n const message = await channel.get(queue);\n\n logger.silly(`Message: ${inspect(message, {colors: true, maxArrayLength: 3, depth: 3})}`);\n // Do not spam the logs\n logger.debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);\n\n if (message) {\n if (toRecord) {\n const headers = getHeaderInfo(message);\n const records = messagesToRecords([message]);\n return {headers, records, messages: [message]};\n }\n\n return message;\n }\n\n return false;\n } catch (error) {\n logError(error);\n }\n }\n\n function ackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Ack message ${message.properties.correlationId}`);\n channel.ack(message);\n });\n }\n\n function nackMessages(messages) {\n messages.forEach(message => {\n logger.silly(`Nack message ${message.properties.correlationId}`);\n channel.nack(message);\n });\n }\n\n async function sendToQueue({queue, correlationId, headers, data}) {\n try {\n logger.silly(`Queue ${queue}`);\n logger.silly(`CorrelationId ${correlationId}`);\n logger.silly(`Data ${JSON.stringify(data)}`);\n logger.silly(`Headers ${JSON.stringify(headers)}`);\n\n errorUndefinedQueue(queue);\n await channel.assertQueue(queue, {durable: true});\n\n channel.sendToQueue(\n queue,\n Buffer.from(JSON.stringify({data})),\n {\n correlationId,\n persistent: true,\n headers\n }\n );\n logger.silly(`Send message for ${correlationId} to queue: ${queue}`);\n } catch (error) {\n logError(error);\n }\n }\n\n async function removeQueue(queue) {\n // deleteQueue borks the channel if the queue does not exist\n // -> use throwaway tempChannel to avoid killing actual channel in use\n // this might be doable also with assertQueue before deleteQueue\n const tempChannel = await connection.createChannel();\n logger.verbose(`Removing queue ${queue}.`);\n await tempChannel.deleteQueue(queue);\n if (tempChannel) { // eslint-disable-line functional/no-conditional-statement\n await tempChannel.close();\n }\n }\n\n // ----------------\n // Helper functions\n // ----------------\n\n function messagesToRecords(messages) {\n logger.debug(`Parsing messages (${messages.length}) to records`);\n\n return messages.map(message => {\n const content = JSON.parse(message.content.toString());\n return new MarcRecord(content.data);\n });\n }\n\n async function getData(queue) {\n logger.debug(`Getting queue data from ${queue}`);\n try {\n const {messageCount} = await channel.checkQueue(queue);\n logger.silly(`There is ${messageCount} messages in queue ${queue}`);\n const messagesToGet = messageCount >= CHUNK_SIZE ? CHUNK_SIZE : messageCount;\n logger.silly(`Getting ${messagesToGet} messages from queue ${queue}`);\n\n const messages = await pump(messagesToGet);\n\n logger.debug(`Returning ${messages.length} unique messages`);\n\n return messages;\n } catch (error) {\n logError(error);\n }\n\n async function pump(count, results = [], identifiers = []) {\n if (count === 0) {\n return results;\n }\n\n const message = await channel.get(queue);\n const identifier = {\n correlationId: message.properties.correlationId,\n deliveryTag: message.fields.deliveryTag\n };\n // Filter not unique messages\n if (identifiers.includes(identifier)) {\n return pump(count - 1, results, identifiers);\n }\n\n return pump(count - 1, results.concat(message), identifiers.concat(identifier));\n }\n }\n\n function getHeaderInfo(data) {\n return data.properties.headers;\n }\n\n function errorUndefinedQueue(queue) {\n if (queue === undefined || queue === '' || queue === null) {\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Undefined queue!`);\n }\n }\n}\n\n\n"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAnCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAWe,wBAAgBA,QAAhB,EAA0B;EACvC,MAAMC,UAAU,GAAG,MAAMC,gBAAA,CAAQC,OAAR,CAAgBH,QAAhB,CAAzB;EACA,MAAMI,OAAO,GAAG,MAAMH,UAAU,CAACI,aAAX,EAAtB;EACA,MAAMC,MAAM,GAAG,IAAAC,mCAAA,GAAf;EAEA,OAAO;IAACC,UAAD;IAAaC,YAAb;IAA2BC,UAA3B;IAAuCC,WAAvC;IAAoDC,YAApD;IAAkEC,WAAlE;IAA+EC,WAA/E;IAA4FC;EAA5F,CAAP,CALuC,CAOvC;;EACA,eAAeP,UAAf,CAA0B;IAACQ,KAAD;IAAQC,KAAK,GAAG,OAAhB;IAAyBC,QAAQ,GAAG,IAApC;IAA0CC,KAAK,GAAG;EAAlD,CAA1B,EAAoF;IAClFb,MAAM,CAACc,KAAP,CAAc,eAAcJ,KAAM,YAAWC,KAAM,eAAcC,QA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B,CAAX;MACD;;MAED,OAAOH,IAAI,CAACC,KAAK,GAAG,CAAT,EAAYC,OAAO,CAACM,MAAR,CAAenC,OAAf,CAAZ,EAAqC8B,WAAW,CAACK,MAAZ,CAAmBJ,UAAnB,CAArC,CAAX;IACD;EACF;;EAED,SAASnC,aAAT,CAAuBe,IAAvB,EAA6B;IAC3B,OAAOA,IAAI,CAACJ,UAAL,CAAgBZ,OAAvB;EACD;;EAED,SAASd,mBAAT,CAA6BL,KAA7B,EAAoC;IAClC,IAAIA,KAAK,KAAK4D,SAAV,IAAuB5D,KAAK,KAAK,EAAjC,IAAuCA,KAAK,KAAK,IAArD,EAA2D;MACzD,MAAM,IAAIY,qBAAJ,CAAaC,mBAAA,CAAWgD,qBAAxB,EAAgD,kBAAhD,CAAN;IACD;EACF;AACF"}
1
+ {"version":3,"file":"amqp.js","names":["AMQP_URL","runHealthCheck","logger","createLogger","debug","createDebugLogger","debugHC","extend","connection","amqplib","connect","channel","createChannel","healthCheckLoop","healthCheck","checkQueue","consumeChunk","consumeOne","ackMessages","nackMessages","sendToQueue","removeQueue","messagesToRecords","closeChannel","closeConnection","close","wait","setTimeoutPromise","promisify","setTimeout","assertQueue","durable","error","errorToThrow","JSON","stringify","handleAmqpErrors","queue","style","toRecord","purge","errorUndefinedQueue","channelInfo","purgeQueue","verbose","messageCount","ApiError","httpStatus","UNPROCESSABLE_ENTITY","messages","getData","headers","getHeaderInfo","length","records","message","get","inspect","colors","maxArrayLength","depth","forEach","properties","correlationId","ack","nack","data","Buffer","from","persistent","tempChannel","deleteQueue","map","content","parse","toString","MarcRecord","messagesToGet","CHUNK_SIZE","pump","count","results","identifiers","identifier","deliveryTag","fields","includes","concat","undefined","INTERNAL_SERVER_ERROR","newError","status","payload","logError"],"sources":["../src/amqp.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport amqplib from 'amqplib';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport createDebugLogger from 'debug';\nimport {CHUNK_SIZE} from './constants';\n//import {logError} from './utils';\nimport {promisify, inspect} from 'util';\nimport httpStatus from 'http-status';\nimport {logError} from './utils';\n\nexport default async function (AMQP_URL, runHealthCheck = false) {\n const logger = createLogger();\n const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:amqp');\n const debugHC = debug.extend('HC');\n\n debug(`Creating an AMQP operator to ${AMQP_URL}`);\n const connection = await amqplib.connect(AMQP_URL);\n const channel = await connection.createChannel();\n\n const healthCheckLoop = runHealthCheck ? healthCheck() : 'Not running health check';\n\n debug(`Connection: ${connection}`);\n debug(`Channel: ${channel}`);\n debug(`HealthCheckLoop ${healthCheckLoop}`);\n\n return {checkQueue, consumeChunk, consumeOne, ackMessages, nackMessages, sendToQueue, removeQueue, messagesToRecords, closeChannel, closeConnection};\n\n async function closeChannel() {\n debug(`Closing channel`);\n await channel.close();\n debug(`Channel: ${channel}`);\n }\n\n async function closeConnection() {\n debug(`Closing connection`);\n await connection.close();\n debug(`Connection: ${connection}`);\n }\n\n async function healthCheck(wait = false) {\n const setTimeoutPromise = promisify(setTimeout);\n if (wait) {\n await setTimeoutPromise(wait);\n return healthCheck(false);\n }\n\n try {\n debugHC(`Health checking amqp by asserting a queue`);\n await channel.assertQueue('HEALTHCHECK', {durable: false});\n debugHC(`Waiting 200ms before running healthCheck next`);\n return healthCheck('200');\n } catch (error) {\n const errorToThrow = error;\n debugHC(`HealthCheck error ${JSON.stringify(error)}`);\n handleAmqpErrors(errorToThrow);\n }\n }\n\n // eslint-disable-next-line max-statements\n async function checkQueue({queue, style = 'basic', toRecord = true, purge = false}) {\n debug(`checkQueue: ${queue}, Style: ${style}, toRecord: ${toRecord}, Purge: ${purge}`);\n\n try {\n\n errorUndefinedQueue(queue);\n const channelInfo = await channel.assertQueue(queue, {durable: true});\n\n if (purge) {\n await purgeQueue(purge);\n logger.verbose(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);\n debug(`Queue ${queue} has purged ${channelInfo.messageCount} messages`);\n return checkQueue({queue, style});\n }\n\n if (style === 'messages') {\n return channelInfo.messageCount;\n }\n\n if (channelInfo.messageCount < 1) {\n debug(`checkQueue: ${channelInfo.messageCount} - ${queue} is empty`);\n return false;\n }\n debug(`Queue ${queue} has ${channelInfo.messageCount} messages`);\n\n // Note: returns one message (+ record, of toRecord: true)\n // note: if toRecord is false returns just plain message / false\n // note: if toRecord is true returns {headers, records, messages} -object / false\n // should this be more consistent?\n if (style === 'one') {\n return consumeOne(queue, toRecord);\n }\n\n // Note: returns a chunk of (100) messages (+ records, if toRecord: true)\n // returns {headers, records, messages} object or {headers, messages} object depending on toRecord\n if (style === 'basic') {\n return consumeChunk(queue, toRecord);\n }\n\n // Defaults:\n throw new ApiError(httpStatus.UNPROCESSABLE_ENTITY);\n } catch (error) {\n handleAmqpErrors(error);\n }\n\n function purgeQueue(purge) {\n debug(`Purging queue: ${queue}, purge: ${purge}`);\n if (purge) {\n return channel.purgeQueue(queue);\n }\n }\n }\n\n async function consumeChunk(queue, toRecord) {\n debug(`Prepared to consumeChunk from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // getData: next chunk (100) messages\n const messages = await getData(queue);\n const headers = getHeaderInfo(messages[0]);\n debug(`consumeChunk (${messages ? messages.length : '0'} from queue ${queue}) ${toRecord ? 'to records' : 'just messages'}`);\n\n if (toRecord) {\n const records = await messagesToRecords(messages);\n return {headers, records, messages};\n }\n\n return {headers, messages};\n } catch (error) {\n handleAmqpErrors(error);\n }\n }\n\n async function consumeOne(queue, toRecord) {\n debug(`Prepared to consumeOne from queue: ${queue}`);\n try {\n await channel.assertQueue(queue, {durable: true});\n\n // Returns false if 0 items in queue\n const message = await channel.get(queue);\n\n debug(`Message: ${inspect(message, {colors: true, maxArrayLength: 3, depth: 3})}`);\n // Do not spam the logs\n debug(`consumeOne from queue: ${queue} ${toRecord ? 'to records' : 'just the message'}`);\n\n if (message) {\n if (toRecord) {\n const headers = getHeaderInfo(message);\n const records = messagesToRecords([message]);\n return {headers, records, messages: [message]};\n }\n\n return message;\n }\n\n return false;\n } catch (error) {\n handleAmqpErrors(error);\n }\n }\n\n function ackMessages(messages) {\n messages.forEach(message => {\n debug(`Ack message ${message.properties.correlationId}`);\n channel.ack(message);\n });\n }\n\n function nackMessages(messages) {\n messages.forEach(message => {\n debug(`Nack message ${message.properties.correlationId}`);\n channel.nack(message);\n });\n }\n\n async function sendToQueue({queue, correlationId, headers, data}) {\n debug(`sendToQueue`);\n // eslint-disable-next-line no-useless-catch\n try {\n debug(`Queue ${queue}`);\n debug(`CorrelationId ${correlationId}`);\n debug(`Data ${JSON.stringify(data)}`);\n debug(`Headers ${JSON.stringify(headers)}`);\n\n errorUndefinedQueue(queue);\n\n debug(`Asserting queue: ${queue}`);\n await channel.assertQueue(queue, {durable: true});\n\n debug(`Actually sendToQueue: ${queue}`);\n await channel.sendToQueue(\n queue,\n Buffer.from(JSON.stringify({data})),\n {\n correlationId,\n persistent: true,\n headers\n }\n );\n debug(`Send message for ${correlationId} to queue: ${queue}`);\n\n\n } catch (error) {\n const errorToThrow = error;\n //if (error instanceof ApiError) {\n // throw error;\n //}\n debug(`SendToQueue errored: ${JSON.stringify(error)}`);\n handleAmqpErrors(errorToThrow);\n }\n }\n\n async function removeQueue(queue) {\n // deleteQueue borks the channel if the queue does not exist\n // -> use throwaway tempChannel to avoid killing actual channel in use\n // this might be doable also with assertQueue before deleteQueue\n const tempChannel = await connection.createChannel();\n logger.verbose(`Removing queue ${queue}.`);\n debug(`Removing queue ${queue}.`);\n await tempChannel.deleteQueue(queue);\n\n if (tempChannel) {\n await tempChannel.close();\n return;\n }\n\n return;\n }\n\n // ----------------\n // Helper functions\n // ----------------\n\n function messagesToRecords(messages) {\n debug(`Parsing messages (${messages.length}) to records`);\n\n return messages.map(message => {\n const content = JSON.parse(message.content.toString());\n return new MarcRecord(content.data);\n });\n }\n\n async function getData(queue) {\n debug(`Getting queue data from ${queue}`);\n try {\n const {messageCount} = await channel.checkQueue(queue);\n debug(`There is ${messageCount} messages in queue ${queue}`);\n const messagesToGet = messageCount >= CHUNK_SIZE ? CHUNK_SIZE : messageCount;\n debug(`Getting ${messagesToGet} messages from queue ${queue}`);\n\n const messages = await pump(messagesToGet);\n\n debug(`Returning ${messages.length} unique messages`);\n\n return messages;\n } catch (error) {\n handleAmqpErrors(error);\n }\n\n async function pump(count, results = [], identifiers = []) {\n if (count === 0) {\n return results;\n }\n\n const message = await channel.get(queue);\n const identifier = {\n correlationId: message.properties.correlationId,\n deliveryTag: message.fields.deliveryTag\n };\n // Filter not unique messages\n if (identifiers.includes(identifier)) {\n return pump(count - 1, results, identifiers);\n }\n\n return pump(count - 1, results.concat(message), identifiers.concat(identifier));\n }\n }\n\n function getHeaderInfo(data) {\n return data.properties.headers;\n }\n\n function errorUndefinedQueue(queue) {\n if (queue === undefined || queue === '' || queue === null) {\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, `Undefined queue!`);\n }\n }\n\n function handleAmqpErrors(error) {\n const newError = error;\n //debug(`HandleAmqpErrors got an error: ${JSON.stringify(error)}`);\n if (error instanceof ApiError) {\n throw new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