trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
74 lines • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Feed = void 0;
const lodash_1 = require("lodash");
const _logger_1 = require("../logger");
const _utilities_1 = require("../utilities");
const mq_feed_1 = require("./mq-feed");
const validators_1 = require("./validators");
/**
* Class that represents all Feed requests
*/
class Feed {
/**
* Creates a new Feed instance.
* @param mqSettings - MQ connection settings (hostname, port, vhost, username, password, packageId, etc.)
* @param logger - Logger instance (optional, defaults to ConsoleAdapter)
* @remarks The Feed constructor only accepts 2 parameters: mqSettings and logger.
* The preConnectionAtStart parameter is passed to the start() method, not the constructor.
*/
constructor(mqSettings, logger = new _logger_1.ConsoleAdapter()) {
this.logger = logger;
this.preConnectionAtStart = false;
this.mqSettings = validators_1.MqConnectionSettingsValidator.validate(mqSettings);
this.consumerMq = new mq_feed_1.MessageConsumerMQ(this.mqSettings, this.logger);
}
setLogger(logger) {
this.logger = logger;
}
async start(preConnectionAtStart = false) {
this.preConnectionAtStart = preConnectionAtStart;
if (this.preConnectionAtStart)
await this.preConnectionInitialization();
await this.consumerMq.start();
}
/**
* Pre connection initialization for the feed service
* to check the distribution status and start the
* distribution flow if it is off.
* @returns void
*/
async preConnectionInitialization() {
const options = { maxAttempts: 5, delayMs: 2000, backoffFactor: 2 };
new _utilities_1.DistributionUtil(this.mqSettings, this.logger);
const distributionStatus = await _utilities_1.DistributionUtil.checkStatus();
if (!(0, lodash_1.isNil)(distributionStatus)) {
const { isDistributionOn } = distributionStatus;
if (!isDistributionOn) {
this.logger.info('Distribution flow is off, will trying to start the flow');
return (0, _utilities_1.withRetry)(async () => {
await _utilities_1.DistributionUtil.start();
const distributionStatusAfterStartOperation = await _utilities_1.DistributionUtil.checkStatus();
if (!(0, lodash_1.isNil)(distributionStatusAfterStartOperation) &&
distributionStatusAfterStartOperation.isDistributionOn) {
this.logger.info('Distribution is activated successfully');
return;
}
}, options, 'Start distribution', this.logger);
}
else if (isDistributionOn) {
this.logger.info('Distribution flow is already on');
}
}
}
async stop() {
await this.consumerMq.stop();
if (this.preConnectionAtStart)
await _utilities_1.DistributionUtil.stop();
}
async addEntityHandler(entityHandler, entityConstructor) {
await this.consumerMq.addEntityHandler(entityHandler, entityConstructor);
}
}
exports.Feed = Feed;
//# sourceMappingURL=feed.js.map