historical-deadletter-processor
Version:
historical-deadletter-processor
108 lines (89 loc) • 3.95 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; }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Generated by CoffeeScript 2.7.0
(function () {
var AbstractReaderProcessor, HighlandPagination, Promise, Search, _, highland;
_ = require("lodash");
Promise = require("bluebird");
highland = require("highland");
Search = require("search-sdk");
HighlandPagination = require("highland-pagination");
require("highland-concurrent-flatmap");
module.exports = AbstractReaderProcessor = function () {
function AbstractReaderProcessor(_ref) {
var connection = _ref.connection,
_ref$logger = _ref.logger,
logger = _ref$logger === undefined ? console : _ref$logger,
_ref$sizePage = _ref.sizePage,
sizePage = _ref$sizePage === undefined ? 100 : _ref$sizePage;
_classCallCheck(this, AbstractReaderProcessor);
this.run = this.run.bind(this);
this._queryOptions_ = this._queryOptions_.bind(this);
this._remove = this._remove.bind(this);
this.logger = logger;
this.sizePage = sizePage;
this.search = this._buildClient(connection);
this.index = connection.index;
this.debug = require("debug")("historical-deadletter:" + this.constructor.name);
}
_createClass(AbstractReaderProcessor, [{
key: "run",
value: function run() {
var _this = this;
return this.search.reverseStream(this._queryOptions_(), this.sizePage).then(function (_ref2) {
var stream = _ref2.stream;
return stream.through(function (s) {
return _this._action_(s);
}).batch(20).concurrentFlatMap(10, function (rows) {
return _this._remove(rows).map(function () {
return rows;
});
}).reduce(0, function (accum, rows) {
return accum + rows.length;
}).toPromise(Promise).tap(function (i) {
return _this.debug("Done process. " + i + " processed successfully");
});
});
}
}, {
key: "_action_",
value: function _action_() {
throw new Error("_action_ subclass responsibility");
}
}, {
key: "_filter_",
value: function _filter_() {
throw new Error("_filter_ subclass responsibility");
}
}, {
key: "_queryOptions_",
value: function _queryOptions_() {
return {
filter: this._filter_()
};
}
}, {
key: "_buildClient",
value: function _buildClient(connection) {
return new Search(_.merge({
index: "incidents"
}, connection));
}
}, {
key: "_remove",
value: function _remove(rows) {
var _search;
var ids;
ids = _.map(rows, function (_ref3) {
var id = _ref3.id;
return { id: id };
});
this.debug("Removing documents " + _.map(ids, "id") + " in " + this.index);
return highland((_search = this.search).remove.apply(_search, _toConsumableArray(ids)));
}
}]);
return AbstractReaderProcessor;
}();
}).call(undefined);