@nsshunt/stsappframework 3.0.95 → 3.0.96

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.
Files changed (42) hide show
  1. package/package.json +1 -1
  2. package/src_working/authDefs.ts +37 -0
  3. package/src_working/authutilsnode.ts +373 -0
  4. package/src_working/commonTypes.ts +239 -0
  5. package/src_working/index.ts +22 -0
  6. package/src_working/influxdb/influxDBManager.ts +970 -0
  7. package/src_working/influxdb/influxDBManagerAgent.ts +314 -0
  8. package/src_working/influxdb/influxDBManagerBase.ts +109 -0
  9. package/src_working/influxdb/influxDBManagerService.ts +373 -0
  10. package/src_working/instrumentationsubscriber.ts +283 -0
  11. package/src_working/kafka/IMKafkaManager.ts +152 -0
  12. package/src_working/kafka/kafkaconsumer.ts +82 -0
  13. package/src_working/kafka/kafkamanager.ts +186 -0
  14. package/src_working/kafka/kafkaproducer.ts +58 -0
  15. package/src_working/kafkatesting/config.ts +10 -0
  16. package/src_working/kafkatesting/consume.ts +116 -0
  17. package/src_working/kafkatesting/produce.ts +153 -0
  18. package/src_working/masterprocessbase.ts +598 -0
  19. package/src_working/middleware/serverNetworkMiddleware.ts +240 -0
  20. package/src_working/network.ts +36 -0
  21. package/src_working/processbase.ts +411 -0
  22. package/src_working/processoptions.ts +164 -0
  23. package/src_working/publishertransports/publishTransportDirect.ts +45 -0
  24. package/src_working/publishertransports/publishTransportUtils.ts +53 -0
  25. package/src_working/server.ts +141 -0
  26. package/src_working/serverprocessbase.ts +393 -0
  27. package/src_working/singleprocessbase.ts +121 -0
  28. package/src_working/socketIoServerHelper.ts +177 -0
  29. package/src_working/stscontrollerbase.ts +15 -0
  30. package/src_working/stslatencycontroller.ts +27 -0
  31. package/src_working/stslatencyroute.ts +16 -0
  32. package/src_working/stsrouterbase.ts +22 -0
  33. package/src_working/tcpclient/app.ts +19 -0
  34. package/src_working/tcpclient/app2.ts +56 -0
  35. package/src_working/tcpserver/app.ts +11 -0
  36. package/src_working/tcpserver/appConfig.ts +65 -0
  37. package/src_working/tcpserver/appmaster.ts +544 -0
  38. package/src_working/validation/errors.ts +6 -0
  39. package/src_working/webworkertesting/app.ts +49 -0
  40. package/src_working/webworkertesting/worker.ts +24 -0
  41. package/src_working/workerprocessbase.test.ts +47 -0
  42. package/src_working/workerprocessbase.ts +185 -0
