@uns-kit/core 2.0.69 → 2.0.71
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/LICENSE +21 -21
- package/README.md +334 -312
- package/dist/base-path.js.map +1 -1
- package/dist/config/app-config.js.map +1 -1
- package/dist/config/project.config.extension.js.map +1 -1
- package/dist/config-file.js.map +1 -1
- package/dist/examples/datahub-client.js.map +1 -1
- package/dist/graphql/schema.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/logger.js.map +1 -1
- package/dist/tools/auth/auth-client.js.map +1 -1
- package/dist/tools/auth/index.js.map +1 -1
- package/dist/tools/auth/secure-store.js.map +1 -1
- package/dist/tools/base-path.js.map +1 -1
- package/dist/tools/datahub/datahub-client.js.map +1 -1
- package/dist/tools/file-utils.js.map +1 -1
- package/dist/tools/generate-config-schema.js.map +1 -1
- package/dist/tools/generate-uns-dictionary.js +7 -7
- package/dist/tools/generate-uns-dictionary.js.map +1 -1
- package/dist/tools/generate-uns-measurements.js +7 -7
- package/dist/tools/generate-uns-measurements.js.map +1 -1
- package/dist/tools/generate-uns-reference.js +9 -9
- package/dist/tools/generate-uns-reference.js.map +1 -1
- package/dist/tools/pull-request.js.map +1 -1
- package/dist/tools/refresh-uns.js.map +1 -1
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/sync-uns-metadata.js +22 -22
- package/dist/tools/sync-uns-metadata.js.map +1 -1
- package/dist/tools/sync-uns-schema.js +17 -17
- package/dist/tools/sync-uns-schema.js.map +1 -1
- package/dist/uns/handover-manager-event-emitter.js.map +1 -1
- package/dist/uns/handover-manager.d.ts.map +1 -1
- package/dist/uns/handover-manager.js +1 -3
- package/dist/uns/handover-manager.js.map +1 -1
- package/dist/uns/process-config.js.map +1 -1
- package/dist/uns/service-metadata.js.map +1 -1
- package/dist/uns/status-monitor.js.map +1 -1
- package/dist/uns/uns-asset.js.map +1 -1
- package/dist/uns/uns-attributes.js.map +1 -1
- package/dist/uns/uns-dictionary-registry.js.map +1 -1
- package/dist/uns/uns-dictionary.generated.js.map +1 -1
- package/dist/uns/uns-event-emitter.js.map +1 -1
- package/dist/uns/uns-interfaces.d.ts +2 -0
- package/dist/uns/uns-interfaces.d.ts.map +1 -1
- package/dist/uns/uns-interfaces.js.map +1 -1
- package/dist/uns/uns-measurements.generated.js.map +1 -1
- package/dist/uns/uns-measurements.js.map +1 -1
- package/dist/uns/uns-object.js.map +1 -1
- package/dist/uns/uns-packet.js.map +1 -1
- package/dist/uns/uns-path.js.map +1 -1
- package/dist/uns/uns-proxy-process.d.ts +4 -1
- package/dist/uns/uns-proxy-process.d.ts.map +1 -1
- package/dist/uns/uns-proxy-process.js +36 -5
- package/dist/uns/uns-proxy-process.js.map +1 -1
- package/dist/uns/uns-proxy.js.map +1 -1
- package/dist/uns/uns-tags.js.map +1 -1
- package/dist/uns/uns-topic-matcher.js.map +1 -1
- package/dist/uns/uns-topics.js.map +1 -1
- package/dist/uns-config/config-schema.js.map +1 -1
- package/dist/uns-config/host-placeholders.js.map +1 -1
- package/dist/uns-config/schema-tolls.js.map +1 -1
- package/dist/uns-config/schema-tools.js.map +1 -1
- package/dist/uns-config/secret-placeholders.js.map +1 -1
- package/dist/uns-config/secret-resolver.js.map +1 -1
- package/dist/uns-config/uns-core-schema.js.map +1 -1
- package/dist/uns-grpc/uns-gateway-cli.js.map +1 -1
- package/dist/uns-grpc/uns-gateway-server.d.ts.map +1 -1
- package/dist/uns-grpc/uns-gateway-server.js +13 -4
- package/dist/uns-grpc/uns-gateway-server.js.map +1 -1
- package/dist/uns-grpc/uns-gateway.proto +107 -107
- package/dist/uns-mqtt/mqtt-interfaces.d.ts +2 -0
- package/dist/uns-mqtt/mqtt-interfaces.d.ts.map +1 -1
- package/dist/uns-mqtt/mqtt-interfaces.js.map +1 -1
- package/dist/uns-mqtt/mqtt-proxy.d.ts +1 -1
- package/dist/uns-mqtt/mqtt-proxy.d.ts.map +1 -1
- package/dist/uns-mqtt/mqtt-proxy.js +35 -38
- package/dist/uns-mqtt/mqtt-proxy.js.map +1 -1
- package/dist/uns-mqtt/mqtt-topic-builder.js.map +1 -1
- package/dist/uns-mqtt/mqtt-worker-init.js.map +1 -1
- package/dist/uns-mqtt/mqtt-worker.d.ts.map +1 -1
- package/dist/uns-mqtt/mqtt-worker.js +33 -17
- package/dist/uns-mqtt/mqtt-worker.js.map +1 -1
- package/dist/uns-mqtt/throttled-queue.d.ts +13 -2
- package/dist/uns-mqtt/throttled-queue.d.ts.map +1 -1
- package/dist/uns-mqtt/throttled-queue.js +85 -33
- package/dist/uns-mqtt/throttled-queue.js.map +1 -1
- package/dist/uns-mqtt/uns-mqtt-proxy.d.ts +21 -3
- package/dist/uns-mqtt/uns-mqtt-proxy.d.ts.map +1 -1
- package/dist/uns-mqtt/uns-mqtt-proxy.js +162 -37
- package/dist/uns-mqtt/uns-mqtt-proxy.js.map +1 -1
- package/dist/uns-mqtt/ws-proxy.js.map +1 -1
- package/package.json +1 -1
|
@@ -10,6 +10,8 @@ export class MqttWorker {
|
|
|
10
10
|
subscriber;
|
|
11
11
|
constructor(workerData) {
|
|
12
12
|
const publishThrottlingDelay = workerData.publishThrottlingDelay ?? 1;
|
|
13
|
+
const publishConcurrency = workerData.publishConcurrency ?? 1;
|
|
14
|
+
const maxPendingPublishes = workerData.maxPendingPublishes;
|
|
13
15
|
const subscribeThrottlingDelay = workerData.subscribeThrottlingDelay ?? 1;
|
|
14
16
|
const persistToDisk = workerData.persistToDisk ?? false;
|
|
15
17
|
const mqttHost = workerData.mqttHost;
|
|
@@ -40,28 +42,14 @@ export class MqttWorker {
|
|
|
40
42
|
const publishOptions = options ?? defaultPublishOptions;
|
|
41
43
|
try {
|
|
42
44
|
await this.mqttProxy.publish(topic, message, publishOptions);
|
|
43
|
-
parentPort?.postMessage({
|
|
44
|
-
command: "enqueueResult",
|
|
45
|
-
id,
|
|
46
|
-
status: "success",
|
|
47
|
-
topic,
|
|
48
|
-
message,
|
|
49
|
-
options: publishOptions,
|
|
50
|
-
});
|
|
51
45
|
}
|
|
52
46
|
catch (reason) {
|
|
53
47
|
logger.error(`${instanceName} - Error publishing message to topic ${topic}: ${reason?.message ?? String(reason)}`);
|
|
54
|
-
parentPort?.postMessage({
|
|
55
|
-
command: "enqueueResult",
|
|
56
|
-
id,
|
|
57
|
-
status: "error",
|
|
58
|
-
error: reason?.message ?? String(reason),
|
|
59
|
-
});
|
|
60
48
|
throw reason;
|
|
61
49
|
}
|
|
62
50
|
};
|
|
63
51
|
// Create an instance of ThrottledPublisher.
|
|
64
|
-
this.publisher = new ThrottledPublisher(publishThrottlingDelay, publishFunction, persistToDisk, join(basePath, "/workerQueue/", "throttled-publisher-queue.json"), instanceName, publisherActive);
|
|
52
|
+
this.publisher = new ThrottledPublisher(publishThrottlingDelay, publishFunction, persistToDisk, join(basePath, "/workerQueue/", "throttled-publisher-queue.json"), instanceName, publisherActive, publishConcurrency, maxPendingPublishes);
|
|
65
53
|
// Define the message handler for incoming messages.
|
|
66
54
|
const messageHandler = async (topic, message) => {
|
|
67
55
|
parentPort?.postMessage({
|
|
@@ -82,10 +70,38 @@ export class MqttWorker {
|
|
|
82
70
|
parentPort?.on("message", async (msg) => {
|
|
83
71
|
if (msg && msg.command === "enqueue" && msg.id && msg.topic && msg.message !== undefined) {
|
|
84
72
|
try {
|
|
85
|
-
|
|
73
|
+
const publishPromise = this.publisher.enqueueOrThrow(msg.topic, msg.message, msg.id, msg.options);
|
|
74
|
+
parentPort?.postMessage({
|
|
75
|
+
command: "enqueueAccepted",
|
|
76
|
+
id: msg.id,
|
|
77
|
+
status: "accepted",
|
|
78
|
+
});
|
|
79
|
+
void publishPromise
|
|
80
|
+
.then(() => {
|
|
81
|
+
parentPort?.postMessage({
|
|
82
|
+
command: "publishResult",
|
|
83
|
+
id: msg.id,
|
|
84
|
+
status: "success",
|
|
85
|
+
topic: msg.topic,
|
|
86
|
+
});
|
|
87
|
+
})
|
|
88
|
+
.catch((error) => {
|
|
89
|
+
parentPort?.postMessage({
|
|
90
|
+
command: "publishResult",
|
|
91
|
+
id: msg.id,
|
|
92
|
+
status: "error",
|
|
93
|
+
topic: msg.topic,
|
|
94
|
+
error: error?.message ?? String(error),
|
|
95
|
+
});
|
|
96
|
+
});
|
|
86
97
|
}
|
|
87
98
|
catch (error) {
|
|
88
|
-
|
|
99
|
+
parentPort?.postMessage({
|
|
100
|
+
command: "enqueueAccepted",
|
|
101
|
+
id: msg.id,
|
|
102
|
+
status: "error",
|
|
103
|
+
error: error?.message ?? String(error),
|
|
104
|
+
});
|
|
89
105
|
}
|
|
90
106
|
}
|
|
91
107
|
else if (msg && msg.command === "subscribeAsync" && msg.topics) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mqtt-worker.js","sourceRoot":"","sources":["../../src/uns-mqtt/mqtt-worker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE/E,MAAM,OAAO,UAAU;IACb,SAAS,CAAY;IACnB,SAAS,CAAqB;IAC9B,UAAU,CAAsB;IAE1C,YAAY,UAA2B;QACrC,MAAM,sBAAsB,GAAG,UAAU,CAAC,sBAAsB,IAAI,CAAC,CAAC;QACtE,MAAM,wBAAwB,GAAG,UAAU,CAAC,wBAAwB,IAAI,CAAC,CAAC;QAC1E,MAAM,aAAa,GAAG,UAAU,CAAC,aAAa,IAAI,KAAK,CAAC;QACxD,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;QACrC,MAAM,YAAY,GAAG,UAAU,CAAC,sBAAsB,CAAC;QACvD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,CAAC,CAAC;gBACvD,UAAU,EAAE,WAAW,CAAC;oBACtB,OAAO,EAAE,oBAAoB;oBAC7B,GAAG,QAAQ;iBACZ,CAAC,CAAC;YACL,CAAC;iBAAM,IAAI,GAAG,IAAI,GAAG,CAAC,OAAO,KAAK,qBAAqB,EAAE,CAAC;gBACxD,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,EAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,EAAE,aAAa,EAAC,CAAC,CAAC;YAC/F,CAAC;iBAAM,IAAI,GAAG,IAAI,GAAG,CAAC,OAAO,KAAK,sBAAsB,EAAE,CAAC;gBACzD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;gBACxD,UAAU,EAAE,WAAW,CAAC;oBACtB,OAAO,EAAE,qBAAqB;oBAC9B,GAAG,QAAQ;iBACZ,CAAC,CAAC;YACL,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,iBAAiB;QACtB,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IACnC,CAAC;IACD;;OAEG;IACI,kBAAkB;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IACpC,CAAC;CAEF","sourcesContent":["import { IClientPublishOptions } from \"mqtt\";\r\nimport { join } from \"path\";\r\nimport { parentPort } from \"worker_threads\";\r\n\r\nimport { basePath } from \"../base-path.js\";\r\nimport logger from \"../logger.js\";\r\nimport { IMqttWorkerData } from \"./mqtt-interfaces.js\";\r\nimport MqttProxy from \"./mqtt-proxy.js\";\r\nimport { ThrottledPublisher, ThrottledSubscriber } from \"./throttled-queue.js\";\r\n\r\nexport class MqttWorker {\r\n private mqttProxy: MqttProxy;\r\n protected publisher: ThrottledPublisher;\r\n protected subscriber: ThrottledSubscriber;\r\n\r\n constructor(workerData: IMqttWorkerData) {\r\n const publishThrottlingDelay = workerData.publishThrottlingDelay ?? 1;\r\n const subscribeThrottlingDelay = workerData.subscribeThrottlingDelay ?? 1;\r\n const persistToDisk = workerData.persistToDisk ?? false;\r\n const mqttHost = workerData.mqttHost;\r\n const instanceName = workerData.instanceNameWithSuffix;\r\n const mqttParameters = workerData.mqttParameters;\r\n const publisherActive = workerData.publisherActive;\r\n const subscriberActive = workerData.subscriberActive;\r\n const defaultPublishOptions = workerData.defaultPublishOptions;\r\n\r\n // Initialize and start the MQTT proxy.\r\n this.mqttProxy = new MqttProxy(mqttHost, instanceName, mqttParameters, this);\r\n this.mqttProxy.start();\r\n\r\n // Set up the event listener for incoming messages from the MQTT proxy.\r\n this.mqttProxy.event.on(\"input\", (event) => {\r\n this.subscriber.enqueue(event.topic, event.message.toString());\r\n });\r\n\r\n // Set up the event listener for status messages from the MQTT proxy.\r\n this.mqttProxy.event.on(\"mqttProxyStatus\", (event) => {\r\n parentPort?.postMessage({\r\n command: \"mqttProxyStatus\",\r\n event: event.event,\r\n value: event.value,\r\n statusTopic: event.statusTopic,\r\n uom: event.uom,\r\n });\r\n });\r\n\r\n // Define the publish function to be used by the ThrottledPublisher.\r\n const publishFunction = async (topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> => {\r\n const publishOptions = options ?? defaultPublishOptions;\r\n try {\r\n await this.mqttProxy.publish(topic, message, publishOptions);\r\n parentPort?.postMessage({\r\n command: \"enqueueResult\",\r\n id,\r\n status: \"success\",\r\n topic,\r\n message,\r\n options: publishOptions,\r\n });\r\n } catch (reason: any) {\r\n logger.error(`${instanceName} - Error publishing message to topic ${topic}: ${reason?.message ?? String(reason)}`);\r\n parentPort?.postMessage({\r\n command: \"enqueueResult\",\r\n id,\r\n status: \"error\",\r\n error: reason?.message ?? String(reason),\r\n });\r\n throw reason;\r\n }\r\n };\r\n\r\n // Create an instance of ThrottledPublisher.\r\n this.publisher = new ThrottledPublisher(\r\n publishThrottlingDelay,\r\n publishFunction,\r\n persistToDisk,\r\n join(basePath, \"/workerQueue/\", \"throttled-publisher-queue.json\"),\r\n instanceName,\r\n publisherActive,\r\n );\r\n\r\n // Define the message handler for incoming messages.\r\n const messageHandler = async (topic: string, message: string): Promise<void> => {\r\n parentPort?.postMessage({\r\n command: \"input\",\r\n topic: topic,\r\n message: message,\r\n });\r\n };\r\n\r\n // Create an instance of ThrottledSubscriber.\r\n this.subscriber = new ThrottledSubscriber(\r\n subscribeThrottlingDelay,\r\n messageHandler,\r\n persistToDisk,\r\n join(basePath, \"/workerQueue/\", \"throttled-subscriber-queue.json\"),\r\n instanceName,\r\n subscriberActive,\r\n );\r\n\r\n // Set up the message listener for commands from the main thread.\r\n this.initializeMessageListener();\r\n }\r\n\r\n /**\r\n * Listen for incoming messages from the main thread and process them.\r\n */\r\n private initializeMessageListener(): void {\r\n parentPort?.on(\"message\", async (msg) => {\r\n if (msg && msg.command === \"enqueue\" && msg.id && msg.topic && msg.message !== undefined) {\r\n try {\r\n await this.publisher.enqueue(msg.topic, msg.message, msg.id, msg.options);\r\n } catch (error: any) {\r\n // Error\r\n }\r\n } else if (msg && msg.command === \"subscribeAsync\" && msg.topics) {\r\n this.mqttProxy.subscribeAsync(msg.topics);\r\n } else if (msg && msg.command === \"unsubscribeAsync\" && msg.topics) {\r\n this.mqttProxy.unsubscribeAsync(msg.topics);\r\n } else if (msg && msg.command === \"setPublisherActive\") {\r\n this.publisher.becomeActive({batchSize: msg?.batchSize, referenceHash: msg?.referenceHash});\r\n } else if (msg && msg.command === \"setPublisherPassive\") {\r\n const snapshot = await this.publisher.becomePassive(3);\r\n parentPort?.postMessage({\r\n command: \"handover_publisher\",\r\n ...snapshot\r\n }); \r\n } else if (msg && msg.command === \"setSubscriberActive\") {\r\n this.subscriber.becomeActive({batchSize: msg?.batchSize, referenceHash: msg?.referenceHash});\r\n } else if (msg && msg.command === \"setSubscriberPassive\") {\r\n const snapshot = await this.subscriber.becomePassive(3);\r\n parentPort?.postMessage({\r\n command: \"handover_subscriber\",\r\n ...snapshot\r\n }); \r\n }\r\n });\r\n }\r\n\r\n /**\r\n * Get the state of the publisher.\r\n */\r\n public getPublisherState(): boolean {\r\n return this.publisher.getState();\r\n }\r\n /**\r\n * Get the state of the subscriber.\r\n */\r\n public getSubscriberState(): boolean {\r\n return this.subscriber.getState();\r\n }\r\n\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"mqtt-worker.js","sourceRoot":"","sources":["../../src/uns-mqtt/mqtt-worker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE/E,MAAM,OAAO,UAAU;IACb,SAAS,CAAY;IACnB,SAAS,CAAqB;IAC9B,UAAU,CAAsB;IAE1C,YAAY,UAA2B;QACrC,MAAM,sBAAsB,GAAG,UAAU,CAAC,sBAAsB,IAAI,CAAC,CAAC;QACtE,MAAM,kBAAkB,GAAG,UAAU,CAAC,kBAAkB,IAAI,CAAC,CAAC;QAC9D,MAAM,mBAAmB,GAAG,UAAU,CAAC,mBAAmB,CAAC;QAC3D,MAAM,wBAAwB,GAAG,UAAU,CAAC,wBAAwB,IAAI,CAAC,CAAC;QAC1E,MAAM,aAAa,GAAG,UAAU,CAAC,aAAa,IAAI,KAAK,CAAC;QACxD,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;QACrC,MAAM,YAAY,GAAG,UAAU,CAAC,sBAAsB,CAAC;QACvD,MAAM,cAAc,GAAG,UAAU,CAAC,cAAc,CAAC;QACjD,MAAM,eAAe,GAAG,UAAU,CAAC,eAAe,CAAC;QACnD,MAAM,gBAAgB,GAAG,U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AAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IACpC,CAAC;CAEF","sourcesContent":["import { IClientPublishOptions } from \"mqtt\";\nimport { join } from \"path\";\nimport { parentPort } from \"worker_threads\";\n\nimport { basePath } from \"../base-path.js\";\nimport logger from \"../logger.js\";\nimport { IMqttWorkerData } from \"./mqtt-interfaces.js\";\nimport MqttProxy from \"./mqtt-proxy.js\";\nimport { ThrottledPublisher, ThrottledSubscriber } from \"./throttled-queue.js\";\n\nexport class MqttWorker {\n private mqttProxy: MqttProxy;\n protected publisher: ThrottledPublisher;\n protected subscriber: ThrottledSubscriber;\n\n constructor(workerData: IMqttWorkerData) {\n const publishThrottlingDelay = workerData.publishThrottlingDelay ?? 1;\n const publishConcurrency = workerData.publishConcurrency ?? 1;\n const maxPendingPublishes = workerData.maxPendingPublishes;\n const subscribeThrottlingDelay = workerData.subscribeThrottlingDelay ?? 1;\n const persistToDisk = workerData.persistToDisk ?? false;\n const mqttHost = workerData.mqttHost;\n const instanceName = workerData.instanceNameWithSuffix;\n const mqttParameters = workerData.mqttParameters;\n const publisherActive = workerData.publisherActive;\n const subscriberActive = workerData.subscriberActive;\n const defaultPublishOptions = workerData.defaultPublishOptions;\n\n // Initialize and start the MQTT proxy.\n this.mqttProxy = new MqttProxy(mqttHost, instanceName, mqttParameters, this);\n this.mqttProxy.start();\n\n // Set up the event listener for incoming messages from the MQTT proxy.\n this.mqttProxy.event.on(\"input\", (event) => {\n this.subscriber.enqueue(event.topic, event.message.toString());\n });\n\n // Set up the event listener for status messages from the MQTT proxy.\n this.mqttProxy.event.on(\"mqttProxyStatus\", (event) => {\n parentPort?.postMessage({\n command: \"mqttProxyStatus\",\n event: event.event,\n value: event.value,\n statusTopic: event.statusTopic,\n uom: event.uom,\n });\n });\n\n // Define the publish function to be used by the ThrottledPublisher.\n const publishFunction = async (topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> => {\n const publishOptions = options ?? defaultPublishOptions;\n try {\n await this.mqttProxy.publish(topic, message, publishOptions);\n } catch (reason: any) {\n logger.error(`${instanceName} - Error publishing message to topic ${topic}: ${reason?.message ?? String(reason)}`);\n throw reason;\n }\n };\n\n // Create an instance of ThrottledPublisher.\n this.publisher = new ThrottledPublisher(\n publishThrottlingDelay,\n publishFunction,\n persistToDisk,\n join(basePath, \"/workerQueue/\", \"throttled-publisher-queue.json\"),\n instanceName,\n publisherActive,\n publishConcurrency,\n maxPendingPublishes,\n );\n\n // Define the message handler for incoming messages.\n const messageHandler = async (topic: string, message: string): Promise<void> => {\n parentPort?.postMessage({\n command: \"input\",\n topic: topic,\n message: message,\n });\n };\n\n // Create an instance of ThrottledSubscriber.\n this.subscriber = new ThrottledSubscriber(\n subscribeThrottlingDelay,\n messageHandler,\n persistToDisk,\n join(basePath, \"/workerQueue/\", \"throttled-subscriber-queue.json\"),\n instanceName,\n subscriberActive,\n );\n\n // Set up the message listener for commands from the main thread.\n this.initializeMessageListener();\n }\n\n /**\n * Listen for incoming messages from the main thread and process them.\n */\n private initializeMessageListener(): void {\n parentPort?.on(\"message\", async (msg) => {\n if (msg && msg.command === \"enqueue\" && msg.id && msg.topic && msg.message !== undefined) {\n try {\n const publishPromise = this.publisher.enqueueOrThrow(msg.topic, msg.message, msg.id, msg.options);\n parentPort?.postMessage({\n command: \"enqueueAccepted\",\n id: msg.id,\n status: \"accepted\",\n });\n void publishPromise\n .then(() => {\n parentPort?.postMessage({\n command: \"publishResult\",\n id: msg.id,\n status: \"success\",\n topic: msg.topic,\n });\n })\n .catch((error: any) => {\n parentPort?.postMessage({\n command: \"publishResult\",\n id: msg.id,\n status: \"error\",\n topic: msg.topic,\n error: error?.message ?? String(error),\n });\n });\n } catch (error: any) {\n parentPort?.postMessage({\n command: \"enqueueAccepted\",\n id: msg.id,\n status: \"error\",\n error: error?.message ?? String(error),\n });\n }\n } else if (msg && msg.command === \"subscribeAsync\" && msg.topics) {\n this.mqttProxy.subscribeAsync(msg.topics);\n } else if (msg && msg.command === \"unsubscribeAsync\" && msg.topics) {\n this.mqttProxy.unsubscribeAsync(msg.topics);\n } else if (msg && msg.command === \"setPublisherActive\") {\n this.publisher.becomeActive({batchSize: msg?.batchSize, referenceHash: msg?.referenceHash});\n } else if (msg && msg.command === \"setPublisherPassive\") {\n const snapshot = await this.publisher.becomePassive(3);\n parentPort?.postMessage({\n command: \"handover_publisher\",\n ...snapshot\n }); \n } else if (msg && msg.command === \"setSubscriberActive\") {\n this.subscriber.becomeActive({batchSize: msg?.batchSize, referenceHash: msg?.referenceHash});\n } else if (msg && msg.command === \"setSubscriberPassive\") {\n const snapshot = await this.subscriber.becomePassive(3);\n parentPort?.postMessage({\n command: \"handover_subscriber\",\n ...snapshot\n }); \n }\n });\n }\n\n /**\n * Get the state of the publisher.\n */\n public getPublisherState(): boolean {\n return this.publisher.getState();\n }\n /**\n * Get the state of the subscriber.\n */\n public getSubscriberState(): boolean {\n return this.subscriber.getState();\n }\n\n}\n"]}
|
|
@@ -7,6 +7,8 @@ declare abstract class ThrottledQueue<T> {
|
|
|
7
7
|
protected queue: T[];
|
|
8
8
|
protected lastProcessedItems: T[];
|
|
9
9
|
protected isProcessing: boolean;
|
|
10
|
+
protected inFlight: number;
|
|
11
|
+
protected maxConcurrency: number;
|
|
10
12
|
delay: number;
|
|
11
13
|
protected persistToDisk: boolean;
|
|
12
14
|
protected persistenceFilePath: string;
|
|
@@ -14,15 +16,17 @@ declare abstract class ThrottledQueue<T> {
|
|
|
14
16
|
protected instanceName: string;
|
|
15
17
|
protected active: boolean;
|
|
16
18
|
private inactiveLogSent;
|
|
17
|
-
constructor(delay: number, persistToDisk: boolean, instanceName: string);
|
|
19
|
+
constructor(delay: number, persistToDisk: boolean, instanceName: string, maxConcurrency?: number);
|
|
18
20
|
/**
|
|
19
21
|
* Process the queue items one at a time with a delay between each.
|
|
20
22
|
*/
|
|
21
23
|
protected processQueue(): Promise<void>;
|
|
24
|
+
private processItemWithBookkeeping;
|
|
22
25
|
/**
|
|
23
26
|
* Log changes to the queue size at significant thresholds.
|
|
24
27
|
*/
|
|
25
28
|
protected logQueueSize(): void;
|
|
29
|
+
protected getPendingCount(): number;
|
|
26
30
|
/**
|
|
27
31
|
* Save the current queue to disk.
|
|
28
32
|
*/
|
|
@@ -61,6 +65,7 @@ interface PublisherQueueItem {
|
|
|
61
65
|
*/
|
|
62
66
|
export declare class ThrottledPublisher extends ThrottledQueue<PublisherQueueItem> {
|
|
63
67
|
private publishFunction;
|
|
68
|
+
private maxPendingPublishes?;
|
|
64
69
|
/**
|
|
65
70
|
* @param delay Delay between messages in milliseconds.
|
|
66
71
|
* @param publishFunction Function to perform the actual publish.
|
|
@@ -68,11 +73,17 @@ export declare class ThrottledPublisher extends ThrottledQueue<PublisherQueueIte
|
|
|
68
73
|
* @param persistenceFilePath File path for queue persistence.
|
|
69
74
|
* @param instanceName Unique instance name for logging.
|
|
70
75
|
*/
|
|
71
|
-
constructor(delay: number, publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>, persistToDisk: boolean, persistenceFilePath: string, instanceName: string, active: boolean);
|
|
76
|
+
constructor(delay: number, publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>, persistToDisk: boolean, persistenceFilePath: string, instanceName: string, active: boolean, maxConcurrency?: number, maxPendingPublishes?: number);
|
|
72
77
|
/**
|
|
73
78
|
* Enqueue a publish request.
|
|
74
79
|
*/
|
|
75
80
|
enqueue(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Enqueue a publish request and throw synchronously if the bounded queue is full.
|
|
83
|
+
*/
|
|
84
|
+
enqueueOrThrow(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void>;
|
|
85
|
+
private assertCanAcceptPublish;
|
|
86
|
+
private enqueueAcceptedPublish;
|
|
76
87
|
private computeHashForBatch;
|
|
77
88
|
/**
|
|
78
89
|
* Switch the publisher to a passive state and return a snapshot
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttled-queue.d.ts","sourceRoot":"","sources":["../../src/uns-mqtt/throttled-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAM7C;;;GAGG;AACH,uBAAe,cAAc,CAAC,CAAC;IAC7B,SAAS,CAAC,KAAK,EAAE,CAAC,EAAE,CAAM;IAC1B,SAAS,CAAC,kBAAkB,EAAE,CAAC,EAAE,CAAM;IACvC,SAAS,CAAC,YAAY,UAAS;
|
|
1
|
+
{"version":3,"file":"throttled-queue.d.ts","sourceRoot":"","sources":["../../src/uns-mqtt/throttled-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAM7C;;;GAGG;AACH,uBAAe,cAAc,CAAC,CAAC;IAC7B,SAAS,CAAC,KAAK,EAAE,CAAC,EAAE,CAAM;IAC1B,SAAS,CAAC,kBAAkB,EAAE,CAAC,EAAE,CAAM;IACvC,SAAS,CAAC,YAAY,UAAS;IAC/B,SAAS,CAAC,QAAQ,SAAK;IACvB,SAAS,CAAC,cAAc,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,aAAa,EAAE,OAAO,CAAC;IACjC,SAAS,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACtC,SAAS,CAAC,kBAAkB,SAAK;IACjC,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAC/B,SAAS,CAAC,MAAM,EAAE,OAAO,CAAQ;IACjC,OAAO,CAAC,eAAe,CAAS;gBAEpB,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,GAAE,MAAU;IAWnG;;OAEG;cACa,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;YAiD/B,0BAA0B;IA4BxC;;OAEG;IACH,SAAS,CAAC,YAAY,IAAI,IAAI;IAgB9B,SAAS,CAAC,eAAe,IAA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e,EAC9B,mBAAmB,EAAE,MAA+E,EACpG,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,OAAO;IAajB;;OAEG;IACI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAepD;;OAEG;cACa,WAAW,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrE;;OAEG;IACH,SAAS,CAAC,aAAa,CAAC,IAAI,EAAE,mBAAmB,GAAG,GAAG;IAMvD;;OAEG;IACH,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,GAAG,GAAG,mBAAmB;IAQzD;;OAEG;IACI,QAAQ,IAAI,OAAO;IAI1B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAK3B;;OAEG;IACU,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;KAAE,CAAC;IAY7F;;OAEG;IACI,YAAY,CAAC,QAAQ,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;CAoClF"}
|
|
@@ -11,6 +11,8 @@ class ThrottledQueue {
|
|
|
11
11
|
queue = [];
|
|
12
12
|
lastProcessedItems = [];
|
|
13
13
|
isProcessing = false;
|
|
14
|
+
inFlight = 0;
|
|
15
|
+
maxConcurrency;
|
|
14
16
|
delay;
|
|
15
17
|
persistToDisk;
|
|
16
18
|
persistenceFilePath;
|
|
@@ -18,10 +20,11 @@ class ThrottledQueue {
|
|
|
18
20
|
instanceName;
|
|
19
21
|
active = true;
|
|
20
22
|
inactiveLogSent = false;
|
|
21
|
-
constructor(delay, persistToDisk, instanceName) {
|
|
23
|
+
constructor(delay, persistToDisk, instanceName, maxConcurrency = 1) {
|
|
22
24
|
this.delay = delay;
|
|
23
25
|
this.persistToDisk = persistToDisk;
|
|
24
26
|
this.instanceName = instanceName;
|
|
27
|
+
this.maxConcurrency = Math.max(1, maxConcurrency);
|
|
25
28
|
if (this.persistToDisk) {
|
|
26
29
|
this.loadQueueFromDisk();
|
|
27
30
|
}
|
|
@@ -32,7 +35,6 @@ class ThrottledQueue {
|
|
|
32
35
|
async processQueue() {
|
|
33
36
|
if (this.isProcessing)
|
|
34
37
|
return;
|
|
35
|
-
this.isProcessing = true;
|
|
36
38
|
if (!this.active) {
|
|
37
39
|
if (!this.inactiveLogSent) {
|
|
38
40
|
logger.info(`${this.instanceName} - Queue is not active. Exiting processQueue.`);
|
|
@@ -41,57 +43,84 @@ class ThrottledQueue {
|
|
|
41
43
|
this.isProcessing = false;
|
|
42
44
|
return;
|
|
43
45
|
}
|
|
46
|
+
this.isProcessing = true;
|
|
44
47
|
// Reset the flag when processing actually starts.
|
|
45
48
|
this.inactiveLogSent = false;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
const item = this.queue.shift();
|
|
54
|
-
if (item) {
|
|
55
|
-
try {
|
|
56
|
-
await this.processItem(item);
|
|
57
|
-
// Only store 10 last processed items
|
|
58
|
-
if (this.lastProcessedItems.length >= 10) {
|
|
59
|
-
this.lastProcessedItems.shift();
|
|
60
|
-
}
|
|
61
|
-
// Store the last processed item
|
|
62
|
-
this.lastProcessedItems.push(item);
|
|
49
|
+
try {
|
|
50
|
+
while (this.queue.length > 0 && this.inFlight < this.maxConcurrency) {
|
|
51
|
+
// Check if the queue is active
|
|
52
|
+
if (!this.active) {
|
|
53
|
+
logger.info(`${this.instanceName} - Queue processing paused.`);
|
|
54
|
+
return; // Exit the loop and stop processing
|
|
63
55
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
const item = this.queue.shift();
|
|
57
|
+
if (!item) {
|
|
58
|
+
continue;
|
|
67
59
|
}
|
|
60
|
+
this.inFlight += 1;
|
|
61
|
+
void this.processItemWithBookkeeping(item);
|
|
68
62
|
if (this.persistToDisk) {
|
|
69
63
|
this.saveQueueToDisk();
|
|
70
64
|
}
|
|
71
|
-
|
|
65
|
+
this.logQueueSize();
|
|
66
|
+
if (this.delay > 0 && this.queue.length > 0 && this.inFlight < this.maxConcurrency) {
|
|
72
67
|
await new Promise((resolve) => setTimeout(resolve, this.delay));
|
|
73
68
|
}
|
|
74
|
-
this.logQueueSize();
|
|
75
69
|
}
|
|
76
70
|
}
|
|
77
|
-
|
|
71
|
+
finally {
|
|
72
|
+
this.isProcessing = false;
|
|
73
|
+
if (this.active && this.queue.length > 0 && this.inFlight < this.maxConcurrency) {
|
|
74
|
+
void this.processQueue();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async processItemWithBookkeeping(item) {
|
|
79
|
+
try {
|
|
80
|
+
await this.processItem(item);
|
|
81
|
+
// Only store 10 last processed items
|
|
82
|
+
if (this.lastProcessedItems.length >= 10) {
|
|
83
|
+
this.lastProcessedItems.shift();
|
|
84
|
+
}
|
|
85
|
+
// Store the last processed item
|
|
86
|
+
this.lastProcessedItems.push(item);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
// Specific error handling can be done in the subclass if needed.
|
|
90
|
+
logger.error(`${this.instanceName} - Error processing item: ${error.message}`);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
this.inFlight = Math.max(0, this.inFlight - 1);
|
|
94
|
+
if (this.persistToDisk) {
|
|
95
|
+
this.saveQueueToDisk();
|
|
96
|
+
}
|
|
97
|
+
this.logQueueSize();
|
|
98
|
+
// Check if the queue is active
|
|
99
|
+
if (this.active && this.queue.length > 0) {
|
|
100
|
+
void this.processQueue();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
78
103
|
}
|
|
79
104
|
/**
|
|
80
105
|
* Log changes to the queue size at significant thresholds.
|
|
81
106
|
*/
|
|
82
107
|
logQueueSize() {
|
|
83
108
|
const className = `${this.constructor.name.includes("Publisher") ? "Publisher" : this.constructor.name.includes("Subscriber") ? "Subscriber" : "Unknown"}`;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
&&
|
|
87
|
-
|
|
88
|
-
this.
|
|
109
|
+
const pendingCount = this.getPendingCount();
|
|
110
|
+
if (pendingCount > 1
|
|
111
|
+
&& pendingCount > this.previousLoggedSize
|
|
112
|
+
&& Math.floor(pendingCount / 100) > Math.floor(this.previousLoggedSize / 100)) {
|
|
113
|
+
logger.info(`${this.instanceName} - ${className} queue size length is ${pendingCount}.`);
|
|
114
|
+
this.previousLoggedSize = pendingCount;
|
|
89
115
|
}
|
|
90
|
-
else if (
|
|
116
|
+
else if (pendingCount === 0 && this.previousLoggedSize > 0) {
|
|
91
117
|
logger.info(`${this.instanceName} - ${className} queue is empty.`);
|
|
92
118
|
this.previousLoggedSize = 0;
|
|
93
119
|
}
|
|
94
120
|
}
|
|
121
|
+
getPendingCount() {
|
|
122
|
+
return this.queue.length + this.inFlight;
|
|
123
|
+
}
|
|
95
124
|
/**
|
|
96
125
|
* Save the current queue to disk.
|
|
97
126
|
*/
|
|
@@ -131,6 +160,7 @@ class ThrottledQueue {
|
|
|
131
160
|
*/
|
|
132
161
|
export class ThrottledPublisher extends ThrottledQueue {
|
|
133
162
|
publishFunction;
|
|
163
|
+
maxPendingPublishes;
|
|
134
164
|
/**
|
|
135
165
|
* @param delay Delay between messages in milliseconds.
|
|
136
166
|
* @param publishFunction Function to perform the actual publish.
|
|
@@ -138,11 +168,12 @@ export class ThrottledPublisher extends ThrottledQueue {
|
|
|
138
168
|
* @param persistenceFilePath File path for queue persistence.
|
|
139
169
|
* @param instanceName Unique instance name for logging.
|
|
140
170
|
*/
|
|
141
|
-
constructor(delay, publishFunction, persistToDisk = false, persistenceFilePath = join(basePath, "/publisherQueue/", "throttled-publisher-queue.json"), instanceName, active) {
|
|
142
|
-
super(delay, persistToDisk, instanceName);
|
|
171
|
+
constructor(delay, publishFunction, persistToDisk = false, persistenceFilePath = join(basePath, "/publisherQueue/", "throttled-publisher-queue.json"), instanceName, active, maxConcurrency = 1, maxPendingPublishes) {
|
|
172
|
+
super(delay, persistToDisk, instanceName, maxConcurrency);
|
|
143
173
|
this.persistenceFilePath = persistenceFilePath;
|
|
144
174
|
this.publishFunction = publishFunction;
|
|
145
175
|
this.active = active;
|
|
176
|
+
this.maxPendingPublishes = maxPendingPublishes;
|
|
146
177
|
if (active) {
|
|
147
178
|
logger.info(`${this.instanceName} - Publisher is active.`);
|
|
148
179
|
}
|
|
@@ -154,6 +185,27 @@ export class ThrottledPublisher extends ThrottledQueue {
|
|
|
154
185
|
* Enqueue a publish request.
|
|
155
186
|
*/
|
|
156
187
|
enqueue(topic, message, id, options) {
|
|
188
|
+
try {
|
|
189
|
+
this.assertCanAcceptPublish();
|
|
190
|
+
return this.enqueueAcceptedPublish(topic, message, id, options);
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
return Promise.reject(error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Enqueue a publish request and throw synchronously if the bounded queue is full.
|
|
198
|
+
*/
|
|
199
|
+
enqueueOrThrow(topic, message, id, options) {
|
|
200
|
+
this.assertCanAcceptPublish();
|
|
201
|
+
return this.enqueueAcceptedPublish(topic, message, id, options);
|
|
202
|
+
}
|
|
203
|
+
assertCanAcceptPublish() {
|
|
204
|
+
if (this.maxPendingPublishes !== undefined && this.getPendingCount() >= this.maxPendingPublishes) {
|
|
205
|
+
throw new Error(`${this.instanceName} - Publisher queue is full (${this.maxPendingPublishes}).`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
enqueueAcceptedPublish(topic, message, id, options) {
|
|
157
209
|
return new Promise((resolve, reject) => {
|
|
158
210
|
this.queue.push({ topic, message, id, options, resolve, reject });
|
|
159
211
|
// Trigger logging after a new message is added.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttled-queue.js","sourceRoot":"","sources":["../../src/u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EAAE,aAAa,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;QAC9C,IAAI,WAAW,GAAG,CAAC,CAAC,CAAC;QAErB,sCAAsC;QACtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;YACxD,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC;YACxD,MAAM,WAAW,GAAG,IAAI,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC;YAC3D,IAAI,WAAW,KAAK,aAAa,EAAE,CAAC;gBAClC,WAAW,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC,6CAA6C;gBAC1E,MAAM;YACR,CAAC;QACH,CAAC;QAED,IAAI,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;YACvB,wEAAwE;YACxE,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;YACjD,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;YACxB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,2DAA2D,CAAC,CAAC;QAC/F,CAAC;aAAM,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACjC,sEAAsE;YACtE,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,qFAAqF,CAAC,CAAC;QACzH,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,8BAA8B,CAAC,CAAC;QAChE,IAAI,CAAC,YAAY,EAAE,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { createHash } from \"crypto\";\r\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\r\nimport { IClientPublishOptions } from \"mqtt\";\r\nimport { join } from \"path\";\r\n\r\nimport { basePath } from \"../base-path.js\";\r\nimport logger from \"../logger.js\";\r\n\r\n/**\r\n * Abstract base class that encapsulates common functionality for managing\r\n * a throttled in‑memory queue with an optional disk persistence.\r\n */\r\nabstract class ThrottledQueue<T> {\r\n protected queue: T[] = [];\r\n protected lastProcessedItems: T[] = [];\r\n protected isProcessing = false;\r\n public delay: number;\r\n protected persistToDisk: boolean;\r\n protected persistenceFilePath: string;\r\n protected previousLoggedSize = 0;\r\n protected instanceName: string;\r\n protected active: boolean = true;\r\n private inactiveLogSent = false;\r\n\r\n constructor(delay: number, persistToDisk: boolean, instanceName: string) {\r\n this.delay = delay;\r\n this.persistToDisk = persistToDisk;\r\n this.instanceName = instanceName;\r\n\r\n if (this.persistToDisk) {\r\n this.loadQueueFromDisk();\r\n }\r\n }\r\n\r\n /**\r\n * Process the queue items one at a time with a delay between each.\r\n */\r\n protected async processQueue(): Promise<void> {\r\n if (this.isProcessing) return;\r\n this.isProcessing = true;\r\n\r\n if (!this.active) {\r\n if (!this.inactiveLogSent) {\r\n logger.info(`${this.instanceName} - Queue is not active. Exiting processQueue.`);\r\n this.inactiveLogSent = true;\r\n }\r\n this.isProcessing = false;\r\n return;\r\n }\r\n // Reset the flag when processing actually starts.\r\n this.inactiveLogSent = false;\r\n \r\n while (this.queue.length > 0) {\r\n // Check if the queue is active\r\n if (!this.active) {\r\n logger.info(`${this.instanceName} - Queue processing paused.`);\r\n this.isProcessing = false;\r\n return; // Exit the loop and stop processing\r\n }\r\n\r\n const item = this.queue.shift();\r\n if (item) {\r\n try {\r\n await this.processItem(item);\r\n // Only store 10 last processed items\r\n if (this.lastProcessedItems.length >= 10) {\r\n this.lastProcessedItems.shift();\r\n }\r\n // Store the last processed item\r\n this.lastProcessedItems.push(item);\r\n } catch (error) {\r\n // Specific error handling can be done in the subclass if needed.\r\n logger.error(`${this.instanceName} - Error processing item: ${(error as Error).message}`);\r\n }\r\n\r\n if (this.persistToDisk) {\r\n this.saveQueueToDisk();\r\n }\r\n\r\n if (this.delay > 0) {\r\n await new Promise((resolve) => setTimeout(resolve, this.delay));\r\n }\r\n\r\n this.logQueueSize();\r\n }\r\n }\r\n\r\n this.isProcessing = false;\r\n }\r\n\r\n /**\r\n * Log changes to the queue size at significant thresholds.\r\n */\r\n protected logQueueSize(): void {\r\n const className = `${this.constructor.name.includes(\"Publisher\") ? \"Publisher\" : this.constructor.name.includes(\"Subscriber\") ? \"Subscriber\" : \"Unknown\"}`;\r\n if (\r\n this.queue.length > 1\r\n && this.queue.length > this.previousLoggedSize\r\n && Math.floor(this.queue.length / 100) > Math.floor(this.previousLoggedSize / 100)\r\n ) {\r\n logger.info(`${this.instanceName} - ${className} queue size length is ${this.queue.length}.`);\r\n this.previousLoggedSize = this.queue.length;\r\n } else if (this.queue.length === 0 && this.previousLoggedSize > 0) {\r\n logger.info(`${this.instanceName} - ${className} queue is empty.`);\r\n this.previousLoggedSize = 0;\r\n }\r\n }\r\n\r\n /**\r\n * Save the current queue to disk.\r\n */\r\n protected saveQueueToDisk(): void {\r\n try {\r\n const queueData = this.queue.map((item) => this.serializeItem(item));\r\n const dir = join(this.persistenceFilePath, \"..\");\r\n if (!existsSync(dir)) {\r\n mkdirSync(dir, { recursive: true });\r\n }\r\n writeFileSync(this.persistenceFilePath, JSON.stringify(queueData, null, 2));\r\n logger.debug(`${this.instanceName} - Queue saved to disk.`);\r\n } catch (error: any) {\r\n logger.error(`${this.instanceName} - Error saving queue to disk: ${error.message}`);\r\n }\r\n }\r\n\r\n /**\r\n * Load the queue from disk.\r\n */\r\n protected loadQueueFromDisk(): void {\r\n try {\r\n if (existsSync(this.persistenceFilePath)) {\r\n const queueData = JSON.parse(readFileSync(this.persistenceFilePath, \"utf8\"));\r\n this.queue = queueData.map((data: any) => this.deserializeItem(data));\r\n logger.info(`${this.instanceName} - Queue loaded from disk.`);\r\n }\r\n } catch (error: any) {\r\n logger.error(`${this.instanceName} - Error loading queue from disk: ${error.message}`);\r\n }\r\n }\r\n\r\n /**\r\n * Process a single queue item.\r\n */\r\n protected abstract processItem(item: T): Promise<void>;\r\n\r\n /**\r\n * Serialize a queue item for persistence.\r\n */\r\n protected abstract serializeItem(item: T): any;\r\n\r\n /**\r\n * Deserialize persisted data into a queue item.\r\n */\r\n protected abstract deserializeItem(data: any): T;\r\n}\r\n\r\n/**\r\n * Interface representing a queued publish item.\r\n */\r\ninterface PublisherQueueItem {\r\n topic: string;\r\n message: string;\r\n id: string;\r\n options?: IClientPublishOptions;\r\n resolve: () => void;\r\n reject: (err: any) => void;\r\n}\r\n\r\n/**\r\n * ThrottledPublisher: manages a queue of outgoing publish requests,\r\n * sending one message at a time using a provided publish function.\r\n */\r\nexport class ThrottledPublisher extends ThrottledQueue<PublisherQueueItem> {\r\n private publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>;\r\n\r\n /**\r\n * @param delay Delay between messages in milliseconds.\r\n * @param publishFunction Function to perform the actual publish.\r\n * @param persistToDisk Whether to persist the queue to disk.\r\n * @param persistenceFilePath File path for queue persistence.\r\n * @param instanceName Unique instance name for logging.\r\n */\r\n constructor(\r\n delay: number,\r\n publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>,\r\n persistToDisk: boolean = false,\r\n persistenceFilePath: string = join(basePath, \"/publisherQueue/\", \"throttled-publisher-queue.json\"),\r\n instanceName: string,\r\n active: boolean,\r\n ) {\r\n super(delay, persistToDisk, instanceName);\r\n this.persistenceFilePath = persistenceFilePath;\r\n this.publishFunction = publishFunction;\r\n this.active = active;\r\n if (active) {\r\n logger.info(`${this.instanceName} - Publisher is active.`);\r\n } else {\r\n logger.info(`${this.instanceName} - Publisher is passive.`);\r\n }\r\n }\r\n\r\n /**\r\n * Enqueue a publish request.\r\n */\r\n public enqueue(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> {\r\n return new Promise((resolve, reject) => {\r\n this.queue.push({ topic, message, id, options, resolve, reject });\r\n\r\n // Trigger logging after a new message is added.\r\n this.logQueueSize(); \r\n\r\n if (this.persistToDisk) {\r\n this.saveQueueToDisk();\r\n }\r\n\r\n if (this.active) {\r\n this.processQueue();\r\n }\r\n });\r\n }\r\n\r\n private computeHashForBatch(messages: any[]): string {\r\n const data = JSON.stringify(messages);\r\n return createHash(\"sha256\").update(data).digest(\"hex\");\r\n }\r\n\r\n /**\r\n * Switch the publisher to a passive state and return a snapshot\r\n */\r\n public becomeActive(snapshot: { referenceHash: string; batchSize: number }): void {\r\n // If snapshot is empty (batchSize is zero), start processing from the beginning.\r\n if (snapshot.batchSize === 0 || snapshot.batchSize === undefined || snapshot.referenceHash === undefined) {\r\n this.active = true;\r\n logger.info(`${this.instanceName} - Publisher became active.`);\r\n this.processQueue();\r\n return;\r\n }\r\n\r\n const { referenceHash, batchSize } = snapshot;\r\n let resumeIndex = -1;\r\n\r\n // Use batchSize as the window length.\r\n for (let i = 0; i <= this.queue.length - batchSize; i++) {\r\n const currentBatch = this.queue.slice(i, i + batchSize);\r\n const currentHash = this.computeHashForBatch(currentBatch);\r\n if (currentHash === referenceHash) {\r\n resumeIndex = i + batchSize; // resume immediately after the matched batch\r\n break;\r\n }\r\n }\r\n\r\n if (resumeIndex !== -1) {\r\n // Adjust the queue so that processing resumes after the matching batch.\r\n this.queue = this.queue.slice(resumeIndex);\r\n logger.info(`${this.instanceName} - Publisher resumed from the matched snapshot position.`);\r\n } else if (this.queue.length > 0) {\r\n // Log info message and process the entire queue if no match is found.\r\n logger.info(`${this.instanceName} - No matching batch found during resume; processing the entire publisher's queue.`);\r\n }\r\n\r\n this.active = true;\r\n logger.info(`${this.instanceName} - Publisher became active.`);\r\n this.processQueue();\r\n }\r\n\r\n /**\r\n * Switch the publisher to a passive state and return a snapshot\r\n */\r\n public async becomePassive(n: number): Promise<{ referenceHash: string; batchSize: number; }> {\r\n this.active = false;\r\n logger.info(`${this.instanceName} - Publisher became passive.`);\r\n const batch = this.queue.slice(-n);\r\n const batchSize = batch.length;\r\n const referenceHash = this.computeHashForBatch(batch);\r\n if (batchSize > 0) {\r\n logger.info(`${this.instanceName} - Publisher batch reference hash: ${referenceHash}, size: ${batchSize}`);\r\n }\r\n return { referenceHash, batchSize };\r\n }\r\n\r\n public getState(): boolean {\r\n return this.active;\r\n }\r\n\r\n /**\r\n * Process a single publish request.\r\n */\r\n protected async processItem(item: PublisherQueueItem): Promise<void> {\r\n try {\r\n await this.publishFunction(item.topic, item.message, item.id, item.options);\r\n item.resolve();\r\n } catch (error) {\r\n item.reject(error);\r\n }\r\n }\r\n\r\n /**\r\n * Serialize a publish request for persistence.\r\n */\r\n protected serializeItem(item: PublisherQueueItem): any {\r\n return {\r\n topic: item.topic,\r\n message: item.message,\r\n id: item.id,\r\n options: item.options,\r\n };\r\n }\r\n\r\n /**\r\n * Deserialize persisted data into a publish request.\r\n */\r\n protected deserializeItem(data: any): PublisherQueueItem {\r\n return {\r\n topic: data.topic,\r\n message: data.message,\r\n id: data.id,\r\n options: data.options,\r\n resolve: () => {},\r\n reject: (err: any) => logger.error(`${this.instanceName} - Error processing queued publish item: ${err.message}`),\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Interface representing a queued subscribe item.\r\n */\r\ninterface SubscriberQueueItem {\r\n topic: string;\r\n message: string;\r\n timestamp: number;\r\n}\r\n\r\n/**\r\n * ThrottledSubscriber: manages a queue of incoming messages and processes\r\n * them one at a time using a provided message handler.\r\n */\r\nexport class ThrottledSubscriber extends ThrottledQueue<SubscriberQueueItem> {\r\n private messageHandler: (topic: string, message: string) => Promise<void>;\r\n\r\n /**\r\n * @param delay Delay between processing messages in milliseconds.\r\n * @param messageHandler Function to process incoming messages.\r\n * @param persistToDisk Whether to persist the queue to disk.\r\n * @param persistenceFilePath File path for queue persistence.\r\n * @param instanceName Unique instance name for logging.\r\n */\r\n constructor(\r\n delay: number,\r\n messageHandler: (topic: string, message: string) => Promise<void>,\r\n persistToDisk: boolean = false,\r\n persistenceFilePath: string = join(basePath, \"/subscriberQueue/\", \"throttled-subscriber-queue.json\"),\r\n instanceName: string,\r\n active: boolean,\r\n ) {\r\n super(delay, persistToDisk, instanceName);\r\n this.persistenceFilePath = persistenceFilePath;\r\n this.messageHandler = messageHandler;\r\n this.active = active;\r\n if (active) {\r\n logger.info(`${this.instanceName} - Subscriber is active.`);\r\n } else {\r\n logger.info(`${this.instanceName} - Subscriber is passive.`);\r\n }\r\n }\r\n\r\n /**\r\n * Enqueue subscribe messages for processing.\r\n */\r\n public enqueue(topic: string, message: string): void {\r\n this.queue.push({ topic, message, timestamp: Date.now() });\r\n \r\n // Trigger logging after a new message is added.\r\n this.logQueueSize();\r\n \r\n if (this.persistToDisk) {\r\n this.saveQueueToDisk();\r\n }\r\n\r\n if (this.active) {\r\n this.processQueue();\r\n }\r\n }\r\n\r\n /**\r\n * Process a single subscribe message.\r\n */\r\n protected async processItem(item: SubscriberQueueItem): Promise<void> {\r\n await this.messageHandler(item.topic, item.message);\r\n }\r\n\r\n /**\r\n * Serialize a subscribe message for persistence.\r\n */\r\n protected serializeItem(item: SubscriberQueueItem): any {\r\n return {\r\n message: item.message,\r\n };\r\n }\r\n\r\n /**\r\n * Deserialize persisted data into a subscribe message.\r\n */\r\n protected deserializeItem(data: any): SubscriberQueueItem {\r\n return {\r\n topic: data.topic,\r\n message: data.message,\r\n timestamp: data.timestamp,\r\n };\r\n }\r\n\r\n /**\r\n * Get the current state of the subscriber (active or passive).\r\n */\r\n public getState(): boolean {\r\n return this.active;\r\n }\r\n\r\n /**\r\n * Computes a SHA‑256 hash for a given batch of subscriber items.\r\n */\r\n private computeHashForBatch(messages: SubscriberQueueItem[]): string {\r\n const data = JSON.stringify(messages.map((item) => this.serializeItem(item)));\r\n return createHash(\"sha256\").update(data).digest(\"hex\");\r\n }\r\n\r\n /**\r\n * Switch the subscriber to a passive state and return a snapshot.\r\n */\r\n public async becomePassive(n: number): Promise<{ referenceHash: string; batchSize: number; }> {\r\n this.active = false;\r\n logger.info(`${this.instanceName} - Subscriber is passive.`);\r\n const batch = this.lastProcessedItems.slice(-n);\r\n const batchSize = batch.length;\r\n const referenceHash = this.computeHashForBatch(batch);\r\n if (batchSize > 0) {\r\n logger.info(`${this.instanceName} - Subscriber batch reference hash: ${referenceHash}, size: ${batchSize}`);\r\n }\r\n return { referenceHash, batchSize };\r\n }\r\n\r\n /**\r\n * Switch the subscriber to an active state and resume processing.\r\n */\r\n public becomeActive(snapshot: { referenceHash: string; batchSize: number }): void {\r\n // If snapshot is empty (batchSize is zero), start processing from the beginning.\r\n if (snapshot.batchSize === 0 || snapshot.batchSize === undefined || snapshot.referenceHash === undefined) {\r\n this.active = true;\r\n logger.info(`${this.instanceName} - Subscriber became active.`); \r\n this.processQueue();\r\n return;\r\n }\r\n\r\n const { referenceHash, batchSize } = snapshot;\r\n let resumeIndex = -1;\r\n\r\n // Use batchSize as the window length.\r\n for (let i = 0; i <= this.queue.length - batchSize; i++) {\r\n const currentBatch = this.queue.slice(i, i + batchSize);\r\n const currentHash = this.computeHashForBatch(currentBatch);\r\n if (currentHash === referenceHash) {\r\n resumeIndex = i + batchSize; // resume immediately after the matched batch\r\n break;\r\n }\r\n }\r\n\r\n if (resumeIndex !== -1) {\r\n // Adjust the queue so that processing resumes after the matching batch.\r\n const queueSlice = this.queue.slice(resumeIndex); \r\n this.queue = queueSlice;\r\n logger.info(`${this.instanceName} - Subscriber resumed from the matched snapshot position.`);\r\n } else if (this.queue.length > 0) {\r\n // Log info message and process the entire queue if no match is found.\r\n logger.info(`${this.instanceName} - No matching batch found during resume; processing the entire subscriber's queue.`);\r\n }\r\n\r\n this.active = true;\r\n logger.info(`${this.instanceName} - Subscriber became active.`); \r\n this.processQueue();\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"throttled-queue.js","sourceRoot":"","sources":["../../src/uns-mqtt/throttled-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE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AI,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;YACvB,wEAAwE;YACxE,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;YACjD,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;YACxB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,2DAA2D,CAAC,CAAC;QAC/F,CAAC;aAAM,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACjC,sEAAsE;YACtE,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,qFAAqF,CAAC,CAAC;QACzH,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,8BAA8B,CAAC,CAAC;QAChE,IAAI,CAAC,YAAY,EAAE,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { createHash } from \"crypto\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\nimport { IClientPublishOptions } from \"mqtt\";\nimport { join } from \"path\";\n\nimport { basePath } from \"../base-path.js\";\nimport logger from \"../logger.js\";\n\n/**\n * Abstract base class that encapsulates common functionality for managing\n * a throttled in‑memory queue with an optional disk persistence.\n */\nabstract class ThrottledQueue<T> {\n protected queue: T[] = [];\n protected lastProcessedItems: T[] = [];\n protected isProcessing = false;\n protected inFlight = 0;\n protected maxConcurrency: number;\n public delay: number;\n protected persistToDisk: boolean;\n protected persistenceFilePath: string;\n protected previousLoggedSize = 0;\n protected instanceName: string;\n protected active: boolean = true;\n private inactiveLogSent = false;\n\n constructor(delay: number, persistToDisk: boolean, instanceName: string, maxConcurrency: number = 1) {\n this.delay = delay;\n this.persistToDisk = persistToDisk;\n this.instanceName = instanceName;\n this.maxConcurrency = Math.max(1, maxConcurrency);\n\n if (this.persistToDisk) {\n this.loadQueueFromDisk();\n }\n }\n\n /**\n * Process the queue items one at a time with a delay between each.\n */\n protected async processQueue(): Promise<void> {\n if (this.isProcessing) return;\n\n if (!this.active) {\n if (!this.inactiveLogSent) {\n logger.info(`${this.instanceName} - Queue is not active. Exiting processQueue.`);\n this.inactiveLogSent = true;\n }\n this.isProcessing = false;\n return;\n }\n this.isProcessing = true;\n // Reset the flag when processing actually starts.\n this.inactiveLogSent = false;\n\n try {\n while (this.queue.length > 0 && this.inFlight < this.maxConcurrency) {\n // Check if the queue is active\n if (!this.active) {\n logger.info(`${this.instanceName} - Queue processing paused.`);\n return; // Exit the loop and stop processing\n }\n\n const item = this.queue.shift();\n if (!item) {\n continue;\n }\n\n this.inFlight += 1;\n void this.processItemWithBookkeeping(item);\n\n if (this.persistToDisk) {\n this.saveQueueToDisk();\n }\n\n this.logQueueSize();\n\n if (this.delay > 0 && this.queue.length > 0 && this.inFlight < this.maxConcurrency) {\n await new Promise((resolve) => setTimeout(resolve, this.delay));\n }\n }\n } finally {\n this.isProcessing = false;\n if (this.active && this.queue.length > 0 && this.inFlight < this.maxConcurrency) {\n void this.processQueue();\n }\n }\n }\n\n private async processItemWithBookkeeping(item: T): Promise<void> {\n try {\n await this.processItem(item);\n // Only store 10 last processed items\n if (this.lastProcessedItems.length >= 10) {\n this.lastProcessedItems.shift();\n }\n // Store the last processed item\n this.lastProcessedItems.push(item);\n } catch (error) {\n // Specific error handling can be done in the subclass if needed.\n logger.error(`${this.instanceName} - Error processing item: ${(error as Error).message}`);\n } finally {\n this.inFlight = Math.max(0, this.inFlight - 1);\n\n if (this.persistToDisk) {\n this.saveQueueToDisk();\n }\n\n this.logQueueSize();\n\n // Check if the queue is active\n if (this.active && this.queue.length > 0) {\n void this.processQueue();\n }\n }\n }\n\n /**\n * Log changes to the queue size at significant thresholds.\n */\n protected logQueueSize(): void {\n const className = `${this.constructor.name.includes(\"Publisher\") ? \"Publisher\" : this.constructor.name.includes(\"Subscriber\") ? \"Subscriber\" : \"Unknown\"}`;\n const pendingCount = this.getPendingCount();\n if (\n pendingCount > 1\n && pendingCount > this.previousLoggedSize\n && Math.floor(pendingCount / 100) > Math.floor(this.previousLoggedSize / 100)\n ) {\n logger.info(`${this.instanceName} - ${className} queue size length is ${pendingCount}.`);\n this.previousLoggedSize = pendingCount;\n } else if (pendingCount === 0 && this.previousLoggedSize > 0) {\n logger.info(`${this.instanceName} - ${className} queue is empty.`);\n this.previousLoggedSize = 0;\n }\n }\n\n protected getPendingCount(): number {\n return this.queue.length + this.inFlight;\n }\n\n /**\n * Save the current queue to disk.\n */\n protected saveQueueToDisk(): void {\n try {\n const queueData = this.queue.map((item) => this.serializeItem(item));\n const dir = join(this.persistenceFilePath, \"..\");\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n writeFileSync(this.persistenceFilePath, JSON.stringify(queueData, null, 2));\n logger.debug(`${this.instanceName} - Queue saved to disk.`);\n } catch (error: any) {\n logger.error(`${this.instanceName} - Error saving queue to disk: ${error.message}`);\n }\n }\n\n /**\n * Load the queue from disk.\n */\n protected loadQueueFromDisk(): void {\n try {\n if (existsSync(this.persistenceFilePath)) {\n const queueData = JSON.parse(readFileSync(this.persistenceFilePath, \"utf8\"));\n this.queue = queueData.map((data: any) => this.deserializeItem(data));\n logger.info(`${this.instanceName} - Queue loaded from disk.`);\n }\n } catch (error: any) {\n logger.error(`${this.instanceName} - Error loading queue from disk: ${error.message}`);\n }\n }\n\n /**\n * Process a single queue item.\n */\n protected abstract processItem(item: T): Promise<void>;\n\n /**\n * Serialize a queue item for persistence.\n */\n protected abstract serializeItem(item: T): any;\n\n /**\n * Deserialize persisted data into a queue item.\n */\n protected abstract deserializeItem(data: any): T;\n}\n\n/**\n * Interface representing a queued publish item.\n */\ninterface PublisherQueueItem {\n topic: string;\n message: string;\n id: string;\n options?: IClientPublishOptions;\n resolve: () => void;\n reject: (err: any) => void;\n}\n\n/**\n * ThrottledPublisher: manages a queue of outgoing publish requests,\n * sending one message at a time using a provided publish function.\n */\nexport class ThrottledPublisher extends ThrottledQueue<PublisherQueueItem> {\n private publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>;\n private maxPendingPublishes?: number;\n\n /**\n * @param delay Delay between messages in milliseconds.\n * @param publishFunction Function to perform the actual publish.\n * @param persistToDisk Whether to persist the queue to disk.\n * @param persistenceFilePath File path for queue persistence.\n * @param instanceName Unique instance name for logging.\n */\n constructor(\n delay: number,\n publishFunction: (topic: string, message: string, id: string, options?: IClientPublishOptions) => Promise<void>,\n persistToDisk: boolean = false,\n persistenceFilePath: string = join(basePath, \"/publisherQueue/\", \"throttled-publisher-queue.json\"),\n instanceName: string,\n active: boolean,\n maxConcurrency: number = 1,\n maxPendingPublishes?: number,\n ) {\n super(delay, persistToDisk, instanceName, maxConcurrency);\n this.persistenceFilePath = persistenceFilePath;\n this.publishFunction = publishFunction;\n this.active = active;\n this.maxPendingPublishes = maxPendingPublishes;\n if (active) {\n logger.info(`${this.instanceName} - Publisher is active.`);\n } else {\n logger.info(`${this.instanceName} - Publisher is passive.`);\n }\n }\n\n /**\n * Enqueue a publish request.\n */\n public enqueue(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> {\n try {\n this.assertCanAcceptPublish();\n return this.enqueueAcceptedPublish(topic, message, id, options);\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n /**\n * Enqueue a publish request and throw synchronously if the bounded queue is full.\n */\n public enqueueOrThrow(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> {\n this.assertCanAcceptPublish();\n return this.enqueueAcceptedPublish(topic, message, id, options);\n }\n\n private assertCanAcceptPublish(): void {\n if (this.maxPendingPublishes !== undefined && this.getPendingCount() >= this.maxPendingPublishes) {\n throw new Error(`${this.instanceName} - Publisher queue is full (${this.maxPendingPublishes}).`);\n }\n }\n\n private enqueueAcceptedPublish(topic: string, message: string, id: string, options?: IClientPublishOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n this.queue.push({ topic, message, id, options, resolve, reject });\n\n // Trigger logging after a new message is added.\n this.logQueueSize(); \n\n if (this.persistToDisk) {\n this.saveQueueToDisk();\n }\n\n if (this.active) {\n this.processQueue();\n }\n });\n }\n\n private computeHashForBatch(messages: any[]): string {\n const data = JSON.stringify(messages);\n return createHash(\"sha256\").update(data).digest(\"hex\");\n }\n\n /**\n * Switch the publisher to a passive state and return a snapshot\n */\n public becomeActive(snapshot: { referenceHash: string; batchSize: number }): void {\n // If snapshot is empty (batchSize is zero), start processing from the beginning.\n if (snapshot.batchSize === 0 || snapshot.batchSize === undefined || snapshot.referenceHash === undefined) {\n this.active = true;\n logger.info(`${this.instanceName} - Publisher became active.`);\n this.processQueue();\n return;\n }\n\n const { referenceHash, batchSize } = snapshot;\n let resumeIndex = -1;\n\n // Use batchSize as the window length.\n for (let i = 0; i <= this.queue.length - batchSize; i++) {\n const currentBatch = this.queue.slice(i, i + batchSize);\n const currentHash = this.computeHashForBatch(currentBatch);\n if (currentHash === referenceHash) {\n resumeIndex = i + batchSize; // resume immediately after the matched batch\n break;\n }\n }\n\n if (resumeIndex !== -1) {\n // Adjust the queue so that processing resumes after the matching batch.\n this.queue = this.queue.slice(resumeIndex);\n logger.info(`${this.instanceName} - Publisher resumed from the matched snapshot position.`);\n } else if (this.queue.length > 0) {\n // Log info message and process the entire queue if no match is found.\n logger.info(`${this.instanceName} - No matching batch found during resume; processing the entire publisher's queue.`);\n }\n\n this.active = true;\n logger.info(`${this.instanceName} - Publisher became active.`);\n this.processQueue();\n }\n\n /**\n * Switch the publisher to a passive state and return a snapshot\n */\n public async becomePassive(n: number): Promise<{ referenceHash: string; batchSize: number; }> {\n this.active = false;\n logger.info(`${this.instanceName} - Publisher became passive.`);\n const batch = this.queue.slice(-n);\n const batchSize = batch.length;\n const referenceHash = this.computeHashForBatch(batch);\n if (batchSize > 0) {\n logger.info(`${this.instanceName} - Publisher batch reference hash: ${referenceHash}, size: ${batchSize}`);\n }\n return { referenceHash, batchSize };\n }\n\n public getState(): boolean {\n return this.active;\n }\n\n /**\n * Process a single publish request.\n */\n protected async processItem(item: PublisherQueueItem): Promise<void> {\n try {\n await this.publishFunction(item.topic, item.message, item.id, item.options);\n item.resolve();\n } catch (error) {\n item.reject(error);\n }\n }\n\n /**\n * Serialize a publish request for persistence.\n */\n protected serializeItem(item: PublisherQueueItem): any {\n return {\n topic: item.topic,\n message: item.message,\n id: item.id,\n options: item.options,\n };\n }\n\n /**\n * Deserialize persisted data into a publish request.\n */\n protected deserializeItem(data: any): PublisherQueueItem {\n return {\n topic: data.topic,\n message: data.message,\n id: data.id,\n options: data.options,\n resolve: () => {},\n reject: (err: any) => logger.error(`${this.instanceName} - Error processing queued publish item: ${err.message}`),\n };\n }\n}\n\n/**\n * Interface representing a queued subscribe item.\n */\ninterface SubscriberQueueItem {\n topic: string;\n message: string;\n timestamp: number;\n}\n\n/**\n * ThrottledSubscriber: manages a queue of incoming messages and processes\n * them one at a time using a provided message handler.\n */\nexport class ThrottledSubscriber extends ThrottledQueue<SubscriberQueueItem> {\n private messageHandler: (topic: string, message: string) => Promise<void>;\n\n /**\n * @param delay Delay between processing messages in milliseconds.\n * @param messageHandler Function to process incoming messages.\n * @param persistToDisk Whether to persist the queue to disk.\n * @param persistenceFilePath File path for queue persistence.\n * @param instanceName Unique instance name for logging.\n */\n constructor(\n delay: number,\n messageHandler: (topic: string, message: string) => Promise<void>,\n persistToDisk: boolean = false,\n persistenceFilePath: string = join(basePath, \"/subscriberQueue/\", \"throttled-subscriber-queue.json\"),\n instanceName: string,\n active: boolean,\n ) {\n super(delay, persistToDisk, instanceName);\n this.persistenceFilePath = persistenceFilePath;\n this.messageHandler = messageHandler;\n this.active = active;\n if (active) {\n logger.info(`${this.instanceName} - Subscriber is active.`);\n } else {\n logger.info(`${this.instanceName} - Subscriber is passive.`);\n }\n }\n\n /**\n * Enqueue subscribe messages for processing.\n */\n public enqueue(topic: string, message: string): void {\n this.queue.push({ topic, message, timestamp: Date.now() });\n \n // Trigger logging after a new message is added.\n this.logQueueSize();\n \n if (this.persistToDisk) {\n this.saveQueueToDisk();\n }\n\n if (this.active) {\n this.processQueue();\n }\n }\n\n /**\n * Process a single subscribe message.\n */\n protected async processItem(item: SubscriberQueueItem): Promise<void> {\n await this.messageHandler(item.topic, item.message);\n }\n\n /**\n * Serialize a subscribe message for persistence.\n */\n protected serializeItem(item: SubscriberQueueItem): any {\n return {\n message: item.message,\n };\n }\n\n /**\n * Deserialize persisted data into a subscribe message.\n */\n protected deserializeItem(data: any): SubscriberQueueItem {\n return {\n topic: data.topic,\n message: data.message,\n timestamp: data.timestamp,\n };\n }\n\n /**\n * Get the current state of the subscriber (active or passive).\n */\n public getState(): boolean {\n return this.active;\n }\n\n /**\n * Computes a SHA‑256 hash for a given batch of subscriber items.\n */\n private computeHashForBatch(messages: SubscriberQueueItem[]): string {\n const data = JSON.stringify(messages.map((item) => this.serializeItem(item)));\n return createHash(\"sha256\").update(data).digest(\"hex\");\n }\n\n /**\n * Switch the subscriber to a passive state and return a snapshot.\n */\n public async becomePassive(n: number): Promise<{ referenceHash: string; batchSize: number; }> {\n this.active = false;\n logger.info(`${this.instanceName} - Subscriber is passive.`);\n const batch = this.lastProcessedItems.slice(-n);\n const batchSize = batch.length;\n const referenceHash = this.computeHashForBatch(batch);\n if (batchSize > 0) {\n logger.info(`${this.instanceName} - Subscriber batch reference hash: ${referenceHash}, size: ${batchSize}`);\n }\n return { referenceHash, batchSize };\n }\n\n /**\n * Switch the subscriber to an active state and resume processing.\n */\n public becomeActive(snapshot: { referenceHash: string; batchSize: number }): void {\n // If snapshot is empty (batchSize is zero), start processing from the beginning.\n if (snapshot.batchSize === 0 || snapshot.batchSize === undefined || snapshot.referenceHash === undefined) {\n this.active = true;\n logger.info(`${this.instanceName} - Subscriber became active.`); \n this.processQueue();\n return;\n }\n\n const { referenceHash, batchSize } = snapshot;\n let resumeIndex = -1;\n\n // Use batchSize as the window length.\n for (let i = 0; i <= this.queue.length - batchSize; i++) {\n const currentBatch = this.queue.slice(i, i + batchSize);\n const currentHash = this.computeHashForBatch(currentBatch);\n if (currentHash === referenceHash) {\n resumeIndex = i + batchSize; // resume immediately after the matched batch\n break;\n }\n }\n\n if (resumeIndex !== -1) {\n // Adjust the queue so that processing resumes after the matching batch.\n const queueSlice = this.queue.slice(resumeIndex); \n this.queue = queueSlice;\n logger.info(`${this.instanceName} - Subscriber resumed from the matched snapshot position.`);\n } else if (this.queue.length > 0) {\n // Log info message and process the entire queue if no match is found.\n logger.info(`${this.instanceName} - No matching batch found during resume; processing the entire subscriber's queue.`);\n }\n\n this.active = true;\n logger.info(`${this.instanceName} - Subscriber became active.`); \n this.processQueue();\n }\n}\n"]}
|
|
@@ -14,16 +14,27 @@ export declare enum MessageMode {
|
|
|
14
14
|
*/
|
|
15
15
|
Both = "both"
|
|
16
16
|
}
|
|
17
|
+
export interface UnsMqttProxyStopOptions {
|
|
18
|
+
drain?: boolean;
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
}
|
|
17
21
|
export default class UnsMqttProxy extends UnsProxy {
|
|
22
|
+
static readonly DEFAULT_DRAIN_TIMEOUT_MS = 30000;
|
|
18
23
|
private lastValues;
|
|
19
24
|
private worker;
|
|
20
25
|
private pendingEnqueues;
|
|
26
|
+
private pendingPublishCompletions;
|
|
21
27
|
private unsParameters;
|
|
22
28
|
protected processStatusTopic: string;
|
|
23
29
|
instanceName: string;
|
|
24
30
|
private currentSequenceId;
|
|
25
31
|
private topicBuilder;
|
|
26
32
|
private deltaModeDeprecationWarned;
|
|
33
|
+
private drainWaiters;
|
|
34
|
+
private workerFailure;
|
|
35
|
+
private lifecycleState;
|
|
36
|
+
private stopPromise;
|
|
37
|
+
private expectedWorkerExit;
|
|
27
38
|
constructor(mqttHost: string, processName: string, instanceName: string, unsParameters?: IUnsParameters, publisherActive?: boolean, subscriberActive?: boolean);
|
|
28
39
|
/**
|
|
29
40
|
* Resolve object identity from explicit fields or the tail of the topic path.
|
|
@@ -44,7 +55,7 @@ export default class UnsMqttProxy extends UnsProxy {
|
|
|
44
55
|
* @param topic - The topic to which the message belongs.
|
|
45
56
|
* @param message - The message to be enqueued.
|
|
46
57
|
* @param options - Optional publish options.
|
|
47
|
-
* @returns A promise that resolves when the message is
|
|
58
|
+
* @returns A promise that resolves when the message is accepted into the worker queue.
|
|
48
59
|
*/
|
|
49
60
|
private enqueueMessageToWorkerQueue;
|
|
50
61
|
/**
|
|
@@ -76,6 +87,8 @@ export default class UnsMqttProxy extends UnsProxy {
|
|
|
76
87
|
* until the queue is empty (all messages are processed).
|
|
77
88
|
*/
|
|
78
89
|
setSubscriberPassiveAndDrainQueue(): Promise<UnsEvents["mqttWorker"]>;
|
|
90
|
+
drainPublishes(timeoutMs?: number): Promise<void>;
|
|
91
|
+
flush(timeoutMs?: number): Promise<void>;
|
|
79
92
|
/**
|
|
80
93
|
* Processes and publishes MQTT messages based on the selected message mode.
|
|
81
94
|
*
|
|
@@ -90,7 +103,7 @@ export default class UnsMqttProxy extends UnsProxy {
|
|
|
90
103
|
*
|
|
91
104
|
* @param topic - The MQTT topic.
|
|
92
105
|
* @param message - The message to publish.
|
|
93
|
-
* @returns A promise that resolves when
|
|
106
|
+
* @returns A promise that resolves when the message is accepted into the worker queue.
|
|
94
107
|
*/
|
|
95
108
|
publishMessage(topic: string, message: string, options?: IClientPublishOptions): Promise<void>;
|
|
96
109
|
/**
|
|
@@ -124,6 +137,11 @@ export default class UnsMqttProxy extends UnsProxy {
|
|
|
124
137
|
/**
|
|
125
138
|
* Stops the UnsProxy instance and cleans up resources.
|
|
126
139
|
*/
|
|
127
|
-
stop(): Promise<void>;
|
|
140
|
+
stop(options?: UnsMqttProxyStopOptions): Promise<void>;
|
|
141
|
+
private performStop;
|
|
142
|
+
private failPendingPublishes;
|
|
143
|
+
private rejectOutstandingPublishes;
|
|
144
|
+
private notifyDrainWaiters;
|
|
145
|
+
private removeDrainWaiter;
|
|
128
146
|
}
|
|
129
147
|
//# sourceMappingURL=uns-mqtt-proxy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uns-mqtt-proxy.d.ts","sourceRoot":"","sources":["../../src/uns-mqtt/uns-mqtt-proxy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAM7C,OAAO,EAAE,mBAAmB,EAAsC,UAAU,EAAE,cAAc,EAAmG,SAAS,EAAa,MAAM,0BAA0B,CAAC;AAOtP,OAAO,QAAQ,MAAM,qBAAqB,CAAC;AAa3C,oBAAY,WAAW;IACrB,GAAG,QAAQ,CAAM,iCAAiC;IAClD;;;OAGG;IACH,KAAK,UAAU,CAAE,8BAA8B;IAC/C;;;OAGG;IACH,IAAI,SAAS;CACd;
|
|
1
|
+
{"version":3,"file":"uns-mqtt-proxy.d.ts","sourceRoot":"","sources":["../../src/uns-mqtt/uns-mqtt-proxy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAM7C,OAAO,EAAE,mBAAmB,EAAsC,UAAU,EAAE,cAAc,EAAmG,SAAS,EAAa,MAAM,0BAA0B,CAAC;AAOtP,OAAO,QAAQ,MAAM,qBAAqB,CAAC;AAa3C,oBAAY,WAAW;IACrB,GAAG,QAAQ,CAAM,iCAAiC;IAClD;;;OAGG;IACH,KAAK,UAAU,CAAE,8BAA8B;IAC/C;;;OAGG;IACH,IAAI,SAAS;CACd;AAED,MAAM,WAAW,uBAAuB;IACtC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA8BD,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,QAAQ;IAChD,gBAAuB,wBAAwB,SAAU;IACzD,OAAO,CAAC,UAAU,CAA8E;IAChG,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,eAAe,CAAmF;IAC1G,OAAO,CAAC,yBAAyB,CAAqB;IACtD,OAAO,CAAC,aAAa,CAAiB;IACtC,SAAS,CAAC,kBAAkB,EAAE,M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