historical-deadletter-processor
Version:
historical-deadletter-processor
110 lines (95 loc) • 3.89 kB
JavaScript
(function() {
var HighlandPagination, HistoricalDeadletterProcessor, Promise, Query, _, createClient, highland, moment, parseAccountString, ref,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ = require("lodash");
Promise = require("bluebird");
highland = require("highland");
ref = require("azure-table-node"), parseAccountString = ref.parseAccountString, createClient = ref.createClient, Query = ref.Query;
HighlandPagination = require("highland-pagination");
moment = require("moment");
module.exports = HistoricalDeadletterProcessor = (function() {
function HistoricalDeadletterProcessor(processor, arg, concurrency, logger, daysRetrying) {
var connection;
this.processor = processor;
connection = arg.connection, this.tableName = arg.tableName, this.partitionKey = arg.partitionKey;
this.concurrency = concurrency != null ? concurrency : {
callsToApi: 20,
callsToAzure: 50
};
this.logger = logger != null ? logger : console;
this.daysRetrying = daysRetrying != null ? daysRetrying : 1;
this._remove = bind(this._remove, this);
this._doProcess = bind(this._doProcess, this);
this._retrieveMessages = bind(this._retrieveMessages, this);
this.run = bind(this.run, this);
if (_.isString(connection)) {
connection = parseAccountString(connection);
}
this.client = Promise.promisifyAll(createClient(connection), {
multiArgs: true
});
}
HistoricalDeadletterProcessor.prototype.run = function() {
return new HighlandPagination(this._retrieveMessages).stream().map(function(row) {
return _.update(row, "notification", JSON.parse);
}).map((function(_this) {
return function(row) {
var RowKey;
RowKey = row.RowKey;
return _this._doProcess(row).tap(function() {
return _this.logger.info("Process successful " + RowKey);
}).map(function() {
return row;
}).errors(function() {
return _this.logger.warn("Still fails " + RowKey);
});
};
})(this)).parallel(this.concurrency.callsToApi).map((function(_this) {
return function(row) {
return _this._remove(row);
};
})(this)).parallel(this.concurrency.callsToAzure).collect().toPromise(Promise);
};
HistoricalDeadletterProcessor.prototype._retrieveMessages = function(continuation) {
var query;
query = Query.create().where("PartitionKey", "==", "" + this.partitionKey).and("Timestamp", ">", moment().subtract(this.daysRetrying, 'days').toDate());
return this.client.queryEntitiesAsync(this.tableName, {
query: query,
limitTo: 20,
continuation: continuation
}).spread((function(_this) {
return function(items, nextToken) {
return {
items: items,
nextToken: nextToken
};
};
})(this));
};
HistoricalDeadletterProcessor.prototype._doProcess = function(row) {
return highland((function(_this) {
return function(push, next) {
var __done;
__done = function(err) {
push(err, null);
return push(null, highland.nil);
};
return _this.processor({
done: __done,
log: _this.logger
}, row);
};
})(this));
};
HistoricalDeadletterProcessor.prototype._remove = function(arg) {
var PartitionKey, RowKey, __etag;
PartitionKey = arg.PartitionKey, RowKey = arg.RowKey, __etag = arg.__etag;
return highland(this.client.deleteEntityAsync(this.tableName, {
PartitionKey: PartitionKey,
RowKey: RowKey,
__etag: __etag
}));
};
return HistoricalDeadletterProcessor;
})();
}).call(this);