@hahnpro/flow-sdk 9.2.0 → 9.3.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.
@@ -4,6 +4,7 @@ exports.FlowApplication = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  require("reflect-metadata");
6
6
  const hpc_api_1 = require("@hahnpro/hpc-api");
7
+ const jetstream_1 = require("@nats-io/jetstream");
7
8
  const cloudevents_1 = require("cloudevents");
8
9
  const lodash_1 = require("lodash");
9
10
  const object_sizeof_1 = tslib_1.__importDefault(require("object-sizeof"));
@@ -315,6 +316,10 @@ class FlowApplication {
315
316
  filter_subject: `${nats_1.natsFlowsPrefixFlowDeployment}.${this.context.deploymentId}.*`,
316
317
  };
317
318
  const consumer = await (0, nats_1.getOrCreateConsumer)(this.logger, this._natsConnection, nats_1.FLOWS_STREAM_NAME, consumerOptions.name, consumerOptions);
319
+ (0, nats_1.natsEventListener)(this._natsConnection, this.logger, async () => {
320
+ this.logger.debug('ConsumerService: Reconnected to Nats and re-creating non-durable consumers');
321
+ await (0, nats_1.getOrCreateConsumer)(this.logger, this._natsConnection, nats_1.FLOWS_STREAM_NAME, consumerOptions.name, consumerOptions);
322
+ });
318
323
  this.consumeNatsMessagesOfConsumer(consumer, consumerOptions);
319
324
  }
320
325
  catch (e) {
@@ -438,8 +443,18 @@ class FlowApplication {
438
443
  if (this.amqpConnection) {
439
444
  await this.amqpConnection.close();
440
445
  }
441
- if (this.natsConnection && !this._natsConnection.isClosed()) {
442
- await this._natsConnection?.drain();
446
+ if (this._natsConnection && !this._natsConnection.isClosed()) {
447
+ await (0, jetstream_1.jetstreamManager)(this._natsConnection).then((jsm) => {
448
+ jsm.consumers
449
+ .delete(nats_1.FLOWS_STREAM_NAME, `flow-deployment-${this.context.deploymentId}`)
450
+ .then(() => {
451
+ this.logger.error(`Deleted consumer for flow deployment ${this.context.deploymentId}`);
452
+ })
453
+ .catch((err) => {
454
+ this.logger.error(`Could not delete consumer for flow deployment ${this.context.deploymentId}: ${err.message}`);
455
+ });
456
+ });
457
+ await this._natsConnection.drain();
443
458
  }
444
459
  await this.natsMessageIterator?.close();
445
460
  await this._natsConnection?.close();
@@ -36,7 +36,7 @@ class FlowLogger {
36
36
  }
37
37
  publish(message, level, options) {
38
38
  if (this.publishEvent) {
39
- const event = new FlowEvent_1.FlowEvent(this.metadata, message, `flow.log.${level}`);
39
+ const event = new FlowEvent_1.FlowEvent(this.metadata, { ...message, message: message.message ?? message.toString() }, `flow.log.${level}`);
40
40
  this.publishEvent(event);
41
41
  }
42
42
  switch (level) {
package/dist/nats.d.ts CHANGED
@@ -7,5 +7,6 @@ export declare const natsFlowsPrefixFlowDeployment = "fs.flowdeployment";
7
7
  export declare const defaultConsumerConfig: ConsumerConfig;
8
8
  export declare const FLOWS_STREAM_NAME = "flows";
9
9
  export declare function getOrCreateConsumer(logger: Logger, natsConnection: NatsConnection, streamName: string, consumerName: string, options: Partial<ConsumerConfig>): Promise<Consumer>;
10
+ export declare function natsEventListener(nc: NatsConnection, logger: Logger, reconnectHandler: () => void): Promise<void>;
10
11
  export declare function publishNatsEvent<T>(logger: Logger, nc: NatsConnection, event: NatsEvent<T>, subject?: string): Promise<PubAck>;
11
12
  export declare function createNatsConnection(config: ConnectionOptions): Promise<NatsConnection>;
package/dist/nats.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FLOWS_STREAM_NAME = exports.defaultConsumerConfig = exports.natsFlowsPrefixFlowDeployment = void 0;
4
4
  exports.getOrCreateConsumer = getOrCreateConsumer;
5
+ exports.natsEventListener = natsEventListener;
5
6
  exports.publishNatsEvent = publishNatsEvent;
6
7
  exports.createNatsConnection = createNatsConnection;
7
8
  const transport_node_1 = require("@nats-io/transport-node");
@@ -51,6 +52,19 @@ async function getOrCreateConsumer(logger, natsConnection, streamName, consumerN
51
52
  }
52
53
  return await (0, jetstream_1.jetstream)(natsConnection).consumers.get(streamName, consumerName);
53
54
  }
55
+ async function natsEventListener(nc, logger, reconnectHandler) {
56
+ const statusAsyncIterator = nc?.status();
57
+ if (!statusAsyncIterator) {
58
+ logger.error('NATS Status-AsyncIterator is not available, cannot listen for events to re-create consumers at reconnects');
59
+ return;
60
+ }
61
+ for await (const status of statusAsyncIterator) {
62
+ logger.debug(`[NatsConsumerService] ${status.type}`);
63
+ if (status.type === 'reconnect') {
64
+ reconnectHandler();
65
+ }
66
+ }
67
+ }
54
68
  async function publishNatsEvent(logger, nc, event, subject) {
55
69
  if (!nc || nc.isClosed()) {
56
70
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hahnpro/flow-sdk",
3
- "version": "9.2.0",
3
+ "version": "9.3.0",
4
4
  "description": "SDK for building Flow Modules",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -25,9 +25,9 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@hahnpro/hpc-api": "2025.2.15",
28
- "@nats-io/jetstream": "3.0.2",
29
- "@nats-io/nats-core": "3.0.2",
30
- "@nats-io/transport-node": "3.0.2",
28
+ "@nats-io/jetstream": "3.1.0",
29
+ "@nats-io/nats-core": "3.1.0",
30
+ "@nats-io/transport-node": "3.1.0",
31
31
  "amqp-connection-manager": "4.1.14",
32
32
  "amqplib": "0.10.8",
33
33
  "class-transformer": "0.5.1",
@@ -44,9 +44,9 @@
44
44
  "@types/amqplib": "0.10.7",
45
45
  "@types/jest": "30.0.0",
46
46
  "@types/lodash": "4.17.20",
47
- "@types/node": "22.16.0",
47
+ "@types/node": "22.16.4",
48
48
  "class-validator-jsonschema": "5.0.2",
49
- "jest": "30.0.3",
49
+ "jest": "30.0.4",
50
50
  "typescript": "5.8.3"
51
51
  },
52
52
  "peerDependencies": {