@hahnpro/flow-sdk 4.20.10 → 4.20.11-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.
@@ -1,9 +1,9 @@
1
1
  import 'reflect-metadata';
2
- import type { CloudEvent } from 'cloudevents';
2
+ import { 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 type { ClassType, Flow, FlowElementContext } from './flow.interface';
6
+ import { 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,6 +21,7 @@ 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;
24
25
  private setQueueMetrics;
25
26
  private updateMetrics;
26
27
  subscribe: (streamId: string, observer: PartialObserver<FlowEvent>) => import("rxjs").Subscription;
@@ -3,6 +3,7 @@ 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");
6
7
  const object_sizeof_1 = tslib_1.__importDefault(require("object-sizeof"));
7
8
  const perf_hooks_1 = require("perf_hooks");
8
9
  const rxjs_1 = require("rxjs");
@@ -11,6 +12,7 @@ const uuid_1 = require("uuid");
11
12
  const hpc_api_1 = require("@hahnpro/hpc-api");
12
13
  const amqp_1 = require("./amqp");
13
14
  const utils_1 = require("./utils");
15
+ const flow_interface_1 = require("./flow.interface");
14
16
  const FlowLogger_1 = require("./FlowLogger");
15
17
  const RpcClient_1 = require("./RpcClient");
16
18
  const MAX_EVENT_SIZE_BYTES = +process.env.MAX_EVENT_SIZE_BYTES || 512 * 1024;
@@ -24,6 +26,24 @@ class FlowApplication {
24
26
  this.outputStreamMap = new Map();
25
27
  this.outputQueueMetrics = new Map();
26
28
  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
+ };
27
47
  this.setQueueMetrics = (id) => {
28
48
  const metrics = this.outputQueueMetrics.get(id) || { size: 0, lastAdd: 0, lastRemove: Date.now(), warnings: 0 };
29
49
  const secsProcessing = Math.round((metrics.lastAdd - metrics.lastRemove) / 1000);
@@ -193,6 +213,7 @@ class FlowApplication {
193
213
  try {
194
214
  await this.amqpConnection.managedChannel.assertExchange('deployment', 'direct', { durable: true });
195
215
  await this.amqpConnection.managedChannel.assertExchange('flowlogs', 'fanout', { durable: true });
216
+ await this.amqpConnection.managedChannel.assertExchange('flow', 'direct', { durable: true });
196
217
  }
197
218
  catch (e) {
198
219
  logErrorAndExit(`Could not assert exchanges: ${e}`);
@@ -260,10 +281,13 @@ class FlowApplication {
260
281
  outputStream
261
282
  .pipe((0, operators_1.tap)(() => this.setQueueMetrics(targetStreamId)), (0, operators_1.mergeMap)(async (event) => {
262
283
  this.performanceMap.set(event.getId(), perf_hooks_1.performance.eventLoopUtilization());
284
+ this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.ACTIVATED);
263
285
  try {
264
286
  await element[streamHandler](event);
287
+ this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.COMPLETED);
265
288
  }
266
289
  catch (err) {
290
+ this.publishLifecycleEvent(event, flow_interface_1.LifecycleEvent.TERMINATED);
267
291
  try {
268
292
  element.handleApiError(err);
269
293
  }
@@ -278,8 +302,8 @@ class FlowApplication {
278
302
  if (elu) {
279
303
  this.performanceMap.delete(event.getId());
280
304
  elu = perf_hooks_1.performance.eventLoopUtilization(elu);
281
- if (elu.utilization > 0.7 && elu.active > 1000) {
282
- 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.`);
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.`);
283
307
  }
284
308
  }
285
309
  }))
@@ -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({}, 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({ id: event.getMetadata().elementId }, event.getMetadata()), { inputStreamId: id }), event.getData(), event.getType(), new Date(event.getTime())));
130
130
  };
131
131
  };
132
132
  }
@@ -8,7 +8,13 @@ export declare class FlowEvent {
8
8
  getDataContentType: () => string;
9
9
  getDataschema: () => string;
10
10
  getId: () => string;
11
- getMetadata: () => any;
11
+ getMetadata: () => {
12
+ deploymentId: string;
13
+ elementId: string;
14
+ flowId: string;
15
+ functionFqn: string;
16
+ inputStreamId: string;
17
+ };
12
18
  getSource: () => string;
13
19
  getStreamId: () => string;
14
20
  getSubject: () => string;
@@ -38,4 +38,9 @@ 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
+ }
41
46
  export {};
@@ -1,2 +1,9 @@
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.10",
3
+ "version": "4.20.11-0",
4
4
  "description": "SDK for building Flow Modules",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -43,12 +43,12 @@
43
43
  "@nestjs/common": "^8.4.4",
44
44
  "@types/amqp-connection-manager": "^2.0.12",
45
45
  "@types/amqplib": "^0.8.2",
46
- "@types/jest": "^27.4.1",
46
+ "@types/jest": "^27.5.0",
47
47
  "@types/lodash": "^4.14.182",
48
- "@types/node": "^16.11.31",
48
+ "@types/node": "^16.11.33",
49
49
  "class-validator-jsonschema": "^3.1.0",
50
50
  "jest": "^27.5.1",
51
- "typescript": "^4.6.3"
51
+ "typescript": "^4.6.4"
52
52
  },
53
53
  "engines": {
54
54
  "node": "^14.13.1 || >=16.0.0"
@@ -57,6 +57,5 @@
57
57
  "build": "../../node_modules/.bin/tsc -p tsconfig.lib.json",
58
58
  "build:nocomments": "../../node_modules/.bin/tsc -p tsconfig.nocomments.json",
59
59
  "postbuild": "../../node_modules/.bin/copyfiles -u 1 lib/*.py dist"
60
- },
61
- "readme": "# `@hahnpro/flow-sdk`\n\nhttps://github.com/hahnprojects/flow\n"
60
+ }
62
61
  }