@imqueue/rpc 3.4.4 → 3.5.1
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.
- package/CHANGELOG.md +12 -0
- package/index.d.ts +58 -0
- package/index.js +58 -0
- package/package.json +1 -1
- package/src/IMQCache.d.ts +20 -11
- package/src/IMQCache.js +20 -11
- package/src/IMQClient.d.ts +191 -38
- package/src/IMQClient.js +204 -57
- package/src/IMQDelay.d.ts +29 -6
- package/src/IMQDelay.js +20 -6
- package/src/IMQLock.d.ts +81 -35
- package/src/IMQLock.js +38 -35
- package/src/IMQMetadata.d.ts +9 -1
- package/src/IMQMetadata.js +1 -1
- package/src/IMQRPCDescription.d.ts +183 -9
- package/src/IMQRPCDescription.js +22 -0
- package/src/IMQRPCError.d.ts +91 -9
- package/src/IMQRPCError.js +23 -7
- package/src/IMQRPCOptions.d.ts +221 -18
- package/src/IMQRPCOptions.js +22 -6
- package/src/IMQRPCRequest.d.ts +39 -1
- package/src/IMQRPCResponse.d.ts +34 -0
- package/src/IMQRequestContext.d.ts +2 -5
- package/src/IMQRequestContext.js +2 -5
- package/src/IMQService.d.ts +144 -19
- package/src/IMQService.js +151 -33
- package/src/cache/ICache.d.ts +62 -16
- package/src/cache/RedisCache.d.ts +58 -17
- package/src/cache/RedisCache.js +43 -17
- package/src/decorators/cache.d.ts +46 -2
- package/src/decorators/cache.js +2 -2
- package/src/decorators/classType.d.ts +28 -13
- package/src/decorators/classType.js +28 -13
- package/src/decorators/expose.d.ts +59 -7
- package/src/decorators/expose.js +77 -30
- package/src/decorators/indexed.d.ts +27 -8
- package/src/decorators/indexed.js +27 -8
- package/src/decorators/lock.d.ts +20 -2
- package/src/decorators/lock.js +2 -2
- package/src/decorators/logged.d.ts +33 -2
- package/src/decorators/logged.js +2 -2
- package/src/decorators/property.d.ts +97 -4
- package/src/decorators/property.js +84 -6
- package/src/decorators/remote.d.ts +1 -1
- package/src/decorators/remote.js +1 -1
- package/src/helpers/fs.d.ts +5 -7
- package/src/helpers/fs.js +8 -10
- package/src/helpers/os-uuid.d.ts +0 -2
- package/src/helpers/os-uuid.js +2 -10
- package/src/helpers/pid.d.ts +7 -7
- package/src/helpers/pid.js +10 -11
- package/src/helpers/signature.d.ts +4 -4
- package/src/helpers/signature.js +7 -7
package/src/IMQService.d.ts
CHANGED
|
@@ -24,11 +24,41 @@
|
|
|
24
24
|
import { type JsonObject, type ILogger, type IMessageQueue } from '@imqueue/core';
|
|
25
25
|
import { type TypesDescription, type IMQRPCRequest, type IMQRPCResponse, type IMQServiceOptions, type ICache, type MethodsCollectionDescription } from './index.js';
|
|
26
26
|
import { type Server } from 'node:http';
|
|
27
|
+
/**
|
|
28
|
+
* The self-description a service serves to its clients, and the input to client
|
|
29
|
+
* generation.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Declared as a class for historical reasons but never instantiated: a service
|
|
33
|
+
* returns a plain object literal, and a client receives its JSON round-trip. So do
|
|
34
|
+
* not use `new Description()` or `instanceof Description`.
|
|
35
|
+
*
|
|
36
|
+
* A service memoizes its description after the first request for it.
|
|
37
|
+
*/
|
|
27
38
|
export declare class Description {
|
|
39
|
+
/**
|
|
40
|
+
* The service itself: its name, and every exposed method with the inheritance
|
|
41
|
+
* chain already flattened.
|
|
42
|
+
*/
|
|
28
43
|
service: {
|
|
44
|
+
/**
|
|
45
|
+
* The service name, which is also its queue name.
|
|
46
|
+
*/
|
|
29
47
|
name: string;
|
|
48
|
+
/**
|
|
49
|
+
* Every exposed method, keyed by name, with parent-class methods merged in
|
|
50
|
+
* and overridden by same-named subclass methods.
|
|
51
|
+
*/
|
|
30
52
|
methods: MethodsCollectionDescription;
|
|
31
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Every complex type registered in the service's process.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* This is the process-global type registry, embedded by reference and
|
|
59
|
+
* unfiltered — so it includes types this service does not use, and each becomes
|
|
60
|
+
* a generated interface in every client.
|
|
61
|
+
*/
|
|
32
62
|
types: TypesDescription;
|
|
33
63
|
}
|
|
34
64
|
/**
|
|
@@ -36,27 +66,81 @@ export declare class Description {
|
|
|
36
66
|
* Basic abstract service (server-side) implementation
|
|
37
67
|
*/
|
|
38
68
|
export declare abstract class IMQService {
|
|
69
|
+
/**
|
|
70
|
+
* Allows methods to be dispatched dynamically by name.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* A consequence worth knowing: arbitrary property access on a service instance
|
|
74
|
+
* type-checks as `any`, so typos in subclass code are not caught by the
|
|
75
|
+
* compiler.
|
|
76
|
+
*/
|
|
39
77
|
[property: string]: any;
|
|
78
|
+
/**
|
|
79
|
+
* The underlying message queue this service consumes requests from.
|
|
80
|
+
*/
|
|
40
81
|
protected imq: IMessageQueue;
|
|
82
|
+
/**
|
|
83
|
+
* The effective logger — `options.logger`, defaulting to `console`.
|
|
84
|
+
*/
|
|
41
85
|
protected logger: ILogger;
|
|
86
|
+
/**
|
|
87
|
+
* The cache adapter used by cached methods.
|
|
88
|
+
*
|
|
89
|
+
* @remarks
|
|
90
|
+
* Never assigned by the service itself: it is populated lazily by the
|
|
91
|
+
* {@link cache} decorator on first use, so it is `undefined` on a service that
|
|
92
|
+
* has no cached methods.
|
|
93
|
+
*/
|
|
42
94
|
protected cache: ICache;
|
|
95
|
+
/**
|
|
96
|
+
* The metrics HTTP server, present only while
|
|
97
|
+
* {@link IMQMetricsServerOptions.enabled} is set.
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* {@link IMQService.destroy} does not close it — do that yourself, or the
|
|
101
|
+
* open listener keeps the process alive.
|
|
102
|
+
*/
|
|
43
103
|
protected metricsServer?: Server<any, any>;
|
|
44
104
|
private readonly signalHandlers;
|
|
105
|
+
/**
|
|
106
|
+
* This service's name, which is also its queue name and the key its description
|
|
107
|
+
* is cached under.
|
|
108
|
+
*/
|
|
45
109
|
name: string;
|
|
110
|
+
/**
|
|
111
|
+
* The effective options for this service: {@link DEFAULT_IMQ_SERVICE_OPTIONS}
|
|
112
|
+
* merged with the values passed to the constructor.
|
|
113
|
+
*/
|
|
46
114
|
options: IMQServiceOptions;
|
|
47
115
|
/**
|
|
48
|
-
*
|
|
116
|
+
* Constructs a service.
|
|
117
|
+
*
|
|
118
|
+
* @param options - service options, shallow-merged over
|
|
119
|
+
* {@link DEFAULT_IMQ_SERVICE_OPTIONS}, except `metricsServer` which is
|
|
120
|
+
* merged separately over
|
|
121
|
+
* {@link DEFAULT_IMQ_METRICS_SERVER_OPTIONS}
|
|
122
|
+
* @param name - overrides the class name as the service/queue name, and as the
|
|
123
|
+
* description cache key
|
|
124
|
+
* @throws TypeError when {@link IMQService} is instantiated directly. Note the
|
|
125
|
+
* check is by resolved name, so passing `'IMQService'` as the `name` of a
|
|
126
|
+
* subclass throws as well.
|
|
49
127
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
128
|
+
* @remarks
|
|
129
|
+
* Constructing a service installs process signal handlers for `SIGTERM`,
|
|
130
|
+
* `SIGINT`, `SIGHUP` and `SIGQUIT`. On any of them the service tears the queue
|
|
131
|
+
* down, closes the metrics server, and then force-exits with code 0 after
|
|
132
|
+
* `IMQ_SHUTDOWN_TIMEOUT` — a fixed timer, not a drain: requests still being
|
|
133
|
+
* processed are not awaited.
|
|
134
|
+
*
|
|
135
|
+
* The merged options are passed straight to the queue factory, so `vendor` and
|
|
136
|
+
* `cluster`/`clusterManagers` select the transport implementation.
|
|
52
137
|
*/
|
|
53
138
|
constructor(options?: Partial<IMQServiceOptions>, name?: string);
|
|
54
139
|
/**
|
|
55
140
|
* Handles incoming request and produces corresponding response
|
|
56
141
|
*
|
|
57
|
-
* @param
|
|
58
|
-
* @param
|
|
59
|
-
* @return {Promise<string>}
|
|
142
|
+
* @param request - request message
|
|
143
|
+
* @param id - message unique identifier
|
|
60
144
|
*/
|
|
61
145
|
private handleRequest;
|
|
62
146
|
private processRequest;
|
|
@@ -64,31 +148,72 @@ export declare abstract class IMQService {
|
|
|
64
148
|
* Initializes this instance of service and starts handling request
|
|
65
149
|
* messages.
|
|
66
150
|
*
|
|
67
|
-
* @
|
|
151
|
+
* @returns the started message queue instance
|
|
152
|
+
*
|
|
153
|
+
* @remarks
|
|
154
|
+
* In single-process mode this starts the queue in the current process. With
|
|
155
|
+
* {@link IMQServiceOptions.multiProcess} the cluster primary forks
|
|
156
|
+
* `cpus().length * childrenPerCore` workers, each receiving its index in the
|
|
157
|
+
* `workerId` environment variable, and installs an exit watcher that terminates
|
|
158
|
+
* the process with code 1 as soon as one worker dies — workers are never
|
|
159
|
+
* respawned, so supervision is left to the process manager.
|
|
160
|
+
*
|
|
161
|
+
* Note that the primary also starts its own queue consumer and metrics listener
|
|
162
|
+
* after forking, so a service configured for N workers runs N+1 consumers and
|
|
163
|
+
* N+1 processes attempt to bind the metrics port.
|
|
68
164
|
*/
|
|
69
165
|
start(): Promise<IMessageQueue | undefined>;
|
|
70
166
|
/**
|
|
71
|
-
*
|
|
167
|
+
* Broadcasts data on this service's pub/sub channel, delivering it to every
|
|
168
|
+
* client currently subscribed to this service.
|
|
169
|
+
*
|
|
170
|
+
* @param data - JSON-serializable payload to broadcast
|
|
171
|
+
* @throws TypeError when the service has not been started, so the queue's writer
|
|
172
|
+
* is not connected
|
|
72
173
|
*
|
|
73
|
-
* @
|
|
174
|
+
* @remarks
|
|
175
|
+
* Fire-and-forget: plain pub/sub with no persistence, ordering guarantee or
|
|
176
|
+
* delivery confirmation, so subscribers that are offline lose the message.
|
|
74
177
|
*/
|
|
75
178
|
publish(data: JsonObject): Promise<void>;
|
|
76
179
|
/**
|
|
77
|
-
* Stops
|
|
180
|
+
* Stops consuming messages from the queue.
|
|
78
181
|
*
|
|
79
|
-
* @
|
|
182
|
+
* @remarks
|
|
183
|
+
* Connections, signal handlers and the metrics server are all left in place —
|
|
184
|
+
* use {@link IMQService.destroy} for full teardown. Calling this first is not a
|
|
185
|
+
* prerequisite for `destroy()`.
|
|
80
186
|
*/
|
|
81
187
|
stop(): Promise<void>;
|
|
82
188
|
/**
|
|
83
|
-
*
|
|
189
|
+
* Removes this instance's signal handlers, then unsubscribes and destroys the
|
|
190
|
+
* underlying queue.
|
|
84
191
|
*
|
|
85
|
-
* @
|
|
192
|
+
* @remarks
|
|
193
|
+
* It does not close the metrics server — if
|
|
194
|
+
* {@link IMQMetricsServerOptions.enabled} was set, close
|
|
195
|
+
* `service.metricsServer` yourself, otherwise the open listener keeps the
|
|
196
|
+
* process alive. In-flight requests are not awaited.
|
|
86
197
|
*/
|
|
87
198
|
destroy(): Promise<void>;
|
|
88
199
|
/**
|
|
89
|
-
* Returns service description metadata
|
|
200
|
+
* Returns this service's description metadata — the exposed method list plus the
|
|
201
|
+
* registered complex types — synchronously.
|
|
202
|
+
*
|
|
203
|
+
* @returns a plain object matching the {@link Description} shape, not a class
|
|
204
|
+
* instance
|
|
205
|
+
*
|
|
206
|
+
* @remarks
|
|
207
|
+
* This method is itself exposed, so a remote caller invoking it through a
|
|
208
|
+
* generated client receives a `Promise<Description>` — but called locally it is
|
|
209
|
+
* not a promise and must not be awaited as one.
|
|
90
210
|
*
|
|
91
|
-
*
|
|
211
|
+
* The result is built once per service name and cached process-wide for the
|
|
212
|
+
* lifetime of the process, so methods attached after the first call are not
|
|
213
|
+
* reflected. The cache key is the service `name` while the method list is
|
|
214
|
+
* resolved from the concrete class name, so constructing two differently-classed
|
|
215
|
+
* services under the same `name` in one process makes them share a single
|
|
216
|
+
* description.
|
|
92
217
|
*/
|
|
93
218
|
describe(): Description;
|
|
94
219
|
private startWithMetricsServer;
|
|
@@ -96,9 +221,9 @@ export declare abstract class IMQService {
|
|
|
96
221
|
/**
|
|
97
222
|
* Sends IMQ response with support of after call optional hook
|
|
98
223
|
*
|
|
99
|
-
* @param
|
|
100
|
-
* @param
|
|
101
|
-
* @param
|
|
102
|
-
* @
|
|
224
|
+
* @param request - from message identifier
|
|
225
|
+
* @param response - response to send
|
|
226
|
+
* @param service - imq service to bind
|
|
227
|
+
* @returns send result message identifier
|
|
103
228
|
*/
|
|
104
229
|
export declare function send(request: IMQRPCRequest, response: IMQRPCResponse, service: IMQService): Promise<string>;
|
package/src/IMQService.js
CHANGED
|
@@ -64,8 +64,31 @@ import {} from './IMQRPCDescription.js';
|
|
|
64
64
|
import {} from './IMQRPCOptions.js';
|
|
65
65
|
import { runWithRequest } from './IMQRequestContext.js';
|
|
66
66
|
import { createServer } from 'node:http';
|
|
67
|
+
/**
|
|
68
|
+
* The self-description a service serves to its clients, and the input to client
|
|
69
|
+
* generation.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* Declared as a class for historical reasons but never instantiated: a service
|
|
73
|
+
* returns a plain object literal, and a client receives its JSON round-trip. So do
|
|
74
|
+
* not use `new Description()` or `instanceof Description`.
|
|
75
|
+
*
|
|
76
|
+
* A service memoizes its description after the first request for it.
|
|
77
|
+
*/
|
|
67
78
|
export class Description {
|
|
79
|
+
/**
|
|
80
|
+
* The service itself: its name, and every exposed method with the inheritance
|
|
81
|
+
* chain already flattened.
|
|
82
|
+
*/
|
|
68
83
|
service;
|
|
84
|
+
/**
|
|
85
|
+
* Every complex type registered in the service's process.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* This is the process-global type registry, embedded by reference and
|
|
89
|
+
* unfiltered — so it includes types this service does not use, and each becomes
|
|
90
|
+
* a generated interface in every client.
|
|
91
|
+
*/
|
|
69
92
|
types;
|
|
70
93
|
}
|
|
71
94
|
const serviceDescriptions = new Map();
|
|
@@ -73,25 +96,33 @@ const serviceDescriptions = new Map();
|
|
|
73
96
|
* Returns collection of class methods metadata even those are inherited
|
|
74
97
|
* from a chain of parent classes
|
|
75
98
|
*
|
|
76
|
-
* @param
|
|
77
|
-
* @return {MethodsCollectionDescription}
|
|
99
|
+
* @param ctor - constructor of the class to collect exposed methods for
|
|
78
100
|
*/
|
|
79
|
-
function getClassMethods(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
101
|
+
function getClassMethods(ctor) {
|
|
102
|
+
const methods = {};
|
|
103
|
+
// Resolve the chain from the runtime prototype chain rather than from the
|
|
104
|
+
// registry's `inherits` links. @expose() only registers the class that
|
|
105
|
+
// *declares* a method, so a class exposing nothing of its own has no
|
|
106
|
+
// registry entry at all — walking `inherits` would either dereference
|
|
107
|
+
// undefined or stop dead at that gap and lose the ancestors' methods.
|
|
108
|
+
const chain = [];
|
|
109
|
+
for (let current = ctor; typeof current === 'function'; current = Object.getPrototypeOf(current)) {
|
|
110
|
+
chain.unshift(current);
|
|
111
|
+
}
|
|
112
|
+
// root-first, so a subclass method overrides a same-named parent one
|
|
113
|
+
for (const link of chain) {
|
|
114
|
+
const info = IMQRPCDescription.serviceDescription[link.name];
|
|
115
|
+
if (info) {
|
|
116
|
+
Object.assign(methods, info.methods);
|
|
117
|
+
}
|
|
85
118
|
}
|
|
86
|
-
Object.assign(methods, classInfo.methods);
|
|
87
119
|
return methods;
|
|
88
120
|
}
|
|
89
121
|
/**
|
|
90
122
|
* Checks if given args match the given args description at least by args count
|
|
91
123
|
*
|
|
92
|
-
* @param
|
|
93
|
-
* @param
|
|
94
|
-
* @returns {boolean}
|
|
124
|
+
* @param argsInfo -
|
|
125
|
+
* @param args -
|
|
95
126
|
*/
|
|
96
127
|
function isValidArgsCount(argsInfo, args) {
|
|
97
128
|
return argsInfo.some(argInfo => argInfo.isOptional)
|
|
@@ -121,18 +152,65 @@ let IMQService = (() => {
|
|
|
121
152
|
__esDecorate(this, null, _describe_decorators, { kind: "method", name: "describe", static: false, private: false, access: { has: obj => "describe" in obj, get: obj => obj.describe }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
122
153
|
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
123
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* The underlying message queue this service consumes requests from.
|
|
157
|
+
*/
|
|
124
158
|
imq = __runInitializers(this, _instanceExtraInitializers);
|
|
159
|
+
/**
|
|
160
|
+
* The effective logger — `options.logger`, defaulting to `console`.
|
|
161
|
+
*/
|
|
125
162
|
logger;
|
|
163
|
+
/**
|
|
164
|
+
* The cache adapter used by cached methods.
|
|
165
|
+
*
|
|
166
|
+
* @remarks
|
|
167
|
+
* Never assigned by the service itself: it is populated lazily by the
|
|
168
|
+
* {@link cache} decorator on first use, so it is `undefined` on a service that
|
|
169
|
+
* has no cached methods.
|
|
170
|
+
*/
|
|
126
171
|
cache;
|
|
172
|
+
/**
|
|
173
|
+
* The metrics HTTP server, present only while
|
|
174
|
+
* {@link IMQMetricsServerOptions.enabled} is set.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* {@link IMQService.destroy} does not close it — do that yourself, or the
|
|
178
|
+
* open listener keeps the process alive.
|
|
179
|
+
*/
|
|
127
180
|
metricsServer;
|
|
128
181
|
signalHandlers = [];
|
|
182
|
+
/**
|
|
183
|
+
* This service's name, which is also its queue name and the key its description
|
|
184
|
+
* is cached under.
|
|
185
|
+
*/
|
|
129
186
|
name;
|
|
187
|
+
/**
|
|
188
|
+
* The effective options for this service: {@link DEFAULT_IMQ_SERVICE_OPTIONS}
|
|
189
|
+
* merged with the values passed to the constructor.
|
|
190
|
+
*/
|
|
130
191
|
options;
|
|
131
192
|
/**
|
|
132
|
-
*
|
|
193
|
+
* Constructs a service.
|
|
194
|
+
*
|
|
195
|
+
* @param options - service options, shallow-merged over
|
|
196
|
+
* {@link DEFAULT_IMQ_SERVICE_OPTIONS}, except `metricsServer` which is
|
|
197
|
+
* merged separately over
|
|
198
|
+
* {@link DEFAULT_IMQ_METRICS_SERVER_OPTIONS}
|
|
199
|
+
* @param name - overrides the class name as the service/queue name, and as the
|
|
200
|
+
* description cache key
|
|
201
|
+
* @throws TypeError when {@link IMQService} is instantiated directly. Note the
|
|
202
|
+
* check is by resolved name, so passing `'IMQService'` as the `name` of a
|
|
203
|
+
* subclass throws as well.
|
|
133
204
|
*
|
|
134
|
-
* @
|
|
135
|
-
*
|
|
205
|
+
* @remarks
|
|
206
|
+
* Constructing a service installs process signal handlers for `SIGTERM`,
|
|
207
|
+
* `SIGINT`, `SIGHUP` and `SIGQUIT`. On any of them the service tears the queue
|
|
208
|
+
* down, closes the metrics server, and then force-exits with code 0 after
|
|
209
|
+
* `IMQ_SHUTDOWN_TIMEOUT` — a fixed timer, not a drain: requests still being
|
|
210
|
+
* processed are not awaited.
|
|
211
|
+
*
|
|
212
|
+
* The merged options are passed straight to the queue factory, so `vendor` and
|
|
213
|
+
* `cluster`/`clusterManagers` select the transport implementation.
|
|
136
214
|
*/
|
|
137
215
|
constructor(options, name) {
|
|
138
216
|
this.name = name || this.constructor.name;
|
|
@@ -171,9 +249,8 @@ let IMQService = (() => {
|
|
|
171
249
|
/**
|
|
172
250
|
* Handles incoming request and produces corresponding response
|
|
173
251
|
*
|
|
174
|
-
* @param
|
|
175
|
-
* @param
|
|
176
|
-
* @return {Promise<string>}
|
|
252
|
+
* @param request - request message
|
|
253
|
+
* @param id - message unique identifier
|
|
177
254
|
*/
|
|
178
255
|
async handleRequest(request, id) {
|
|
179
256
|
return runWithRequest(request, () => this.processRequest(request, id));
|
|
@@ -246,7 +323,19 @@ let IMQService = (() => {
|
|
|
246
323
|
* Initializes this instance of service and starts handling request
|
|
247
324
|
* messages.
|
|
248
325
|
*
|
|
249
|
-
* @
|
|
326
|
+
* @returns the started message queue instance
|
|
327
|
+
*
|
|
328
|
+
* @remarks
|
|
329
|
+
* In single-process mode this starts the queue in the current process. With
|
|
330
|
+
* {@link IMQServiceOptions.multiProcess} the cluster primary forks
|
|
331
|
+
* `cpus().length * childrenPerCore` workers, each receiving its index in the
|
|
332
|
+
* `workerId` environment variable, and installs an exit watcher that terminates
|
|
333
|
+
* the process with code 1 as soon as one worker dies — workers are never
|
|
334
|
+
* respawned, so supervision is left to the process manager.
|
|
335
|
+
*
|
|
336
|
+
* Note that the primary also starts its own queue consumer and metrics listener
|
|
337
|
+
* after forking, so a service configured for N workers runs N+1 consumers and
|
|
338
|
+
* N+1 processes attempt to bind the metrics port.
|
|
250
339
|
*/
|
|
251
340
|
async start() {
|
|
252
341
|
if (!this.options.multiProcess) {
|
|
@@ -278,25 +367,40 @@ let IMQService = (() => {
|
|
|
278
367
|
return this.startWithMetricsServer();
|
|
279
368
|
}
|
|
280
369
|
/**
|
|
281
|
-
*
|
|
370
|
+
* Broadcasts data on this service's pub/sub channel, delivering it to every
|
|
371
|
+
* client currently subscribed to this service.
|
|
282
372
|
*
|
|
283
|
-
* @param
|
|
373
|
+
* @param data - JSON-serializable payload to broadcast
|
|
374
|
+
* @throws TypeError when the service has not been started, so the queue's writer
|
|
375
|
+
* is not connected
|
|
376
|
+
*
|
|
377
|
+
* @remarks
|
|
378
|
+
* Fire-and-forget: plain pub/sub with no persistence, ordering guarantee or
|
|
379
|
+
* delivery confirmation, so subscribers that are offline lose the message.
|
|
284
380
|
*/
|
|
285
381
|
async publish(data) {
|
|
286
382
|
await this.imq.publish(data);
|
|
287
383
|
}
|
|
288
384
|
/**
|
|
289
|
-
* Stops
|
|
385
|
+
* Stops consuming messages from the queue.
|
|
290
386
|
*
|
|
291
|
-
* @
|
|
387
|
+
* @remarks
|
|
388
|
+
* Connections, signal handlers and the metrics server are all left in place —
|
|
389
|
+
* use {@link IMQService.destroy} for full teardown. Calling this first is not a
|
|
390
|
+
* prerequisite for `destroy()`.
|
|
292
391
|
*/
|
|
293
392
|
async stop() {
|
|
294
393
|
await this.imq.stop();
|
|
295
394
|
}
|
|
296
395
|
/**
|
|
297
|
-
*
|
|
396
|
+
* Removes this instance's signal handlers, then unsubscribes and destroys the
|
|
397
|
+
* underlying queue.
|
|
298
398
|
*
|
|
299
|
-
* @
|
|
399
|
+
* @remarks
|
|
400
|
+
* It does not close the metrics server — if
|
|
401
|
+
* {@link IMQMetricsServerOptions.enabled} was set, close
|
|
402
|
+
* `service.metricsServer` yourself, otherwise the open listener keeps the
|
|
403
|
+
* process alive. In-flight requests are not awaited.
|
|
300
404
|
*/
|
|
301
405
|
async destroy() {
|
|
302
406
|
// unregister this instance's process signal handlers so destroyed
|
|
@@ -309,9 +413,23 @@ let IMQService = (() => {
|
|
|
309
413
|
await this.imq.destroy();
|
|
310
414
|
}
|
|
311
415
|
/**
|
|
312
|
-
* Returns service description metadata
|
|
416
|
+
* Returns this service's description metadata — the exposed method list plus the
|
|
417
|
+
* registered complex types — synchronously.
|
|
418
|
+
*
|
|
419
|
+
* @returns a plain object matching the {@link Description} shape, not a class
|
|
420
|
+
* instance
|
|
421
|
+
*
|
|
422
|
+
* @remarks
|
|
423
|
+
* This method is itself exposed, so a remote caller invoking it through a
|
|
424
|
+
* generated client receives a `Promise<Description>` — but called locally it is
|
|
425
|
+
* not a promise and must not be awaited as one.
|
|
313
426
|
*
|
|
314
|
-
*
|
|
427
|
+
* The result is built once per service name and cached process-wide for the
|
|
428
|
+
* lifetime of the process, so methods attached after the first call are not
|
|
429
|
+
* reflected. The cache key is the service `name` while the method list is
|
|
430
|
+
* resolved from the concrete class name, so constructing two differently-classed
|
|
431
|
+
* services under the same `name` in one process makes them share a single
|
|
432
|
+
* description.
|
|
315
433
|
*/
|
|
316
434
|
describe() {
|
|
317
435
|
let description = serviceDescriptions.get(this.name) || null;
|
|
@@ -319,7 +437,7 @@ let IMQService = (() => {
|
|
|
319
437
|
description = {
|
|
320
438
|
service: {
|
|
321
439
|
name: this.name,
|
|
322
|
-
methods: getClassMethods(this.constructor
|
|
440
|
+
methods: getClassMethods(this.constructor),
|
|
323
441
|
},
|
|
324
442
|
types: IMQRPCDescription.typesDescription,
|
|
325
443
|
};
|
|
@@ -338,7 +456,7 @@ let IMQService = (() => {
|
|
|
338
456
|
if (req.url === '/metrics') {
|
|
339
457
|
const length = await this.imq.queueLength();
|
|
340
458
|
const content = metricServerOptions.queueLengthFormatter?.(length, 'queue_length') || String(length);
|
|
341
|
-
res.setHeader('Content-Type', 'plain
|
|
459
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
342
460
|
res.setHeader('Content-Length', Buffer.byteLength(content));
|
|
343
461
|
res.writeHead(200);
|
|
344
462
|
res.end(content);
|
|
@@ -358,10 +476,10 @@ export { IMQService };
|
|
|
358
476
|
/**
|
|
359
477
|
* Sends IMQ response with support of after call optional hook
|
|
360
478
|
*
|
|
361
|
-
* @param
|
|
362
|
-
* @param
|
|
363
|
-
* @param
|
|
364
|
-
* @
|
|
479
|
+
* @param request - from message identifier
|
|
480
|
+
* @param response - response to send
|
|
481
|
+
* @param service - imq service to bind
|
|
482
|
+
* @returns send result message identifier
|
|
365
483
|
*/
|
|
366
484
|
export async function send(request, response, service) {
|
|
367
485
|
const logger = service.options.logger || console;
|
package/src/cache/ICache.d.ts
CHANGED
|
@@ -22,9 +22,22 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
/**
|
|
25
|
-
* Constructor signature
|
|
25
|
+
* Constructor signature the registry uses to instantiate a cache adapter class.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* The registry always calls it with no arguments. The optional `name` argument
|
|
29
|
+
* is a legacy affordance that no built-in adapter accepts and that the registry
|
|
30
|
+
* never supplies, so implementations must be constructible with zero arguments and
|
|
31
|
+
* set their own {@link ICache.name}.
|
|
26
32
|
*/
|
|
27
33
|
export interface ICacheConstructor {
|
|
34
|
+
/**
|
|
35
|
+
* Constructs a cache adapter instance, ready to be initialized.
|
|
36
|
+
*
|
|
37
|
+
* @param name - legacy, and never supplied by the registry
|
|
38
|
+
* @returns an uninitialized adapter — {@link ICache.init} must be called
|
|
39
|
+
* before use
|
|
40
|
+
*/
|
|
28
41
|
new (name?: string): ICache;
|
|
29
42
|
}
|
|
30
43
|
/**
|
|
@@ -35,50 +48,83 @@ export interface ICache {
|
|
|
35
48
|
/**
|
|
36
49
|
* Adapter (cache) name.
|
|
37
50
|
*
|
|
38
|
-
* @
|
|
51
|
+
* @remarks
|
|
52
|
+
* The name is load-bearing twice over: it is the key the adapter is registered
|
|
53
|
+
* under, and it is a segment of every physical cache key
|
|
54
|
+
* (`<prefix>:<name>:<key>` in the Redis adapter). It must therefore be stable —
|
|
55
|
+
* changing it at runtime moves the namespace and orphans existing entries.
|
|
39
56
|
*/
|
|
40
57
|
name: string;
|
|
41
58
|
/**
|
|
42
59
|
* Whether the cache adapter is initialized and ready to use.
|
|
43
60
|
*
|
|
44
|
-
* @
|
|
61
|
+
* @remarks
|
|
62
|
+
* For the built-in Redis adapter this flag is never reset — destroying the
|
|
63
|
+
* shared connection leaves it `true`, so a `true` value does not guarantee a
|
|
64
|
+
* live connection after an explicit destroy.
|
|
45
65
|
*/
|
|
46
66
|
ready: boolean;
|
|
47
67
|
/**
|
|
48
68
|
* Initializes the cache adapter with the given adapter-specific options.
|
|
49
69
|
*
|
|
50
|
-
* @param
|
|
51
|
-
*
|
|
70
|
+
* @param options - adapter-specific options
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* May be asynchronous: the registry awaits whatever is returned, so an
|
|
74
|
+
* implementation that needs I/O must return a promise that settles once the
|
|
75
|
+
* adapter is ready. Implementations must set {@link ICache.ready} on success.
|
|
52
76
|
*/
|
|
53
77
|
init(options?: any): void;
|
|
54
78
|
/**
|
|
55
79
|
* Returns the value stored in the cache under the given key.
|
|
56
80
|
*
|
|
57
|
-
* @param
|
|
58
|
-
* @returns
|
|
81
|
+
* @param key - key to read the value for
|
|
82
|
+
* @returns the deserialized stored value, or `undefined` when the key is absent
|
|
83
|
+
* or expired
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* A stored `null` is returned as `null` and is therefore distinguishable from a
|
|
87
|
+
* miss; a stored `undefined` is not. Callers that must tell "cached as
|
|
88
|
+
* empty" from "not cached" should store an explicit sentinel rather than
|
|
89
|
+
* `undefined`.
|
|
59
90
|
*/
|
|
60
91
|
get(key: string): Promise<any>;
|
|
61
92
|
/**
|
|
62
93
|
* Stores the given value in the cache under the given key.
|
|
63
94
|
*
|
|
64
|
-
* @param
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @returns
|
|
95
|
+
* @param key - key to store the value under
|
|
96
|
+
* @param value - value to store
|
|
97
|
+
* @param ttl - time-to-live in milliseconds
|
|
98
|
+
* @returns a truthy value when the write happened, falsy when it did not
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Test the result for truthiness rather than comparing against `true` — the
|
|
102
|
+
* built-in Redis adapter resolves to the string `'OK'` or to `null`, never to a
|
|
103
|
+
* boolean.
|
|
104
|
+
*
|
|
105
|
+
* The framework additionally passes a fourth argument, an `nx` flag meaning
|
|
106
|
+
* "only create the key if it does not already exist". Adapters that cannot
|
|
107
|
+
* honour it should say so, because the {@link cache} decorator always passes it.
|
|
68
108
|
*/
|
|
69
109
|
set(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
70
110
|
/**
|
|
71
111
|
* Removes the value stored in the cache under the given key.
|
|
72
112
|
*
|
|
73
|
-
* @param
|
|
74
|
-
* @returns
|
|
113
|
+
* @param key - key to remove
|
|
114
|
+
* @returns true if a key was actually removed, false if it did not exist —
|
|
115
|
+
* which is not an error
|
|
75
116
|
*/
|
|
76
117
|
del(key: string): Promise<boolean>;
|
|
77
118
|
/**
|
|
78
|
-
*
|
|
119
|
+
* Deletes every key matching the given wildcard mask.
|
|
120
|
+
*
|
|
121
|
+
* @param keyMask - wildcard mask matched against fully qualified key names
|
|
122
|
+
* @returns whether the purge ran
|
|
79
123
|
*
|
|
80
|
-
* @
|
|
81
|
-
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* The mask is not automatically scoped to this cache — callers must include
|
|
126
|
+
* the namespace themselves (`<prefix>:<name>:...`). A broad mask such as `'*'`
|
|
127
|
+
* will delete unrelated data, including message-queue keys.
|
|
82
128
|
*/
|
|
83
129
|
purge(keyMask: string): Promise<boolean>;
|
|
84
130
|
}
|