@nsshunt/stsappframework 3.1.209 → 3.1.211

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 (82) hide show
  1. package/build.sh +5 -0
  2. package/dist/commonTypes.js.map +1 -1
  3. package/dist/index.js +0 -5
  4. package/dist/index.js.map +1 -1
  5. package/dist/masterprocessbase.js +3 -53
  6. package/dist/masterprocessbase.js.map +1 -1
  7. package/dist/processbase.js +4 -17
  8. package/dist/processbase.js.map +1 -1
  9. package/dist/testing/app.js +0 -457
  10. package/dist/testing/app.js.map +1 -1
  11. package/dist/workerprocessbase.js +1 -36
  12. package/dist/workerprocessbase.js.map +1 -1
  13. package/package.json +14 -13
  14. package/src/commonTypes.ts +2 -33
  15. package/src/index.ts +0 -5
  16. package/src/masterprocessbase.ts +4 -18
  17. package/src/processbase.ts +7 -22
  18. package/src/testing/app.ts +1 -530
  19. package/src/workerprocessbase.ts +1 -40
  20. package/types/commonTypes.d.ts +0 -23
  21. package/types/commonTypes.d.ts.map +1 -1
  22. package/types/index.d.ts +0 -5
  23. package/types/index.d.ts.map +1 -1
  24. package/types/masterprocessbase.d.ts.map +1 -1
  25. package/types/processbase.d.ts +0 -2
  26. package/types/processbase.d.ts.map +1 -1
  27. package/types/workerprocessbase.d.ts.map +1 -1
  28. package/dist/ipcMessageHandler.js +0 -189
  29. package/dist/ipcMessageHandler.js.map +0 -1
  30. package/dist/ipcMessageManager.js +0 -146
  31. package/dist/ipcMessageManager.js.map +0 -1
  32. package/dist/ipcMessageProcessorPrimary.js +0 -65
  33. package/dist/ipcMessageProcessorPrimary.js.map +0 -1
  34. package/dist/ipcMessageProcessorWorker.js +0 -61
  35. package/dist/ipcMessageProcessorWorker.js.map +0 -1
  36. package/dist/messagehandling/webWorkerMessageHandler.js +0 -280
  37. package/dist/messagehandling/webWorkerMessageHandler.js.map +0 -1
  38. package/dist/messagehandling/webWorkerSupport.js +0 -62
  39. package/dist/messagehandling/webWorkerSupport.js.map +0 -1
  40. package/dist/redisMessageHandler.js +0 -305
  41. package/dist/redisMessageHandler.js.map +0 -1
  42. package/dist/redisMessageHandler.test.js +0 -129
  43. package/dist/redisMessageHandler.test.js.map +0 -1
  44. package/dist/testing/app_ipc_legacy.js +0 -84
  45. package/dist/testing/app_ipc_legacy.js.map +0 -1
  46. package/dist/testing/app_ipcex.js +0 -69
  47. package/dist/testing/app_ipcex.js.map +0 -1
  48. package/dist/testing/app_ww.js +0 -54
  49. package/dist/testing/app_ww.js.map +0 -1
  50. package/src/ipcMessageHandler.ts +0 -201
  51. package/src/ipcMessageManager.ts +0 -171
  52. package/src/ipcMessageProcessorPrimary.ts +0 -76
  53. package/src/ipcMessageProcessorWorker.ts +0 -70
  54. package/src/messagehandling/webWorkerMessageHandler.ts +0 -341
  55. package/src/messagehandling/webWorkerSupport.ts +0 -66
  56. package/src/redisMessageHandler.test.ts +0 -157
  57. package/src/redisMessageHandler.ts +0 -371
  58. package/src/testing/app_ipc_legacy.ts +0 -87
  59. package/src/testing/app_ipcex.ts +0 -68
  60. package/src/testing/app_ww.ts +0 -68
  61. package/types/ipcMessageHandler.d.ts +0 -30
  62. package/types/ipcMessageHandler.d.ts.map +0 -1
  63. package/types/ipcMessageManager.d.ts +0 -30
  64. package/types/ipcMessageManager.d.ts.map +0 -1
  65. package/types/ipcMessageProcessorPrimary.d.ts +0 -26
  66. package/types/ipcMessageProcessorPrimary.d.ts.map +0 -1
  67. package/types/ipcMessageProcessorWorker.d.ts +0 -25
  68. package/types/ipcMessageProcessorWorker.d.ts.map +0 -1
  69. package/types/messagehandling/webWorkerMessageHandler.d.ts +0 -52
  70. package/types/messagehandling/webWorkerMessageHandler.d.ts.map +0 -1
  71. package/types/messagehandling/webWorkerSupport.d.ts +0 -6
  72. package/types/messagehandling/webWorkerSupport.d.ts.map +0 -1
  73. package/types/redisMessageHandler.d.ts +0 -51
  74. package/types/redisMessageHandler.d.ts.map +0 -1
  75. package/types/redisMessageHandler.test.d.ts +0 -2
  76. package/types/redisMessageHandler.test.d.ts.map +0 -1
  77. package/types/testing/app_ipc_legacy.d.ts +0 -2
  78. package/types/testing/app_ipc_legacy.d.ts.map +0 -1
  79. package/types/testing/app_ipcex.d.ts +0 -2
  80. package/types/testing/app_ipcex.d.ts.map +0 -1
  81. package/types/testing/app_ww.d.ts +0 -2
  82. package/types/testing/app_ww.d.ts.map +0 -1
