@movilizame/noderavel
Version:
Laravel-NodeJs Queue Sync
111 lines (90 loc) • 4.22 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } 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; }
var Serialize = require('php-serialize');
var EventEmitter = require('events');
var Noderavel =
/*#__PURE__*/
function (_EventEmitter) {
_inherits(Noderavel, _EventEmitter);
function Noderavel(_ref) {
var _this;
var _ref$driver = _ref.driver,
driver = _ref$driver === void 0 ? 'redis' : _ref$driver,
client = _ref.client,
_ref$scope = _ref.scope,
scope = _ref$scope === void 0 ? {} : _ref$scope,
_ref$queue = _ref.queue,
queue = _ref$queue === void 0 ? 'default' : _ref$queue;
_classCallCheck(this, Noderavel);
_this = _possibleConstructorReturn(this, (Noderavel.__proto__ || Object.getPrototypeOf(Noderavel)).call(this));
_this.driver = driver;
_this.client = client;
_this.scope = scope;
_this.queue = queue;
return _this;
}
_createClass(Noderavel, [{
key: "listen",
value: function listen() {
switch (this.driver) {
case 'redis':
this.redisPop();
break;
}
}
}, {
key: "push",
value: function push(name, object) {
switch (this.driver) {
case 'redis':
this.redisPush(name, object);
break;
}
}
}, {
key: "redisPop",
value: function redisPop() {
var _this2 = this;
var pop = function pop() {
_this2.client.blpop('queues:' + _this2.queue, 60000, function (err, replay) {
if (err) {// Error!
} else {
var obj = JSON.parse(replay[1]);
var command = obj.data.command;
var raw = Serialize.unserialize(command, _this2.scope);
_this2.emit('job', {
name: obj.data.commandName,
data: raw
});
}
pop();
});
};
pop();
}
}, {
key: "redisPush",
value: function redisPush(name, object) {
var command = Serialize.serialize(object, this.scope);
var data = {
job: 'Illuminate\\Queue\\CallQueuedHandler@call',
data: {
commandName: name,
command: command
},
id: Date.now(),
attempts: 1
};
this.client.rpush('queues:' + this.queue, JSON.stringify(data), function (err, replay) {// Queue pushed
});
}
}]);
return Noderavel;
}(EventEmitter);
module.exports = Noderavel;