@imqueue/rpc 2.0.15 → 2.0.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "2.0.15",
3
+ "version": "2.0.18",
4
4
  "description": "RPC-like client-service implementation over messaging queue",
5
5
  "keywords": [
6
6
  "redis",
@@ -37,7 +37,7 @@
37
37
  "author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
38
38
  "license": "GPL-3.0-only",
39
39
  "dependencies": {
40
- "@imqueue/core": "^2.0.11",
40
+ "@imqueue/core": "^2.0.14",
41
41
  "@types/node": "^24.3.0",
42
42
  "acorn": "^8.15.0",
43
43
  "farmhash": "^4.0.2",
package/src/IMQClient.js CHANGED
@@ -157,7 +157,7 @@ class IMQClient extends events_1.EventEmitter {
157
157
  * @return {Promise<void>}
158
158
  */
159
159
  async subscribe(handler) {
160
- return this.subscriptionImq.subscribe(this.name, handler);
160
+ return this.subscriptionImq.subscribe(this.serviceName, handler);
161
161
  }
162
162
  // noinspection JSUnusedGlobalSymbols
163
163
  /**
@@ -32,9 +32,15 @@ export interface IMQBeforeCall<T> {
32
32
  export interface IMQAfterCall<T> {
33
33
  (req: IMQRPCRequest, res?: IMQRPCResponse): Promise<void>;
34
34
  }
35
+ export interface IMQMetricsServerOptions {
36
+ enabled?: boolean;
37
+ port?: number;
38
+ queueLengthFormatter?: (length: number, metricName: string) => string;
39
+ }
35
40
  export interface IMQServiceOptions extends IMQOptions {
36
41
  multiProcess: boolean;
37
42
  childrenPerCore: number;
43
+ metricsServer?: IMQMetricsServerOptions;
38
44
  beforeCall?: IMQBeforeCall<IMQService>;
39
45
  afterCall?: IMQAfterCall<IMQService>;
40
46
  }
@@ -53,6 +59,12 @@ export interface IMQClientOptions extends IMQOptions {
53
59
  * @type {IMQServiceOptions}
54
60
  */
55
61
  export declare const DEFAULT_IMQ_SERVICE_OPTIONS: IMQServiceOptions;
62
+ /**
63
+ * Default metric server options
64
+ *
65
+ * @type {NonNullable<IMQMetricsServerOptions>}
66
+ */
67
+ export declare const DEFAULT_IMQ_METRICS_SERVER_OPTIONS: NonNullable<IMQMetricsServerOptions>;
56
68
  /**
57
69
  * Default client options
58
70
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_IMQ_CLIENT_OPTIONS = exports.DEFAULT_IMQ_SERVICE_OPTIONS = void 0;
3
+ exports.DEFAULT_IMQ_CLIENT_OPTIONS = exports.DEFAULT_IMQ_METRICS_SERVER_OPTIONS = exports.DEFAULT_IMQ_SERVICE_OPTIONS = void 0;
4
4
  /*!
5
5
  * IMQ-RPC Interfaces: IMQServiceOptions
6
6
  *
@@ -31,6 +31,16 @@ const core_1 = require("@imqueue/core");
31
31
  * @type {IMQServiceOptions}
32
32
  */
33
33
  exports.DEFAULT_IMQ_SERVICE_OPTIONS = Object.assign(Object.assign({}, core_1.DEFAULT_IMQ_OPTIONS), { cleanup: true, cleanupFilter: '*:client', multiProcess: false, childrenPerCore: 1 });
34
+ /**
35
+ * Default metric server options
36
+ *
37
+ * @type {NonNullable<IMQMetricsServerOptions>}
38
+ */
39
+ exports.DEFAULT_IMQ_METRICS_SERVER_OPTIONS = {
40
+ enabled: false,
41
+ port: 9090,
42
+ queueLengthFormatter: (length, metricName) => `${metricName} {} ${length}`,
43
+ };
34
44
  /**
35
45
  * Default client options
36
46
  *
@@ -23,6 +23,7 @@
23
23
  */
24
24
  import { JsonObject, ILogger, IMessageQueue } from '@imqueue/core';
25
25
  import { TypesDescription, IMQRPCRequest, IMQRPCResponse, IMQServiceOptions, ICache, MethodsCollectionDescription } from '.';
26
+ import * as http from 'node:http';
26
27
  export declare class Description {
27
28
  service: {
28
29
  name: string;
@@ -39,6 +40,7 @@ export declare abstract class IMQService {
39
40
  protected imq: IMessageQueue;
40
41
  protected logger: ILogger;
41
42
  protected cache: ICache;
43
+ protected metricsServer?: http.Server<any, any>;
42
44
  name: string;
43
45
  options: IMQServiceOptions;
44
46
  /**
@@ -89,6 +91,7 @@ export declare abstract class IMQService {
89
91
  * @returns {Promise<Description>}
90
92
  */
91
93
  describe(): Description;
94
+ private startWithMetricsServer;
92
95
  }
93
96
  /**
94
97
  * Sends IMQ response with support of after call optional hook
package/src/IMQService.js CHANGED
@@ -37,6 +37,7 @@ exports.send = send;
37
37
  const core_1 = require("@imqueue/core");
38
38
  const _1 = require(".");
39
39
  const os = require("os");
40
+ const http = require("node:http");
40
41
  const cluster = require('cluster');
41
42
  class Description {
42
43
  }
@@ -60,7 +61,7 @@ function getClassMethods(className) {
60
61
  return methods;
61
62
  }
62
63
  /**
63
- * Checks if given args match given args description at least by args count
64
+ * Checks if given args match the given args description at least by args count
64
65
  *
65
66
  * @param {ArgDescription[]} argsInfo
66
67
  * @param {any[]} args
@@ -91,12 +92,15 @@ class IMQService {
91
92
  throw new TypeError('IMQService class is abstract and cannot ' +
92
93
  'be instantiated directly!');
93
94
  }
94
- this.options = Object.assign(Object.assign({}, _1.DEFAULT_IMQ_SERVICE_OPTIONS), options);
95
+ this.options = Object.assign(Object.assign(Object.assign({}, _1.DEFAULT_IMQ_SERVICE_OPTIONS), options), { metricsServer: Object.assign(Object.assign({}, _1.DEFAULT_IMQ_METRICS_SERVER_OPTIONS), ((options === null || options === void 0 ? void 0 : options.metricsServer) || {})) });
95
96
  this.logger = this.options.logger || /* istanbul ignore next */ console;
96
97
  this.imq = core_1.default.create(this.name, this.options);
97
98
  this.handleRequest = this.handleRequest.bind(this);
98
99
  _1.SIGNALS.forEach((signal) => process.on(signal, async () => {
99
100
  this.destroy().catch(this.logger.error);
101
+ if (this.metricsServer) {
102
+ this.metricsServer.close();
103
+ }
100
104
  // istanbul ignore next
101
105
  setTimeout(() => process.exit(0), core_1.IMQ_SHUTDOWN_TIMEOUT);
102
106
  }));
@@ -176,7 +180,7 @@ class IMQService {
176
180
  if (!this.options.multiProcess) {
177
181
  this.logger.info('%s: starting single-worker, pid %s', this.name, process.pid);
178
182
  this.describe();
179
- return this.imq.start();
183
+ return this.startWithMetricsServer();
180
184
  }
181
185
  if (cluster.isMaster) {
182
186
  const numCpus = os.cpus().length;
@@ -194,8 +198,9 @@ class IMQService {
194
198
  else {
195
199
  this.logger.info('%s: worker #%s started, pid %s', this.name, process.env['workerId'], process.pid);
196
200
  this.describe();
197
- return this.imq.start();
201
+ return this.startWithMetricsServer();
198
202
  }
203
+ return this.startWithMetricsServer();
199
204
  }
200
205
  // noinspection JSUnusedGlobalSymbols
201
206
  /**
@@ -242,6 +247,32 @@ class IMQService {
242
247
  }
243
248
  return description;
244
249
  }
250
+ async startWithMetricsServer() {
251
+ const service = this.imq.start();
252
+ const metricServerOptions = this.options.metricsServer;
253
+ if (!(metricServerOptions && metricServerOptions.enabled)) {
254
+ return service;
255
+ }
256
+ this.logger.log('Starting metrics server...');
257
+ this.metricsServer = http.createServer(async (req, res) => {
258
+ var _a;
259
+ if (req.url === '/metrics') {
260
+ const length = await this.imq.queueLength();
261
+ const content = ((_a = metricServerOptions.queueLengthFormatter) === null || _a === void 0 ? void 0 : _a.call(metricServerOptions, length, 'queue_length')) || String(length);
262
+ res.setHeader('Content-Type', 'plain/text');
263
+ res.setHeader('Content-Length', Buffer.byteLength(content));
264
+ res.writeHead(200);
265
+ res.end(content);
266
+ return;
267
+ }
268
+ res.writeHead(404);
269
+ res.end();
270
+ });
271
+ this.metricsServer.listen(metricServerOptions.port, '0.0.0.0', () => {
272
+ this.logger.info('%s: metrics server started on port %s', this.name, metricServerOptions.port);
273
+ });
274
+ return service;
275
+ }
245
276
  }
246
277
  exports.IMQService = IMQService;
247
278
  __decorate([