@actonate/mirkwood
Version:
GraphQL based Rapid Server-side Development framework
167 lines (143 loc) • 5.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }(); //Plugin
var _bull = require('bull');
var _bull2 = _interopRequireDefault(_bull);
var _couchdb = require('../../documentStore/couchdb');
var _couchdb2 = _interopRequireDefault(_couchdb);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var queues = [];
var BullQueueAdapter = function () {
function BullQueueAdapter(connection) {
_classCallCheck(this, BullQueueAdapter);
this.client = connection;
this._initializeQueue();
}
_createClass(BullQueueAdapter, [{
key: '_initializeQueue',
value: function _initializeQueue() {
var queueName = this._getQueueName();
var limiter = _extends({}, this.client.limiter);
queues[queueName] = new _bull2.default(queueName); //LEGACY: {redis: this.client.connection, limiter}
var concurrency = this.client.concurrency || 1;
queues[queueName].process(queueName, concurrency, this.client.action).then(function (resp) {/* JOB COMPLETED */}).catch(function (err) {
/* Error */console.log("ERROR: ", err);
});
// Local events pass the job instance...
this._onCompleted(queueName);
this._onFailed(queueName);
}
}, {
key: '_createDocument',
value: function _createDocument(data, _extra_data) {
if (this.client.persistence.adapter !== 'couchdb') {
return false;
}
if (!this.client.persist) {
return false;
}
var extra_data = this._getDocument(data, _extra_data);
var doc = new _couchdb2.default(this.client.persistence);
doc.create(null, extra_data, this.client.persistence.database).then(function (resp) {/*CREATED - console.log(resp);*/}).catch(function (err) {});
}
}, {
key: '_getDocument',
value: function _getDocument(job, _extra_data) {
var d = _extends({
_id: job.id.toString(),
name: job.name,
data: job.data,
delay: job.delay,
timestamp: job.timestamp,
attemptsMade: job.attemptsMade,
stacktrace: job.stacktrace,
returnvalue: job.returnvalue,
finishedOn: job.finishedOn,
processedOn: job.processedOn
}, _extra_data);
return d;
}
}, {
key: '_updateDocument',
value: function _updateDocument(job_id, data, _extra) {
if (this.client.persistence.adapter !== 'couchdb') {
return false;
}
if (!this.client.persist) {
return false;
}
var doc = new _couchdb2.default(this.client.persistence);
var _id = job_id.toString();
var extra_data = this._getDocument(data, _extra);
doc.update(null, { input: extra_data, _id: _id, store: this.client.persistence.database }).then(function (resp) {/* RESPONSE */}).catch(function (err) {});
}
}, {
key: '_onCompleted',
value: function _onCompleted(queueName) {
var _this = this;
queues[queueName].on('completed', function (job, result) {
var tmp = {
status: "COMPLETED",
finishedOn: (0, _moment2.default)().format('YYYY-MM-DD HH:mm:ss')
};
_this._updateDocument(job.id, job, tmp);
});
}
}, {
key: '_onFailed',
value: function _onFailed(queueName) {
var _this2 = this;
queues[queueName].on('failed', function (job, result) {
var tmp = {
status: "FAILED",
failedOn: (0, _moment2.default)().format('YYYY-MM-DD HH:mm:ss')
};
_this2._updateDocument(job.id, job, tmp);
});
}
}, {
key: '_getQueueName',
value: function _getQueueName() {
return this.client.prefix + this.client.name;
}
}, {
key: 'push',
value: function push(data) {
var _this3 = this;
var queueName = this._getQueueName();
var options = _extends({}, this.client.options, data.options);
return new Promise(function (resolve, reject) {
queues[queueName].add(queueName, data.data, _extends({}, options)).then(function (resp) {
var tmp_resp = {
id: resp.id,
data: resp.data
};
_this3._createDocument(resp, { status: "PENDING", jobType: data.jobType });
resolve(tmp_resp);
}).catch(function (err) {
reject(err);
});
});
}
}, {
key: 'clean',
value: function clean(data) {
var queueName = this._getQueueName();
return new Promise(function (resolve, reject) {
queues[queueName].clean(data.grace, data.type).then(function (resp) {
resolve(resp);
}).catch(function (err) {
reject(err);
});
});
}
}]);
return BullQueueAdapter;
}();
exports.default = BullQueueAdapter;