UNPKG

tdl-client-nodejs

Version:

A NodeJs client that allows users to get up to speed with the TDL system.

88 lines (76 loc) 2.64 kB
"use strict"; var AuditStream = require("../audit/audit_stream"); var RemoteBroker = require("./transport/remote_broker"); var FatalErrorResponse = require("./abstractions/response/fatal_error_response"); function QueueBasedImplementationRunner(config, deployProcessingRules) { this._config = config; this._deployProcessingRules = deployProcessingRules; } QueueBasedImplementationRunner.prototype.getRequestTimeoutMillisecond = function () { return this._config.getTimeToWaitForRequest(); }; QueueBasedImplementationRunner.prototype.run = function () { var config = this._config; var processingRules = this._deployProcessingRules; var remoteBroker; return new RemoteBroker( config.getRequestQueueName(), config.getResponseQueueName() ) .connect( config.getHostName(), config.getPort() ) .then(function (rb) { remoteBroker = rb console.log("Starting client."); return remoteBroker.subscribeAndProcess( new ApplyProcessingRules(processingRules), config.getTimeToWaitForRequest() ); }) .catch(function (error) { console.error( "There was a problem processing messages. " + error.message ); console.error(error.stack); }) .finally(function () { if (remoteBroker) { console.log("Stopping client."); return remoteBroker.close(); } }); }; //~~~~ Queue handling policies function ApplyProcessingRules(processingRules) { this.processingRules = processingRules; this.audit = new AuditStream(); } ApplyProcessingRules.prototype.processNextRequestFrom = function ( remoteBroker, request ) { var audit = this.audit; audit.startLine(); audit.log_request(request); // Obtain response from user var response = this.processingRules.getResponseFor(request); audit.log_response(response); // Act return Promise.resolve(remoteBroker) .then(function (remoteBroker) { if (response instanceof FatalErrorResponse) { return Promise.resolve(remoteBroker); } return remoteBroker.respondTo(request, response); }) .then(function (remoteBroker) { audit.endLine(); if (response instanceof FatalErrorResponse) { return remoteBroker.close(); } return Promise.resolve(remoteBroker); }); }; module.exports = QueueBasedImplementationRunner;