@hahnpro/flow-sdk 4.20.11-0 → 4.20.13

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.
@@ -1,9 +1,9 @@
1
1
  import 'reflect-metadata';
2
- import { CloudEvent } from 'cloudevents';
2
+ import type { CloudEvent } from 'cloudevents';
3
3
  import { PartialObserver } from 'rxjs';
4
4
  import { API } from '@hahnpro/hpc-api';
5
5
  import { AmqpConnection, Nack } from './amqp';
6
- import { ClassType, Flow, FlowElementContext } from './flow.interface';
6
+ import type { ClassType, Flow, FlowElementContext } from './flow.interface';
7
7
  import type { FlowEvent } from './FlowEvent';
8
8
  import { Logger } from './FlowLogger';
9
9
  import { RpcClient } from './RpcClient';
@@ -21,7 +21,6 @@ export declare class FlowApplication {
21
21
  private _rpcClient;
22
22
  constructor(modules: ClassType<any>[], flow: Flow, logger?: Logger, amqpConnection?: AmqpConnection, skipApi?: boolean);
23
23
  private init;
24
- private publishLifecycleEvent;
25
24
  private setQueueMetrics;
26
25
  private updateMetrics;
27
26
  subscribe: (streamId: string, observer: PartialObserver<FlowEvent>) => import("rxjs").Subscription;
@@ -33,7 +32,7 @@ export declare class FlowApplication {
33
32
  * Publish a flow event to the amqp flowlogs exchange.
34
33
  * If the event size exceeds the limit it will be truncated
35
34
  */
36
- publishEvent: (event: FlowEvent) => Promise<void>;
35
+ publishEvent: (event: FlowEvent) => void;
37
36
  rpcClient(): Promise<RpcClient>;
38
37
  /**
39
38
  * Calls onDestroy lifecycle method on all flow elements,
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlowApplication = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  require("reflect-metadata");
6
- const cloudevents_1 = require("cloudevents");
7
6
  const object_sizeof_1 = tslib_1.__importDefault(require("object-sizeof"));
8
7
  const perf_hooks_1 = require("perf_hooks");
9
8
  const rxjs_1 = require("rxjs");
@@ -12,7 +11,6 @@ const uuid_1 = require("uuid");
12
11
  const hpc_api_1 = require("@hahnpro/hpc-api");
13
12
  const amqp_1 = require("./amqp");
14
13
  const utils_1 = require("./utils");
15
- const flow_interface_1 = require("./flow.interface");
16
14
  const FlowLogger_1 = require("./FlowLogger");
17
15
  const RpcClient_1 = require("./RpcClient");
18
16
  const MAX_EVENT_SIZE_BYTES = +process.env.MAX_EVENT_SIZE_BYTES || 512 * 1024;
@@ -26,24 +24,6 @@ class FlowApplication {
26
24
  this.outputStreamMap = new Map();
27
25
  this.outputQueueMetrics = new Map();
28
26
  this.performanceMap = new Map();
29
- this.publishLifecycleEvent = (flowEvent, eventType, date = new Date()) => {
30
- if (!this.amqpConnection) {
31
- return;
32
- }
33
- try {
34
- const event = new cloudevents_1.CloudEvent({
35
- source: flowEvent.getSource(),
36
- type: eventType,
37
- data: Object.assign(Object.assign({}, flowEvent.getMetadata()), { flowEventId: flowEvent.getId() }),
38
- time: date.toISOString(),
39
- });
40
- const message = event.toJSON();
41
- return this.amqpConnection.publish('flow', 'lifecycle', message);
42
- }
43
- catch (err) {
44
- this.logger.error(err);
45
- }
46
- };
47
27
  this.setQueueMetrics = (id) => {
48
28
  const metrics = this.outputQueueMetrics.get(id) || { size: 0, lastAdd: 0, lastRemove: Date.now(), warnings: 0 };
49
29
  const secsProcessing = Math.round((metrics.lastAdd - metrics.lastRemove) / 1000);
@@ -129,7 +109,12 @@ class FlowApplication {
129
109
  contentType: 'application/json',
130
110
  data: { deploymentId: this.context.deploymentId, status: 'updated' },
131
111
  };
132
- (_f = this.amqpConnection) === null || _f === void 0 ? void 0 : _f.publish('deployment', 'health', statusEvent).catch((err) => this.logger.error(err));
112
+ try {
113
+ (_f = this.amqpConnection) === null || _f === void 0 ? void 0 : _f.publish('deployment', 'health', statusEvent);
114
+ }
115
+ catch (err) {
116
+ this.logger.error(err);
117
+ }
133
118
  }
134
119
  catch (err) {
135
120
  this.logger.error(err);
@@ -140,7 +125,12 @@ class FlowApplication {
140
125
  contentType: 'application/json',
141
126
  data: { deploymentId: this.context.deploymentId, status: 'updating failed' },
142
127
  };
143
- (_g = this.amqpConnection) === null || _g === void 0 ? void 0 : _g.publish('deployment', 'health', statusEvent).catch((err) => this.logger.error(err));
128
+ try {
129
+ (_g = this.amqpConnection) === null || _g === void 0 ? void 0 : _g.publish('deployment', 'health', statusEvent);
130
+ }
131
+ catch (e) {
132
+ this.logger.error(e);
133
+ }
144
134
  }
145
135
  }
146
136
  else if (event.type === 'com.flowstudio.deployment.message') {
@@ -213,7 +203,6 @@ class FlowApplication {
213
203
  try {
214
204
  await this.amqpConnection.managedChannel.assertExchange('deployment', 'direct', { durable: true });
215
205
  await this.amqpConnection.managedChannel.assertExchange('flowlogs', 'fanout', { durable: true });
216
- await this.amqpConnection.managedChannel.assertExchange('flow', 'direct', { durable: true });
217
206
  }
218
207
  catch (e) {
219
208
  logErrorAndExit(`Could not assert exchanges: ${e}`);
@@ -224,7 +213,7 @@ class FlowApplication {
224
213
  exchange: 'deployment',
225
214
  routingKey: this.context.deploymentId,
226
215
  queueOptions: { durable: false, exclusive: true },
227
- });
216
+ }, 'FlowApplication.onMessage');
228
217
  }
229
218
  catch (err) {
230
219
  logErrorAndExit(`Could not subscribe to deployment exchange: ${err}`);
@@ -281,13 +270,10 @@ class FlowApplication {
281
270
  outputStream
282
271
  .pipe((0, operators_1.tap)(() => this.setQueueMetrics(targetStreamId)), (0, operators_1.mergeMap)(async (event) => {
283
272
  this.performanceMap.set(event.getId(), perf_hooks_1.performance.eventLoopUtilization());
284
- this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.ACTIVATED);
285
273
  try {
286
274
  await element[streamHandler](event);
287
- this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.COMPLETED);
288
275
  }
289
276
  catch (err) {
290
- this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.TERMINATED);
291
277
  try {
292
278
  element.handleApiError(err);
293
279
  }
@@ -302,8 +288,8 @@ class FlowApplication {
302
288
  if (elu) {
303
289
  this.performanceMap.delete(event.getId());
304
290
  elu = perf_hooks_1.performance.eventLoopUtilization(elu);
305
- if (elu.utilization > 0.75 && elu.active > 2000) {
306
- this.logger.warn(`High event loop utilization detected for ${targetStreamId} with event ${event.getId()}! Handler was active for ${Number(elu.active).toFixed(2)}ms with a utilization of ${Number(elu.utilization * 100).toFixed(2)}%. Consider refactoring or move tasks to a worker thread.`);
291
+ if (elu.utilization > 0.7 && elu.active > 1000) {
292
+ this.logger.warn(`High event loop utilization detected for ${targetStreamId} with event ${event.getId()}! Handler has been active for ${Number(elu.active).toFixed(2)}ms with a utilization of ${Number(elu.utilization * 100).toFixed(2)}%. Consider refactoring or move tasks to a worker thread.`);
307
293
  }
308
294
  }
309
295
  }))
@@ -126,7 +126,7 @@ function InputStream(id = 'default', options) {
126
126
  if (!this.stopPropagateStream.has(id)) {
127
127
  this.stopPropagateStream.set(id, (_a = options === null || options === void 0 ? void 0 : options.stopPropagation) !== null && _a !== void 0 ? _a : false);
128
128
  }
129
- return method.call(this, new FlowEvent_1.FlowEvent(Object.assign(Object.assign({ id: event.getMetadata().elementId }, event.getMetadata()), { inputStreamId: id }), event.getData(), event.getType(), new Date(event.getTime())));
129
+ return method.call(this, new FlowEvent_1.FlowEvent(Object.assign(Object.assign({}, event.getMetadata()), { inputStreamId: id }), event.getData(), event.getType(), new Date(event.getTime())));
130
130
  };
131
131
  };
132
132
  }
@@ -8,13 +8,7 @@ export declare class FlowEvent {
8
8
  getDataContentType: () => string;
9
9
  getDataschema: () => string;
10
10
  getId: () => string;
11
- getMetadata: () => {
12
- deploymentId: string;
13
- elementId: string;
14
- flowId: string;
15
- functionFqn: string;
16
- inputStreamId: string;
17
- };
11
+ getMetadata: () => any;
18
12
  getSource: () => string;
19
13
  getStreamId: () => string;
20
14
  getSubject: () => string;
@@ -16,7 +16,7 @@ export declare class FlowLogger implements Logger {
16
16
  private readonly logger;
17
17
  private readonly publishEvent?;
18
18
  private static getStackTrace;
19
- constructor(metadata: FlowElementContext, logger?: Logger, publishEvent?: (event: FlowEvent) => Promise<void>);
19
+ constructor(metadata: FlowElementContext, logger?: Logger, publishEvent?: (event: FlowEvent) => void);
20
20
  debug: (message: any, options?: LoggerOptions) => void;
21
21
  error: (message: any, options?: LoggerOptions) => void;
22
22
  log: (message: any, options?: LoggerOptions) => void;
@@ -35,10 +35,9 @@ class FlowLogger {
35
35
  return stack.splice(1).join('\n');
36
36
  }
37
37
  publish(message, level, options) {
38
- var _a;
39
38
  if (this.publishEvent) {
40
39
  const event = new FlowEvent_1.FlowEvent(this.metadata, message, `flow.log.${level}`);
41
- (_a = this.publishEvent(event)) === null || _a === void 0 ? void 0 : _a.catch((err) => this.logger.error(err, this.metadata));
40
+ this.publishEvent(event);
42
41
  }
43
42
  switch (level) {
44
43
  case 'debug':
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlowModule = void 0;
4
4
  function FlowModule(metadata) {
5
- const fqnRegExp = new RegExp('^([a-zA-Z][a-zA-Z0-9]*[.-])*[a-zA-Z][a-zA-Z0-9]*$');
6
- if (!fqnRegExp.test(metadata.name)) {
7
- throw new Error(`Flow Module name (${metadata.name}) is not valid`);
5
+ const validateNameRegExp = new RegExp(/^(@[a-z][a-z0-9-]*\/)?[a-z][a-z0-9-]*$/);
6
+ if (!validateNameRegExp.test(metadata.name)) {
7
+ throw new Error(`Flow Module name (${metadata.name}) is not valid. Name must be all lowercase and not contain any special characters except for hyphens. Can optionally start with a scope "@scopename/"`);
8
8
  }
9
9
  return (target) => {
10
10
  Reflect.defineMetadata('module:name', metadata.name, target);
package/dist/amqp.d.ts CHANGED
@@ -5,8 +5,8 @@ export interface AmqpConnection {
5
5
  connection: Connection;
6
6
  managedChannel: ChannelWrapper;
7
7
  managedConnection: AmqpConnectionManager;
8
- createSubscriber<T>(handler: (msg: T | undefined, rawMessage?: ConsumeMessage) => Promise<any | undefined>, msgOptions: MessageHandlerOptions): Promise<void>;
9
- publish(exchange: string, routingKey: string, message: any, options?: Options.Publish): Promise<void>;
8
+ createSubscriber<T>(handler: (msg: T | undefined, rawMessage?: ConsumeMessage) => Promise<any | undefined | void>, msgOptions: MessageHandlerOptions, originalHandlerName: string): Promise<void>;
9
+ publish(exchange: string, routingKey: string, message: any, options?: Options.Publish): void;
10
10
  }
11
11
  export declare class Nack {
12
12
  private readonly _requeue;
@@ -38,9 +38,4 @@ export interface StreamOptions {
38
38
  concurrent?: number;
39
39
  }
40
40
  export declare type ClassType<T> = new (...args: any[]) => T;
41
- export declare enum LifecycleEvent {
42
- ACTIVATED = "com.hahnpro.flow_function.activated",
43
- COMPLETED = "com.hahnpro.flow_function.completed",
44
- TERMINATED = "com.hahnpro.flow_function.terminated"
45
- }
46
41
  export {};
@@ -1,9 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LifecycleEvent = void 0;
4
- var LifecycleEvent;
5
- (function (LifecycleEvent) {
6
- LifecycleEvent["ACTIVATED"] = "com.hahnpro.flow_function.activated";
7
- LifecycleEvent["COMPLETED"] = "com.hahnpro.flow_function.completed";
8
- LifecycleEvent["TERMINATED"] = "com.hahnpro.flow_function.terminated";
9
- })(LifecycleEvent = exports.LifecycleEvent || (exports.LifecycleEvent = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hahnpro/flow-sdk",
3
- "version": "4.20.11-0",
3
+ "version": "4.20.13",
4
4
  "description": "SDK for building Flow Modules",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -24,9 +24,9 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@hahnpro/hpc-api": "2.3.1",
27
+ "@hahnpro/hpc-api": "3.0.0",
28
28
  "amqp-connection-manager": "^3.9.0",
29
- "amqplib": "^0.8.0",
29
+ "amqplib": "^0.9.1",
30
30
  "class-transformer": "0.5.1",
31
31
  "class-validator": "~0.13.2",
32
32
  "cloudevents": "^6.0.1",
@@ -39,16 +39,16 @@
39
39
  "uuid": "^8.3.2"
40
40
  },
41
41
  "devDependencies": {
42
- "@golevelup/nestjs-rabbitmq": "^2.3.0",
43
- "@nestjs/common": "^8.4.4",
42
+ "@golevelup/nestjs-rabbitmq": "^2.4.0",
43
+ "@nestjs/common": "^8.4.6",
44
44
  "@types/amqp-connection-manager": "^2.0.12",
45
45
  "@types/amqplib": "^0.8.2",
46
- "@types/jest": "^27.5.0",
46
+ "@types/jest": "^27.5.1",
47
47
  "@types/lodash": "^4.14.182",
48
- "@types/node": "^16.11.33",
49
- "class-validator-jsonschema": "^3.1.0",
50
- "jest": "^27.5.1",
51
- "typescript": "^4.6.4"
48
+ "@types/node": "^16.11.38",
49
+ "class-validator-jsonschema": "^3.1.1",
50
+ "jest": "^28.1.0",
51
+ "typescript": "^4.7.2"
52
52
  },
53
53
  "engines": {
54
54
  "node": "^14.13.1 || >=16.0.0"