@@ -0,0 +1,185 @@
1
+ /* eslint @typescript-eslint/no-explicit-any: 0 */ // --> OFF
2
+ import debugModule from 'debug'
3
+ const debug = debugModule(`proc:${process.pid}`);
4
+
5
+ import { Gauge, InstrumentGaugeTelemetry } from '@nsshunt/stsinstrumentation'
6
+ import { JSONObject } from '@nsshunt/stsutils'
7
+ import { ProcessOptions } from './processoptions'
8
+ import { IPCMessage, IPCMessages, IPCMessagePayload, IPCMessageCommand, IWorkerProcessBase } from './commonTypes'
9
+ import { STSExpressServer } from './server';
10
+ import { ServerProcessBase } from './serverprocessbase'
11
+
12
+ import { v4 as uuidv4 } from 'uuid';
13
+
14
+ import colors from 'colors'
15
+
16
+ /**
17
+ * todo
18
+ * @typedef {Object} options - todo
19
+ * @property {boolean} [wssServer=false] - Create a web socket server on this worker instance
20
+ */
21
+ export class WorkerProcessBase extends ServerProcessBase implements IWorkerProcessBase
22
+ {
23
+ #inFlightMessage: IPCMessages = { }
24
+ #requestResponseMessageTimeout = 2000; //@@ config
25
+
26
+ constructor(options: ProcessOptions) {
27
+ super(options);
28
+ }
29
+
30
+ WorkerStarted() {
31
+ return null;
32
+ }
33
+
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
35
+ ReceivedMessageFromMaster(msg: any) {
36
+ // Override in subclass if required
37
+ }
38
+
39
+ override CollectAdditionalTelemetry(): void {
40
+ this.httpServer.getConnections((error: any, count: any) => {
41
+ //@@this.instruments[Gauge.CONNECTION_COUNT_GAUGE].val = count;
42
+ this.UpdateInstrument(Gauge.CONNECTION_COUNT_GAUGE, {
43
+ val: count
44
+ } as InstrumentGaugeTelemetry);
45
+ });
46
+ }
47
+
48
+ #SendMessageToParentProcess = (message: IPCMessagePayload): Promise<JSONObject> => {
49
+ return new Promise((resolve, reject) => {
50
+ if (this.#inFlightMessage[message.id]) {
51
+ reject(`Message with id: [${message.id}] already exists within the Request/Response record structure`);
52
+ } else {
53
+ this.#inFlightMessage[message.id] = {
54
+ iPCMessagePayload: { ...message },
55
+ cb: () => {
56
+ const detail: JSONObject = this.#inFlightMessage[message.id].iPCMessagePayload.responseDetail as JSONObject
57
+ clearTimeout(this.#inFlightMessage[message.id].timeout);
58
+ setTimeout(() => {
59
+ delete this.#inFlightMessage[message.id];
60
+ }, 0).unref();
61
+ debug(`Resolving response message with id: [${message.id}] from parent process via IPC. Details: [${JSON.stringify(this.#inFlightMessage[message.id].iPCMessagePayload)}]`.green);
62
+ resolve(detail);
63
+ },
64
+ timeout: setTimeout(() => {
65
+ setTimeout(() => {
66
+ delete this.#inFlightMessage[message.id];
67
+ }, 0).unref();
68
+ debug(`Timeout has occurred after: [${this.#requestResponseMessageTimeout}]ms with message id: [${message.id}]. Details: [${JSON.stringify(this.#inFlightMessage[message.id].iPCMessagePayload)}]`.red);
69
+ reject('Did not receive response form parent process.');
70
+ }, this.#requestResponseMessageTimeout) // max message timeout allowed
71
+ }
72
+ debug(`Sending message with id: [${message.id}] to parent process via IPC. Details: [${JSON.stringify(this.#inFlightMessage[message.id].iPCMessagePayload)}]`.yellow);
73
+ (process as any).send(message);
74
+ }
75
+ });
76
+ }
77
+
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+ AddWorker = async (options: any): Promise<string> => {
80
+ const workerResponse: JSONObject = await this.#SendMessageToParentProcess({
81
+ requestResponse: true,
82
+ id: uuidv4(),
83
+ command: IPCMessageCommand.AddWorker,
84
+ requestDetail: {
85
+ options
86
+ }
87
+ });
88
+ return workerResponse.workerId;
89
+ }
90
+
91
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
+ DeleteWorker = async (workerId: string, options: any): Promise<JSONObject> => {
93
+ const workerResponse: JSONObject = await this.#SendMessageToParentProcess({
94
+ requestResponse: true,
95
+ id: uuidv4(),
96
+ command: IPCMessageCommand.DeleteWorker,
97
+ requestDetail: {
98
+ options,
99
+ workerId
100
+ }
101
+ });
102
+ return workerResponse;
103
+ }
104
+
105
+ ProcessTerminating = async (): Promise<void> => {
106
+ return;
107
+ }
108
+
109
+ SetupServer = async () =>
110
+ {
111
+ this.SetupInstrumentation();
112
+ setTimeout(() => {
113
+ this.SetupServerEx();
114
+ }, 100);
115
+ }
116
+
117
+ SetupServerEx = async () => {
118
+ this.ProcessStartup();
119
+
120
+ if (this.options.expressServerRouteFactory || this.options.expressServerRouteStaticFactory) {
121
+ this.expressServer = new STSExpressServer(this.options, this);
122
+ }
123
+
124
+ this.LogEx(`Worker instance starting. Service instance Id: [${this.options.serviceInstanceId}]`);
125
+
126
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ process.on('message', async (msg: any) => {
128
+ if (msg.requestResponse) {
129
+ const iPCMessagePayload: IPCMessagePayload = msg as IPCMessagePayload;
130
+ if (iPCMessagePayload.id) {
131
+ if (this.#inFlightMessage[iPCMessagePayload.id]) {
132
+ const responseMessage: IPCMessage = this.#inFlightMessage[iPCMessagePayload.id];
133
+ responseMessage.iPCMessagePayload.responseDetail = { ...iPCMessagePayload.responseDetail }
134
+ responseMessage.cb();
135
+ } else {
136
+ throw new Error(`Could not find Request/Response message with id: [${iPCMessagePayload.id}]`);
137
+ }
138
+ } else {
139
+ throw new Error(`Message does not have id attribute. [${JSON.stringify(iPCMessagePayload)}]`);
140
+ }
141
+ return;
142
+ }
143
+ if (msg.command) //@@ constants
144
+ {
145
+ switch (msg.command)
146
+ {
147
+ case 'Terminate' :
148
+ this.LogEx((`Received ` + colors.bold(`Terminate`.italic) + ` message from master thread`).gray);
149
+ await this.Terminate(true, false); // Don't kill the child process here, the master will take care of that ...
150
+ break;
151
+ case 'TerminateAndKill' :
152
+ this.LogEx((`Received ` + colors.bold(`Terminate`.italic) + ` message from master thread`).gray);
153
+ await this.Terminate(true, true);
154
+ break;
155
+ case 'Message' :
156
+ //this.LogEx((`Received ` + colors.bold(`Message`.italic) + ` message from master thread`).gray);
157
+ this.ReceivedMessageFromMaster(msg.data);
158
+ break;
159
+ case 'Response' : // General response to a req/response interaction
160
+ msg.details
161
+ }
162
+ }
163
+ });
164
+
165
+ // Signal Codes
166
+ // https://en.wikipedia.org/wiki/Signal_(IPC)
167
+ process.on('SIGTERM', async () =>
168
+ {
169
+ this.LogEx(`SIGTERM signal received for worker: ${process.pid}`);
170
+ await this.Terminate(true, true);
171
+ });
172
+
173
+ process.on('SIGINT', async () =>
174
+ {
175
+ this.LogEx(`SIGINT signal received for worker: ${process.pid}`);
176
+ await this.Terminate(true, true);
177
+ });
178
+
179
+ await this.SetupSTSServer();
180
+
181
+ this.WorkerStarted();
182
+
183
+ this.LogEx(`Worker process:${process.pid} started`.green);
184
+ };
185
+ }