@nsshunt/stsappframework 3.0.94 → 3.0.95

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