historical-deadletter-processor
Version:
historical-deadletter-processor
133 lines (114 loc) • 6.47 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// Generated by CoffeeScript 2.7.0
(function () {
var AbstractReaderProcessor,
RetryHistoricalProcessor,
_,
highland,
moment,
boundMethodCheck = function boundMethodCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new Error('Bound instance method accessed before binding');
}
};
_ = require("lodash");
highland = require("highland");
moment = require("moment");
AbstractReaderProcessor = require("./abstract.reader.processor");
module.exports = RetryHistoricalProcessor = function (_AbstractReaderProces) {
_inherits(RetryHistoricalProcessor, _AbstractReaderProces);
function RetryHistoricalProcessor(opts) {
_classCallCheck(this, RetryHistoricalProcessor);
var _this = _possibleConstructorReturn(this, (RetryHistoricalProcessor.__proto__ || Object.getPrototypeOf(RetryHistoricalProcessor)).call(this, opts));
_this._filter_ = _this._filter_.bind(_this);
_this._queryOptions_ = _this._queryOptions_.bind(_this);
_this._doProcess = _this._doProcess.bind(_this);
_this.processor = opts.processor;
_this.app = opts.app;
_this.job = opts.job;
var _opts$daysRetrying = opts.daysRetrying;
_this.daysRetrying = _opts$daysRetrying === undefined ? 1 : _opts$daysRetrying;
var _opts$concurrency = opts.concurrency;
_this.concurrency = _opts$concurrency === undefined ? {
callsToApi: 20
} : _opts$concurrency;
var _opts$conditions = opts.conditions;
_this.conditions = _opts$conditions === undefined ? _this._buildConditions(opts) : _opts$conditions;
var _opts$select = opts.select;
_this.select = _opts$select === undefined ? "id,notification,resource,job" : _opts$select;
return _this;
}
_createClass(RetryHistoricalProcessor, [{
key: "_action_",
value: function _action_(stream) {
var _this2 = this;
return stream.map(function (row) {
return _.update(row, "notification", JSON.parse);
}).concurrentFlatMap(this.concurrency.callsToApi, function (row) {
return _this2._doProcess(row).errors(function () {
return _this2.debug("Still fails " + row.resource + " in " + _this2.app + "/" + _this2.job);
});
}).tap(function (row) {
return _this2.debug("Process successful " + row.resource + " in " + _this2.app + "/" + _this2.job);
});
}
}, {
key: "_filter_",
value: function _filter_() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
boundMethodCheck(this, RetryHistoricalProcessor);
return this.conditions.map(function (condition) {
return "(" + condition + ")";
}).join(" and ");
}
}, {
key: "_buildConditions",
value: function _buildConditions(opts) {
var conditions, defaultConditions, extraConditions, nDaysAgo;
conditions = _.get(opts, 'conditions');
if (_.get(opts, 'conditions')) {
return conditions;
}
extraConditions = _.get(opts, 'extraConditions');
nDaysAgo = moment().subtract(this.daysRetrying, 'days').utc().format("YYYY-MM-DDTHH:mm:ss") + "z";
defaultConditions = ["app eq '" + this.app + "'", "job eq '" + this.job + "'", "timestamp gt " + nDaysAgo];
if (_.isNil(extraConditions)) {
return defaultConditions;
}
return _.concat(defaultConditions, extraConditions);
}
}, {
key: "_queryOptions_",
value: function _queryOptions_() {
boundMethodCheck(this, RetryHistoricalProcessor);
return _.merge(_get(RetryHistoricalProcessor.prototype.__proto__ || Object.getPrototypeOf(RetryHistoricalProcessor.prototype), "_queryOptions_", this).call(this), {
select: this.select
});
}
}, {
key: "_doProcess",
value: function _doProcess(row) {
var _this3 = this;
boundMethodCheck(this, RetryHistoricalProcessor);
return highland(function (push, next) {
var __done;
__done = function __done(err) {
push(err, row);
return push(null, highland.nil);
};
return _this3.processor({
done: __done,
log: _this3.logger,
job: row.job
}, row);
});
}
}]);
return RetryHistoricalProcessor;
}(AbstractReaderProcessor);
}).call(undefined);