@@ -1,66 +0,0 @@
1
- /* eslint @typescript-eslint/no-explicit-any: 0, @typescript-eslint/no-unused-vars: 0 */ // --> OFF */ // --> OFF
2
- import { parentPort, MessagePort } from 'worker_threads';
3
-
4
- import { WebWorkerMessageHandler } from './webWorkerMessageHandler'
5
-
6
- import { defaultLogger, JSONObject } from '@nsshunt/stsutils'
7
-
8
- import chalk from 'chalk';
9
-
10
- export class WorkerSupport {
11
- #wwClient: WebWorkerMessageHandler | null = null;
12
-
13
- constructor() {
14
- this.#wwClient = new WebWorkerMessageHandler({
15
- logger: defaultLogger,
16
- role: 'CLIENT',
17
- namespace: 'mytestapp',
18
- groups: [ ],
19
- ignoreEvents: [ 'ping' ],
20
- messagePort: parentPort as MessagePort
21
- });
22
- this.#wwClient.Start();
23
-
24
- this.#wwClient.on('test', (arg1: JSONObject, callback: any) => {
25
- callback( {
26
- status: `PID: [${process.pid}]: response message from event = test with args ${JSON.stringify(arg1)}`
27
- })
28
- });
29
-
30
- this.#wwClient.on('globalmessage', (arg1: JSONObject, callback: any) => {
31
- callback( {
32
- status: `PID: [${process.pid}]: response message from event = globalmessage with args ${JSON.stringify(arg1)}`
33
- })
34
- });
35
-
36
- setTimeout(async () => {
37
- const sendObj1 = { from: 'child1' };
38
- console.log(chalk.magenta(`calling globalmessage = [${JSON.stringify(sendObj1)}]`));
39
- const retVal1 = await this.#wwClient?.emitex('globalmessage', sendObj1);
40
- console.log(chalk.magenta(`caller response from globalmessage = [${JSON.stringify(retVal1)}]`));
41
-
42
- const sendObj2 = { from: 'child2' };
43
- console.log(chalk.magenta(`calling test = [${JSON.stringify(sendObj2)}]`));
44
- const retVal2 = await this.#wwClient?.emitex('test', sendObj2);
45
- console.log(chalk.magenta(`caller response from test = [${JSON.stringify(retVal2)}]`));
46
- }, 2000);
47
- }
48
-
49
- ProcessMessage = async(data: any) => {
50
- console.log(`Received: [${data}]`);
51
- //parentPort?.postMessage(data);
52
- }
53
- }
54
-
55
- new WorkerSupport();
56
-
57
- /*
58
- parentPort?.on('message', (data: any) => {
59
- if (isNode) {
60
- worker.ProcessMessage(data);
61
- } else {
62
- // const payloadMessage: IIWMessagePayload = data.data as IIWMessagePayload; // browser version
63
- worker.ProcessMessage(data.data); // browser version
64
- }
65
- });
66
- */
@@ -1,157 +0,0 @@
1
- /* eslint @typescript-eslint/no-explicit-any: 0 */ // --> OFF
2
- import { beforeAll, afterAll, test, describe, expect } from 'vitest';
3
-
4
- import { RedisMessageHandler } from './redisMessageHandler'
5
-
6
- import { JSONObject, Sleep, defaultLogger } from '@nsshunt/stsutils';
7
-
8
- import chalk from 'chalk';
9
-
10
- import { v4 as uuidv4 } from 'uuid';
11
-
12
- import { GenericContainer } from "testcontainers";
13
-
14
- describe.skip("Redis Message Handler Test", () =>
15
- {
16
- let ioRedisContainer: any;
17
- let ioRedisMessageProcessorUrl = '';
18
-
19
- beforeAll(async () => {
20
- // --- [ioRedis ] -------------------------------------------------------------------------
21
-
22
- ioRedisContainer = await new GenericContainer("redis")
23
- .withExposedPorts(6379)
24
- .start();
25
- ioRedisMessageProcessorUrl = `redis://${ioRedisContainer.getHost()}:${ioRedisContainer.getMappedPort(6379)}`;
26
- });
27
-
28
- afterAll(async () => {
29
- if (ioRedisContainer) {
30
- await ioRedisContainer.stop();
31
- }
32
- }, 5000);
33
-
34
- class Server {
35
- #r1: RedisMessageHandler;
36
-
37
- constructor() {
38
- this.#r1 = new RedisMessageHandler({
39
- logger: defaultLogger,
40
- role: 'SERVER',
41
- redisUrl: ioRedisMessageProcessorUrl, // goptions.imRedisMessageProcessorUrl,
42
- namespace: 'redistestingstsframework',
43
- groups: [ ]
44
- });
45
- this.#r1.Start();
46
-
47
- this.#r1.on('fromworkerredis', (arg1: string, arg2: number, arg3: string, callback: any) => {
48
- console.log(chalk.magenta(`${process.pid}: fromworkerredis.ON: [${arg1} ${arg2} ${arg3}]`));
49
- //callback(null);
50
- callback({status: `response message from event = fromworkerredis with args ${arg1} ${arg2} ${arg3}`})
51
- });
52
- }
53
-
54
- Start = async (expectedResponses: number, iterations: number, delay: number) => {
55
- for (let i=0; i < iterations; i++) {
56
- /*
57
- Object.keys(p).forEach(async (pKey) => {
58
- const emitobj = { i: i, a: 'a', b: 'b', worker: pKey };
59
- console.log(chalk.green(`${process.pid}: emit event to worker: [${pKey}] fromprimary: [${JSON.stringify(emitobj)}`));
60
- p[pKey].emit('fromprimary', emitobj, (response: any) => {
61
- console.log(chalk.green(`${process.pid}: ${JSON.stringify(response)}`));
62
- });
63
- });
64
- */
65
-
66
-
67
- const emitobj = { i: i, a: 'a', b: 'b' };
68
- console.log(chalk.cyan(`${process.pid}: emit event fromprimaryredis fromprimary: [${JSON.stringify(emitobj)}`));
69
- this.#r1.emit('fromprimaryredis', emitobj, (response: any) => {
70
- console.log(chalk.cyan(`${process.pid}: Response(fromprimaryredis): Count: [${response.length}] [${JSON.stringify(response)}]`));
71
- expect(response.length).toEqual(expectedResponses);
72
- });
73
-
74
- await Sleep(delay);
75
- }
76
- }
77
-
78
- Stop = () => {
79
- this.#r1.Stop();
80
- }
81
- }
82
-
83
- class Client {
84
- #r1: RedisMessageHandler;
85
-
86
- constructor() {
87
- this.#r1 = new RedisMessageHandler({
88
- logger: defaultLogger,
89
- role: 'CLIENT',
90
- redisUrl: ioRedisMessageProcessorUrl, // goptions.imRedisMessageProcessorUrl,
91
- namespace: 'redistestingstsframework',
92
- groups: [ ]
93
- });
94
- this.#r1.Start();
95
-
96
- this.#r1.on('fromprimaryredis', (arg1: JSONObject, callback: any) => {
97
- //callback();
98
- console.log(chalk.yellow(`${process.pid}: fromprimaryredis.ON: [${JSON.stringify(arg1)}]`));
99
- callback({status: `PID: [${process.pid}]: response message from event = fromprimaryredis with args ${JSON.stringify(arg1)}`})
100
- });
101
- }
102
-
103
- Start = async (iterations: number, delay: number) => {
104
- for (let i=0; i < iterations; i++) {
105
- const id = uuidv4();
106
- /*
107
- console.log(chalk.yellow(`${process.pid}: emit event fromworker: [${id} ${i} Hello]`));
108
- w1.emit('fromworker', id, i, 'Hello', (response: any) => {
109
- console.log(chalk.yellow(`${process.pid}: ${JSON.stringify(response)}`));
110
- });
111
- */
112
-
113
- console.log(chalk.green(`${process.pid}: emit event fromworkerredis: [${id} ${i} Hello]`));
114
- this.#r1.emit('fromworkerredis', id, i, 'Hello', (response: any) => {
115
- console.log(chalk.green(`${process.pid}: Response(fromworkerredis): [${JSON.stringify(response)}]`));
116
- });
117
- await Sleep(delay);
118
- }
119
- }
120
-
121
- Stop = () => {
122
- this.#r1.Stop();
123
- }
124
- }
125
-
126
-
127
- test('Test 1', async () =>
128
- {
129
- const clientCount = 20;
130
- const iterations = 5;
131
- const delay = 100;
132
-
133
- expect.assertions(iterations);
134
-
135
- // Setup new server and clients
136
- const server = new Server();
137
- const clients: Client[] = [ ];
138
- for (let i=0; i < clientCount; i++) {
139
- clients.push(new Client());
140
- }
141
-
142
- // Need this delay to ensure the pings are all sent and received between the clients and the server
143
- await Sleep(2000);
144
-
145
- // Start sending messages
146
- server.Start(clientCount, iterations, delay);
147
- clients.forEach(c => c.Start(iterations, delay));
148
-
149
- // Allow time to process
150
- await Sleep(3000);
151
-
152
- // All done, stop.
153
- clients.forEach(c => c.Stop());
154
- server.Stop();
155
- }, 30000);
156
- });
157
-
@@ -1,371 +0,0 @@
1
- /* eslint @typescript-eslint/no-explicit-any: 0, @typescript-eslint/no-unused-vars: 0 */ // --> OFF */ // --> OFF
2
- import { TinyEmitter } from "tiny-emitter";
3
- import { ISTSLogger, JSONObject } from '@nsshunt/stsutils'
4
-
5
- import { IIPCMessageProcessorIPCPayload, IServiceProcessContext, ProcessOptions } from './commonTypes'
6
-
7
- import { IPCMessageManager, IPCMessageManagerOptions } from './ipcMessageManager'
8
- import { Redis, RedisOptions } from "ioredis";
9
-
10
- import chalk from 'chalk';
11
-
12
- const REQUEST_CHANNEL = '__STS__SVC_stsappframework_request'
13
- const RESPONSE_CHANNEL = '__STS__SVC_stsappframework_response'
14
-
15
- export interface IRedisAdminManagerOptions {
16
- redisUrl: string
17
- logger: ISTSLogger
18
- role: 'SERVER' | 'CLIENT'
19
- namespace: string
20
- groups: string[]
21
- ignoreEvents?: string[]
22
- processOptions?: ProcessOptions
23
- }
24
-
25
- export interface IClientRecord {
26
- id: string
27
- clientConnected: Date
28
- pingCount: number
29
- timeout: NodeJS.Timeout
30
- groups: string[]
31
- serviceProcessContext?: IServiceProcessContext
32
- }
33
-
34
- export interface IEventPayload {
35
- __eventName: string
36
- args: any[]
37
- }
38
-
39
- export interface IEventRecord {
40
- event: string
41
- callback: any,
42
- ctx?: any
43
- }
44
-
45
- export interface IPingData {
46
- id: string
47
- groups: string[]
48
- serviceProcessContext?: IServiceProcessContext
49
- }
50
-
51
- export class RedisMessageHandler extends TinyEmitter {
52
- #ipcMessageManager: IPCMessageManager | null = null;
53
- #options: IRedisAdminManagerOptions;
54
- #events: Record<string, IEventRecord> = { };
55
- #requestChannel: string;
56
- #responseChannel: string;
57
- #redisSubscriber: Redis;
58
- #redisPublisher: Redis;
59
- #clients: Record<string, IClientRecord> = { };
60
- #pingTimeout: NodeJS.Timeout | null = null;
61
-
62
- constructor(options: IRedisAdminManagerOptions) {
63
- super();
64
- this.#options = options;
65
-
66
- this.#requestChannel = REQUEST_CHANNEL
67
- this.#responseChannel = RESPONSE_CHANNEL
68
-
69
- const redisOptions: RedisOptions = {
70
- showFriendlyErrorStack: true,
71
- maxRetriesPerRequest: 20
72
- }
73
-
74
- this.#redisSubscriber = new Redis(this.#options.redisUrl, redisOptions);
75
- this.#redisPublisher = new Redis(this.#options.redisUrl, redisOptions);
76
-
77
- this.#redisSubscriber.subscribe(this.#requestChannel, this.#responseChannel, (error, count) => {
78
- if (error) {
79
- // Just like other commands, subscribe() can fail for some reasons, // ex network issues.
80
- this.#LogError(chalk.red(`RedisAdminManager:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Error - Failed to subscribe: [${error}]`));
81
- } else {
82
- // `count` represents the number of channels this client is currently subscribed to.
83
- this.#LogInfo(chalk.white(`RedisAdminManager:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Subscribed successfully! This client is currently subscribed to ${count} channels.`));
84
- }
85
- });
86
-
87
- this.SetupPrimary();
88
-
89
- if (this.#options.role.localeCompare('CLIENT') === 0) {
90
- const ping = () => {
91
- this.#pingTimeout = setTimeout(() => {
92
- const pingData: IPingData = {
93
- id: (this.#ipcMessageManager as IPCMessageManager).id,
94
- groups: this.#options.groups,
95
- }
96
- if (this.#options.processOptions) {
97
- pingData.serviceProcessContext = this.#options.processOptions.serviceProcessContext;
98
- }
99
- this.emit('ping', pingData, (response: any) => { });
100
- ping();
101
- }, 1000).unref();
102
- }
103
- ping();
104
- } else {
105
- this.on('ping', (pingData: IPingData, callback: any) => {
106
- const { id, groups, serviceProcessContext } = pingData;
107
- if (this.#clients[id]) {
108
- clearTimeout(this.#clients[id].timeout);
109
- this.#clients[id].pingCount++;
110
- this.#clients[id].timeout = setTimeout(() => {
111
- delete this.#clients[id];
112
- }, 2000);
113
- this.#clients[id].groups = groups;
114
- this.#clients[id].serviceProcessContext = serviceProcessContext;
115
- } else {
116
- this.#clients[id] = {
117
- id,
118
- clientConnected: new Date(),
119
- pingCount: 0,
120
- timeout: setTimeout(() => {
121
- delete this.#clients[id];
122
- }, 2000),
123
- groups,
124
- serviceProcessContext
125
- }
126
- }
127
- callback('ok');
128
- });
129
- }
130
- }
131
-
132
- #LogInfo(message: any) {
133
- this.#options.logger.info(message);
134
- }
135
-
136
- #LogError(message: any) {
137
- this.#options.logger.error(message);
138
- }
139
-
140
- #processRawMessage = (channel: string, rawmessage: string) => {
141
- const message = JSON.parse(rawmessage);
142
- this.#ipcMessageManager?.ProcessMessage(message, { channel });
143
- }
144
-
145
- get clients(): Record<string, IClientRecord> {
146
- return this.#clients;
147
- }
148
-
149
- get groups(): string[] {
150
- return this.#options.groups;
151
- }
152
-
153
- AddGroup = (group: string) => {
154
- const index = this.#options.groups.indexOf(group);
155
- if (index === -1) {
156
- this.#options.groups.push(group);
157
- }
158
- }
159
-
160
- RemoveGroup = (group: string) => {
161
- const removeIndex = this.#options.groups.indexOf(group);
162
- if (removeIndex !== -1) {
163
- this.#options.groups.splice(removeIndex, 1);
164
- }
165
- }
166
-
167
- SetupPrimary = () => {
168
- const ipcMessageManagerOptions: IPCMessageManagerOptions = {
169
- logger: this.#options.logger,
170
- requestResponseMessageTimeout: 5000,
171
- namespace: this.#options.namespace,
172
- role: this.#options.role,
173
- messageSender: this.#messageSender,
174
- groups: this.#options.groups,
175
- // This method is used to calculate if all responses have been received from multiple clients (broadcast)
176
- // returns true/false.
177
- ProcessResponseMessage: this.#ProcessResponseMessage,
178
- // This gets called when an event is received from a message receiver (when ProcessMessage is invoked from the receiver event handler)
179
- ProcessRequestMessage: this.#processPayload,
180
-
181
- messageReceiverStart: (options: any) => {
182
- this.#redisSubscriber.on("message", this.#processRawMessage);
183
- },
184
-
185
- messageReceiverStop: (options: any) => {
186
- this.#redisSubscriber.off("message", this.#processRawMessage);
187
- }
188
- }
189
- this.#ipcMessageManager = new IPCMessageManager(ipcMessageManagerOptions);
190
- }
191
-
192
-
193
- #messageSender = (payload: IIPCMessageProcessorIPCPayload, options: any) => {
194
- if (payload.messageType.localeCompare('REQUEST') === 0) {
195
- this.#redisPublisher.publish(this.#requestChannel, JSON.stringify(payload));
196
- } else if (payload.messageType.localeCompare('RESPONSE') === 0) {
197
- this.#redisPublisher.publish(this.#responseChannel, JSON.stringify(payload));
198
- }
199
- }
200
-
201
-
202
- #ProcessResponseMessage = async (responses: Record<string, IIPCMessageProcessorIPCPayload>, options: any): Promise<boolean> => {
203
- // Now check if we have all responses ...
204
- let allFound = false;
205
-
206
- for (const [responseId, response] of Object.entries(responses)) {
207
- if (response.senderRole.localeCompare('CLIENT') === 0) {
208
- allFound = true;
209
- break;
210
- }
211
- }
212
- if (allFound) {
213
- return allFound;
214
- }
215
-
216
- let found = true;
217
-
218
- // Sender role here is SERVER
219
- let requestGroup = null;
220
- for (const [responseId, response] of Object.entries(responses)) {
221
- if (response.requestPayload.args.length > 0 && response.requestPayload.args[0].group) {
222
- requestGroup = response.requestPayload.args[0].group;
223
- break;
224
- }
225
- }
226
-
227
- if (requestGroup) {
228
- const clientsInGroup = Object.values(this.#clients).filter(c => {
229
- if (c.groups.indexOf(requestGroup) === -1) {
230
- return false;
231
- }
232
- return true;
233
- });
234
-
235
- // Now make sure that all clients are in the responses
236
- found = true;
237
- clientsInGroup.forEach(c => {
238
- if (!responses[c.id]) {
239
- found = false;
240
- }
241
- })
242
- } else {
243
- const clientsInGroup = Object.values(this.#clients)
244
-
245
- // Now make sure that all clients are in the responses
246
- found = true;
247
- clientsInGroup.forEach(c => {
248
- if (!responses[c.id]) {
249
- found = false;
250
- }
251
- })
252
- }
253
-
254
- return found;
255
- }
256
-
257
- #processPayload = (payload: IIPCMessageProcessorIPCPayload, options: any): Promise<JSONObject> => {
258
- // check the event name from the collection and invoke that function
259
- return new Promise<JSONObject>((resolve, reject) => {
260
- if (payload.messageType.localeCompare('REQUEST') === 0) {
261
- if (payload.requestPayload['__eventName']) {
262
- const eventName = payload.requestPayload['__eventName'];
263
- // Only process events that I have registered interest in (using .on)
264
- if (this.#events[eventName]) {
265
- try {
266
- //const retVal = this.#events[eventName].callback(payload.requestPayload.args, payload, options, this.#events[eventName].ctx);
267
- this.#events[eventName].callback(...payload.requestPayload.args, (responseMessage: any) => {
268
- resolve(responseMessage);
269
- });
270
- } catch (error) {
271
- reject(error);
272
- }
273
- }
274
- }
275
- }
276
- });
277
- }
278
-
279
- // p.on('fromworker', (arg1: number, arg2: string, callback: any) => {
280
- override on(event: string, callback: any, ctx?: any): this {
281
- if (this.#events[event]) {
282
- // Update the event with the same name
283
- delete this.#events[event];
284
- }
285
- const eventObject: IEventRecord = {
286
- event,
287
- callback,
288
- ctx
289
- }
290
- this.#events[eventObject.event] = eventObject;
291
- return this;
292
- }
293
-
294
- override off(event: string, callback?: any): this {
295
- if (this.#events[event]) {
296
- delete this.#events[event];
297
- }
298
- return this;
299
- }
300
-
301
- Start = () => {
302
- this.#ipcMessageManager?.Start();
303
- }
304
-
305
- Stop = () => {
306
- if (this.#pingTimeout) {
307
- clearTimeout(this.#pingTimeout);
308
- this.#pingTimeout = null;
309
- }
310
-
311
- this.#ipcMessageManager?.Stop();
312
-
313
- this.#redisSubscriber.quit();
314
- this.#redisSubscriber.disconnect();
315
-
316
- this.#redisPublisher.quit();
317
- this.#redisPublisher.disconnect();
318
- }
319
-
320
- override emit(event: string, ...args: any[]): this {
321
- (async () => {
322
- try {
323
- const retVal = await this.#ipcMessageManager?.SendMessage({
324
- __eventName: event,
325
- args: args.slice(0, args.length-1)
326
- } as IEventPayload);
327
- // Invoke the response callback
328
- args[args.length-1](retVal);
329
- } catch (error) {
330
- if (this.#options.ignoreEvents) {
331
- //console.log(chalk.red(`RedisMessageHandler:emit(): ignoreEvents: [${this.#options.ignoreEvents}]`))
332
- if (this.#options.ignoreEvents.indexOf((error as any).__eventName) !== -1) {
333
- return;
334
- }
335
- }
336
- this.#options.logger.error(chalk.red(`RedisMessageHandler:emit(): Error: [${JSON.stringify(error)}]`));
337
- }
338
- })();
339
- return this;
340
- }
341
-
342
- emitWithError(event: string, args: JSONObject, responseCb: (response: JSONObject | undefined) => void, errorCb: (error: any) => void): this {
343
- (async () => {
344
- try {
345
- const retVal = await this.#ipcMessageManager?.SendMessage({
346
- __eventName: event,
347
- args: [ args ]
348
- } as IEventPayload);
349
- // Invoke the response callback
350
- responseCb(retVal);
351
- } catch (error) {
352
- if (this.#options.ignoreEvents) {
353
- //console.log(chalk.red(`RedisMessageHandler:emit(): ignoreEvents: [${this.#options.ignoreEvents}]`))
354
- if (this.#options.ignoreEvents.indexOf((error as any).__eventName) !== -1) {
355
- return;
356
- }
357
- }
358
- //this.#options.logger.error(chalk.red(`RedisMessageHandler:emit(): Error: [${JSON.stringify(error)}]`));
359
- errorCb(error);
360
- }
361
- })();
362
- return this;
363
- }
364
-
365
- emitex = async(event: string, ...args: any[]): Promise<JSONObject> => {
366
- return (this.#ipcMessageManager as IPCMessageManager).SendMessage({
367
- __eventName: event,
368
- args
369
- } as IEventPayload);
370
- }
371
- }
@@ -1,87 +0,0 @@
1
- /* eslint @typescript-eslint/no-explicit-any: 0, @typescript-eslint/no-unused-vars: 0 */ // --> OFF
2
- import { ServiceConfigOptions } from './appConfig'
3
- import { MasterProcessBase } from './..'
4
- import { WorkerProcess } from './appWorkerWSS'
5
-
6
- import cluster, { Worker } from 'node:cluster';
7
-
8
- import { IPCMessageProcessorWorker } from './../ipcMessageProcessorWorker'
9
- import { defaultLogger } from '@nsshunt/stsutils';
10
-
11
- import { IPCMessageProcessorPrimary } from './../ipcMessageProcessorPrimary'
12
-
13
- import { IIPCMessageProcessorIPCPayload } from './../commonTypes'
14
-
15
- import chalk from 'chalk';
16
-
17
- const iterations = 2000;
18
-
19
- const StartTestPrimary = () => {
20
- setTimeout(async () => {
21
- for (const id in cluster.workers) {
22
- const p1 = new IPCMessageProcessorPrimary({
23
- logger: defaultLogger,
24
- namespace: 'test1',
25
- processPayload: async (payload: IIPCMessageProcessorIPCPayload) => {
26
- console.log(chalk.grey(`Inside Primary ==> [${process.pid}] Received message from worker: [${JSON.stringify(payload.requestPayload)}]`));
27
- return {
28
- msg: `From PRIMARY: [${process.pid}]`,
29
- yousent: payload.requestPayload,
30
- hello1: `world1`,
31
- pid: process.pid
32
- };
33
- }
34
- })
35
- p1.Start(cluster.workers[id] as Worker);
36
- setTimeout(async () => {
37
- for (let i=0; i < iterations; i++) {
38
- const sendMessage = {
39
- from1: `From PRIMARY: [${process.pid}]`,
40
- loop1: `${i}`,
41
- };
42
- console.log(chalk.yellow(`Inside Primary ==> [${process.pid}] Sending message to worker: [${JSON.stringify(sendMessage)}]`));
43
- const retVal1 = await p1.SendMessage(sendMessage);
44
- console.log(chalk.yellow(` Inside Primary ==> [${process.pid}] Response from worker: [${JSON.stringify(retVal1)}]`));
45
- }
46
- }, 1000);
47
- }
48
- }, 2000);
49
- }
50
-
51
- const StartTestWorker = () => {
52
- setTimeout(() => {
53
- const w1 = new IPCMessageProcessorWorker({
54
- logger: defaultLogger,
55
- namespace: 'test1',
56
- requestResponseMessageTimeout: 5000,
57
- processPayload: async (payload) => {
58
- console.log(chalk.cyan(`Inside Worker ==> [${process.pid}] Received message from primary: [${JSON.stringify(payload.requestPayload)}]`));
59
- return {
60
- msg1: `From WORKER: [${process.pid}]`,
61
- yousent1: payload.requestPayload,
62
- pid1: process.pid
63
- }
64
- }
65
- })
66
- w1.Start();
67
- setTimeout(async () => {
68
- for (let i=0; i < iterations; i++) {
69
- const sendMessage = {
70
- message:`From WORKER ${process.pid}`,
71
- loop1: `${i}`,
72
- };
73
- console.log(chalk.blue(`Inside Worker ==> [${process.pid}] Sending message to primary: [${JSON.stringify(sendMessage)}]`));
74
- const retVal1 = await w1.SendMessage(sendMessage);
75
- console.log(chalk.blue(` Inside Worker ==> [${process.pid}] Response from primary: [${JSON.stringify(retVal1)}]`));
76
- }
77
- }, 1000);
78
- }, 2000);
79
- }
80
-
81
- if (cluster.isPrimary) {
82
- new MasterProcessBase(ServiceConfigOptions(true, cluster.isPrimary)).SetupServer();
83
- StartTestPrimary();
84
- } else {
85
- new WorkerProcess(ServiceConfigOptions(true, cluster.isPrimary)).SetupServer();
86
- StartTestWorker();
87
- }