@mhubcloud/tms_core 3.7.0-beta.0 → 3.7.0
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.js +78 -44
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -390,7 +390,15 @@ function resolveEvent(event, publisherConfig) {
|
|
|
390
390
|
}
|
|
391
391
|
match = matchEvent(event, publisherEvent);
|
|
392
392
|
if (match) {
|
|
393
|
-
|
|
393
|
+
if (typeof publisherEvent === "object" && publisherEvent.schedule) {
|
|
394
|
+
publishers.push({
|
|
395
|
+
queueName: publisher2,
|
|
396
|
+
schedule: publisherEvent.schedule
|
|
397
|
+
});
|
|
398
|
+
} else {
|
|
399
|
+
publishers.push(publisher2);
|
|
400
|
+
}
|
|
401
|
+
break;
|
|
394
402
|
}
|
|
395
403
|
}
|
|
396
404
|
}
|
|
@@ -403,16 +411,17 @@ function resolveEvent(event, publisherConfig) {
|
|
|
403
411
|
}
|
|
404
412
|
function matchEvent(event, publisherEvent) {
|
|
405
413
|
if (typeof publisherEvent === "string") {
|
|
406
|
-
const regex = new RegExp(publisherEvent);
|
|
414
|
+
const regex = new RegExp(`^(?:${publisherEvent})$`);
|
|
407
415
|
const match = regex.test(event.code);
|
|
408
416
|
return match;
|
|
409
417
|
} else if (typeof publisherEvent === "object") {
|
|
418
|
+
const eventParams = publisherEvent.event;
|
|
410
419
|
let paramMatch = true;
|
|
411
|
-
for (const key of Object.keys(
|
|
412
|
-
const publisherEventParam =
|
|
420
|
+
for (const key of Object.keys(eventParams)) {
|
|
421
|
+
const publisherEventParam = eventParams[key];
|
|
413
422
|
const eventParam = event[key];
|
|
414
423
|
if (typeof publisherEventParam === "string" && typeof eventParam === "string") {
|
|
415
|
-
const regex = new RegExp(publisherEventParam);
|
|
424
|
+
const regex = new RegExp(`^(?:${publisherEventParam})$`);
|
|
416
425
|
paramMatch = regex.test(eventParam);
|
|
417
426
|
if (!paramMatch) {
|
|
418
427
|
return false;
|
|
@@ -437,17 +446,37 @@ async function dispatchEvent(message, queues) {
|
|
|
437
446
|
}
|
|
438
447
|
await Promise.all(
|
|
439
448
|
queues.map(async (queue) => {
|
|
449
|
+
let queueName;
|
|
440
450
|
try {
|
|
441
451
|
let sender;
|
|
442
|
-
|
|
443
|
-
|
|
452
|
+
let serviceBusMessage;
|
|
453
|
+
if (typeof queue === "string") {
|
|
454
|
+
queueName = queue;
|
|
455
|
+
serviceBusMessage = { body: message };
|
|
456
|
+
} else if (typeof queue === "object") {
|
|
457
|
+
queueName = queue.queueName;
|
|
458
|
+
serviceBusMessage = {
|
|
459
|
+
body: message,
|
|
460
|
+
scheduledEnqueueTimeUtc: parseScheduleOptions(queue.schedule)
|
|
461
|
+
};
|
|
444
462
|
} else {
|
|
445
|
-
|
|
446
|
-
|
|
463
|
+
console.log(`Invalid queue name format`);
|
|
464
|
+
}
|
|
465
|
+
if (queueName && serviceBusMessage) {
|
|
466
|
+
if (senders[queueName]) {
|
|
467
|
+
sender = senders[queueName];
|
|
468
|
+
} else {
|
|
469
|
+
senders[queueName] = serviceBusClient.createSender(queueName);
|
|
470
|
+
sender = senders[queueName];
|
|
471
|
+
}
|
|
472
|
+
if (sender) {
|
|
473
|
+
await sender.sendMessages(serviceBusMessage);
|
|
474
|
+
}
|
|
447
475
|
}
|
|
448
|
-
await sender.sendMessages({ body: message });
|
|
449
476
|
} catch (e) {
|
|
450
|
-
console.log(
|
|
477
|
+
console.log(
|
|
478
|
+
`Sending to queue ${queueName ?? "unknown"} failed. Cause: ${e instanceof Error ? e.message : ""}`
|
|
479
|
+
);
|
|
451
480
|
}
|
|
452
481
|
})
|
|
453
482
|
);
|
|
@@ -471,6 +500,42 @@ async function logToCosmosDb(message) {
|
|
|
471
500
|
logException(e);
|
|
472
501
|
}
|
|
473
502
|
}
|
|
503
|
+
function parseScheduleOptions(options) {
|
|
504
|
+
let dispatchTime;
|
|
505
|
+
const now = /* @__PURE__ */ new Date();
|
|
506
|
+
if ("timestamp" in options) {
|
|
507
|
+
dispatchTime = new Date(options.timestamp);
|
|
508
|
+
} else if ("scheduleHour" in options) {
|
|
509
|
+
dispatchTime = new Date(
|
|
510
|
+
now.getFullYear(),
|
|
511
|
+
now.getMonth(),
|
|
512
|
+
now.getDate() + (options.dayOffset ?? 0),
|
|
513
|
+
options.scheduleHour,
|
|
514
|
+
options.scheduleMinute ?? 0,
|
|
515
|
+
0,
|
|
516
|
+
0
|
|
517
|
+
);
|
|
518
|
+
} else if ("scheduleInterval" in options && "startHour" in options) {
|
|
519
|
+
const hour = now.getHours();
|
|
520
|
+
const nextHour = Math.ceil(hour / options.scheduleInterval) * options.scheduleInterval;
|
|
521
|
+
dispatchTime = new Date(
|
|
522
|
+
now.getFullYear(),
|
|
523
|
+
now.getMonth(),
|
|
524
|
+
now.getDate() + (options.dayOffset ?? 0),
|
|
525
|
+
nextHour,
|
|
526
|
+
options.startMinute ?? 0,
|
|
527
|
+
0,
|
|
528
|
+
0
|
|
529
|
+
);
|
|
530
|
+
if (nextHour >= 24) {
|
|
531
|
+
dispatchTime.setDate(dispatchTime.getDate() + 1);
|
|
532
|
+
dispatchTime.setHours(options.startHour, options.startMinute);
|
|
533
|
+
}
|
|
534
|
+
} else {
|
|
535
|
+
throw new Error("Invalid scheduling configuration");
|
|
536
|
+
}
|
|
537
|
+
return dispatchTime;
|
|
538
|
+
}
|
|
474
539
|
async function scheduleEvent(message, queue, options) {
|
|
475
540
|
if (!serviceBusClient) {
|
|
476
541
|
if (!serviceBusConnectionString) {
|
|
@@ -486,39 +551,7 @@ async function scheduleEvent(message, queue, options) {
|
|
|
486
551
|
senders[queue] = serviceBusClient.createSender(queue);
|
|
487
552
|
sender = senders[queue];
|
|
488
553
|
}
|
|
489
|
-
|
|
490
|
-
const now = /* @__PURE__ */ new Date();
|
|
491
|
-
if ("timestamp" in options) {
|
|
492
|
-
dispatchTime = new Date(options.timestamp);
|
|
493
|
-
} else if ("scheduleHour" in options) {
|
|
494
|
-
dispatchTime = new Date(
|
|
495
|
-
now.getFullYear(),
|
|
496
|
-
now.getMonth(),
|
|
497
|
-
now.getDate() + (options.dayOffset ?? 0),
|
|
498
|
-
options.scheduleHour,
|
|
499
|
-
options.scheduleMinute ?? 0,
|
|
500
|
-
0,
|
|
501
|
-
0
|
|
502
|
-
);
|
|
503
|
-
} else if ("scheduleInterval" in options && "startHour" in options) {
|
|
504
|
-
const hour = now.getHours();
|
|
505
|
-
const nextHour = Math.ceil(hour / options.scheduleInterval) * options.scheduleInterval;
|
|
506
|
-
dispatchTime = new Date(
|
|
507
|
-
now.getFullYear(),
|
|
508
|
-
now.getMonth(),
|
|
509
|
-
now.getDate() + (options.dayOffset ?? 0),
|
|
510
|
-
nextHour,
|
|
511
|
-
options.startMinute ?? 0,
|
|
512
|
-
0,
|
|
513
|
-
0
|
|
514
|
-
);
|
|
515
|
-
if (nextHour >= 24) {
|
|
516
|
-
dispatchTime.setDate(dispatchTime.getDate() + 1);
|
|
517
|
-
dispatchTime.setHours(options.startHour, options.startMinute);
|
|
518
|
-
}
|
|
519
|
-
} else {
|
|
520
|
-
throw new Error("Invalid scheduling configuration");
|
|
521
|
-
}
|
|
554
|
+
const dispatchTime = parseScheduleOptions(options);
|
|
522
555
|
const serviceBusMessage = {
|
|
523
556
|
body: message,
|
|
524
557
|
scheduledEnqueueTimeUtc: dispatchTime
|
|
@@ -529,6 +562,7 @@ async function scheduleEvent(message, queue, options) {
|
|
|
529
562
|
console.log(
|
|
530
563
|
`Scheduling message to queue ${queue} failed. Cause: ${e instanceof Error ? e.message : ""}`
|
|
531
564
|
);
|
|
565
|
+
return null;
|
|
532
566
|
}
|
|
533
567
|
}
|
|
534
568
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mhubcloud/tms_core",
|
|
3
|
-
"version": "3.7.0
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "Core function for mHub Cloud TMS publishers",
|
|
5
5
|
"homepage": "https://mhubcloud.com/",
|
|
6
6
|
"license": "Apache 2.0",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"conversion API"
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
|
-
"test": "mocha ./tests
|
|
19
|
+
"test": "mocha ./tests/broker.js --local-settings=./local.settings.json --timeout 100000",
|
|
20
20
|
"dev": "ts-node index.ts",
|
|
21
21
|
"build": "tsup ./src/index.ts --format cjs --target node22"
|
|
22
22
|
},
|