@mdf.js/service-registry
Version:
MMS - API - Service Registry
81 lines • 3.44 kB
JavaScript
;
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkerPort = void 0;
const types_1 = require("../types");
const Port_1 = require("./Port");
/**
* WorkerPort class is designed to manage error reporting in a worker process, specifically
* within a Node.js cluster. It listens for messages from the master process to either send
* back the current error registry or to clear its error records upon request.
*
* Inherits from the Port class, leveraging its logging functionality to provide insight
* into the inter-process communication and error management actions taking place within
* the worker process.
*/
class WorkerPort extends Port_1.Port {
/**
* Create an instance of errors manager in a worker process
* @param aggregator - Aggregator instance to manage the errors
* @param logger - Logger instance for logging activities
*/
constructor(aggregator, logger) {
super(logger);
this.aggregator = aggregator;
/**
* Handles requests from the master process, responding with the current error registry or
* clearing errors.
* @param message - The message received from the master process, containing the request type and
* optionally a request ID.
*/
this.onMasterRequestHandler = (message) => {
if (message.type === types_1.RegisterMessageType.REQ && process.send) {
// Stryker disable next-line all
this.logger.debug(`New update request received with requestId [${message.requestId}]`);
process.send({
type: types_1.RegisterMessageType.RES,
requestId: message.requestId,
errors: this.aggregator.errors,
});
}
else if (message.type === types_1.RegisterMessageType.CLR_REQ) {
// Stryker disable next-line all
this.logger.debug(`New clear request received on worker [${process.pid}] from master`);
this.clear();
}
};
// Stryker disable next-line all
this.logger.debug(`New worker port instance created`);
}
/** Clear all the actual error in the registry */
clear() {
// Stryker disable next-line all
this.logger.debug('Clearing errors registry in worker process');
this.aggregator.clear();
}
/**
* Starts listening for messages from the master process, enabling response to error registry
* requests and clear commands.
*/
start() {
// Stryker disable next-line all
this.logger.debug('Starting error registry request listener in worker process');
process.on('message', this.onMasterRequestHandler);
}
/**
* Stops listening for messages from the master process, effectively disabling further
* communication for error registry management.
*/
stop() {
// Stryker disable next-line all
this.logger.debug('Stopping error registry request listener in worker process');
process.off('message', this.onMasterRequestHandler);
}
}
exports.WorkerPort = WorkerPort;
//# sourceMappingURL=WorkerPort.js.map