process-custodian
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -106,3 +106,3 @@ 'use strict'; | ||
var renewingMasterReservation = function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(tickTime) { | ||
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(tickTime, marginTimeForRenew) { | ||
var renewDate, result; | ||
@@ -115,3 +115,3 @@ return _regenerator2.default.wrap(function _callee3$(_context3) { | ||
renewDate.setSeconds(renewDate.getSeconds() - tickTime * 2); | ||
renewDate.setSeconds(renewDate.getSeconds() - (tickTime + marginTimeForRenew)); | ||
_context3.prev = 2; | ||
@@ -152,3 +152,3 @@ _context3.next = 5; | ||
return function renewingMasterReservation(_x2) { | ||
return function renewingMasterReservation(_x2, _x3) { | ||
return _ref4.apply(this, arguments); | ||
@@ -159,4 +159,4 @@ }; | ||
var tryBeMaster = function () { | ||
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(tickTime, isInit) { | ||
var result, deathDate; | ||
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(tickTime, marginTimeForRenew, isInit) { | ||
var result, deathDate, limit; | ||
return _regenerator2.default.wrap(function _callee4$(_context4) { | ||
@@ -192,6 +192,7 @@ while (1) { | ||
deathDate = new Date(); | ||
limit = tickTime + (isInit ? marginTimeForRenew : marginTimeForRenew * 2); | ||
// no active master or last one is too busy to be master | ||
deathDate.setSeconds(deathDate.getSeconds() - tickTime * (isInit ? 2 : 3)); | ||
_context4.next = 10; | ||
deathDate.setSeconds(deathDate.getSeconds() - limit); | ||
_context4.next = 11; | ||
return this._collection.updateOne({ | ||
@@ -208,8 +209,8 @@ // if no active master | ||
case 10: | ||
case 11: | ||
result = _context4.sent; | ||
return _context4.abrupt('return', result.modifiedCount); | ||
case 14: | ||
_context4.prev = 14; | ||
case 15: | ||
_context4.prev = 15; | ||
_context4.t0 = _context4['catch'](0); | ||
@@ -219,6 +220,6 @@ | ||
case 17: | ||
case 18: | ||
return _context4.abrupt('return', false); | ||
case 18: | ||
case 19: | ||
case 'end': | ||
@@ -228,6 +229,6 @@ return _context4.stop(); | ||
} | ||
}, _callee4, this, [[0, 14]]); | ||
}, _callee4, this, [[0, 15]]); | ||
})); | ||
return function tryBeMaster(_x3, _x4) { | ||
return function tryBeMaster(_x4, _x5, _x6) { | ||
return _ref5.apply(this, arguments); | ||
@@ -268,3 +269,5 @@ }; | ||
_ref$tickTimeInSecond = _ref.tickTimeInSeconds, | ||
tickTimeInSeconds = _ref$tickTimeInSecond === undefined ? 60 : _ref$tickTimeInSecond; | ||
tickTimeInSeconds = _ref$tickTimeInSecond === undefined ? 60 : _ref$tickTimeInSecond, | ||
_ref$marginTimeForRen = _ref.marginTimeForRenew, | ||
marginTimeForRenew = _ref$marginTimeForRen === undefined ? 10 : _ref$marginTimeForRen; | ||
(0, _classCallCheck3.default)(this, ProcessCustodian); | ||
@@ -289,6 +292,9 @@ this._isMaster = false; | ||
if (!rawCollection) { | ||
throw new Error('Missing Collection in constructor of ProcessCustodian'); | ||
} | ||
this._emitter = new _events2.default(); | ||
this._collection = rawCollection; | ||
ensureIndexExist.call(this, tickTimeInSeconds); | ||
this._stop = runActivityQueue.call(this, tickTimeInSeconds, true); | ||
this._stop = runActivityQueue.call(this, tickTimeInSeconds, marginTimeForRenew, true); | ||
@@ -322,6 +328,6 @@ // preparing methods onTick, onIAmNewMaster, onIAmSlave | ||
function runActivityQueue(tickTimeInSeconds) { | ||
function runActivityQueue(tickTimeInSeconds, marginTimeForRenew) { | ||
var _this2 = this; | ||
var isInit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var isInit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
@@ -332,67 +338,77 @@ if (isInit && this._timeout !== null) { | ||
} | ||
this._timeout = setTimeout((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() { | ||
var wasMaster; | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
wasMaster = _this2._isMaster; | ||
_context5.prev = 1; | ||
var doTick = function () { | ||
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() { | ||
var wasMaster; | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
wasMaster = _this2._isMaster; | ||
_context5.prev = 1; | ||
if (!wasMaster) { | ||
_context5.next = 8; | ||
break; | ||
} | ||
if (!wasMaster) { | ||
_context5.next = 8; | ||
break; | ||
} | ||
_context5.next = 5; | ||
return renewingMasterReservation.call(_this2, tickTimeInSeconds); | ||
_context5.next = 5; | ||
return renewingMasterReservation.call(_this2, tickTimeInSeconds, marginTimeForRenew); | ||
case 5: | ||
_this2._isMaster = _context5.sent; | ||
_context5.next = 11; | ||
break; | ||
case 5: | ||
_this2._isMaster = _context5.sent; | ||
_context5.next = 11; | ||
break; | ||
case 8: | ||
_context5.next = 10; | ||
return tryBeMaster.call(_this2, tickTimeInSeconds, isInit); | ||
case 8: | ||
_context5.next = 10; | ||
return tryBeMaster.call(_this2, tickTimeInSeconds, marginTimeForRenew, isInit); | ||
case 10: | ||
_this2._isMaster = _context5.sent; | ||
case 10: | ||
_this2._isMaster = _context5.sent; | ||
case 11: | ||
_context5.next = 13; | ||
return oneHeartbeat.call(_this2, tickTimeInSeconds); | ||
case 11: | ||
_context5.next = 13; | ||
return oneHeartbeat.call(_this2, tickTimeInSeconds); | ||
case 13: | ||
_context5.next = 18; | ||
break; | ||
case 13: | ||
_context5.next = 18; | ||
break; | ||
case 15: | ||
_context5.prev = 15; | ||
_context5.t0 = _context5['catch'](1); | ||
case 15: | ||
_context5.prev = 15; | ||
_context5.t0 = _context5['catch'](1); | ||
console.error('ActivityQueue:', _context5.t0); | ||
console.error('ActivityQueue:', _context5.t0); | ||
case 18: | ||
_context5.prev = 18; | ||
case 18: | ||
_context5.prev = 18; | ||
runActivityQueue.call(_this2, tickTimeInSeconds); | ||
_this2._emitter.emit(_constants.EVENTS.TICK); | ||
if (!wasMaster && _this2._isMaster) { | ||
_this2._emitter.emit(_constants.EVENTS.I_AM_MASTER); | ||
} | ||
if ((wasMaster || isInit) && !_this2._isMaster) { | ||
_this2._emitter.emit(_constants.EVENTS.I_AM_SLAVE); | ||
} | ||
return _context5.finish(18); | ||
runActivityQueue.call(_this2, tickTimeInSeconds, marginTimeForRenew); | ||
_this2._emitter.emit(_constants.EVENTS.TICK); | ||
if (!wasMaster && _this2._isMaster) { | ||
_this2._emitter.emit(_constants.EVENTS.I_AM_MASTER); | ||
} | ||
if ((wasMaster || isInit) && !_this2._isMaster) { | ||
_this2._emitter.emit(_constants.EVENTS.I_AM_SLAVE); | ||
} | ||
return _context5.finish(18); | ||
case 24: | ||
case 'end': | ||
return _context5.stop(); | ||
case 24: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
} | ||
}, _callee5, _this2, [[1, 15, 18, 24]]); | ||
})), isInit ? 0 : tickTimeInSeconds * 1000); | ||
}, _callee5, _this2, [[1, 15, 18, 24]]); | ||
})); | ||
return function doTick() { | ||
return _ref6.apply(this, arguments); | ||
}; | ||
}(); | ||
if (isInit) { | ||
doTick(); | ||
} else { | ||
this._timeout = setTimeout(doTick, tickTimeInSeconds * 1000); | ||
} | ||
return _stop.bind(this); | ||
} |
{ | ||
"name": "process-custodian", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "This package helps with organizing of tasks between few instances of same app, It can identify processes and track them activity.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -62,2 +62,3 @@ # Process Custodian | ||
tickTimeInSeconds: 60 | ||
marginTimeForRenew = 10 | ||
}); | ||
@@ -64,0 +65,0 @@ |
@@ -25,7 +25,10 @@ import {MASTER_KEY, EVENTS} from './constants'; | ||
constructor ({rawCollection, tickTimeInSeconds = 60}) { | ||
constructor ({rawCollection, tickTimeInSeconds = 60, marginTimeForRenew = 10}) { | ||
if (!rawCollection) { | ||
throw new Error('Missing Collection in constructor of ProcessCustodian'); | ||
} | ||
this._emitter = new EventEmitter(); | ||
this._collection = rawCollection; | ||
ensureIndexExist.call(this, tickTimeInSeconds); | ||
this._stop = runActivityQueue.call(this, tickTimeInSeconds, true); | ||
this._stop = runActivityQueue.call(this, tickTimeInSeconds, marginTimeForRenew, true); | ||
@@ -92,5 +95,5 @@ // preparing methods onTick, onIAmNewMaster, onIAmSlave | ||
async function renewingMasterReservation (tickTime) { | ||
async function renewingMasterReservation (tickTime, marginTimeForRenew) { | ||
const renewDate = new Date(); | ||
renewDate.setSeconds(renewDate.getSeconds() - (tickTime * 2)); | ||
renewDate.setSeconds(renewDate.getSeconds() - (tickTime + marginTimeForRenew)); | ||
try { | ||
@@ -115,3 +118,3 @@ const result = await this._collection.updateOne({ | ||
async function tryBeMaster (tickTime, isInit) { | ||
async function tryBeMaster (tickTime, marginTimeForRenew, isInit) { | ||
try { | ||
@@ -134,4 +137,5 @@ // check if you can be first master | ||
const deathDate = new Date(); | ||
const limit = (tickTime + (isInit? marginTimeForRenew : (marginTimeForRenew * 2))); | ||
// no active master or last one is too busy to be master | ||
deathDate.setSeconds(deathDate.getSeconds() - (tickTime * (isInit? 2 : 3))); | ||
deathDate.setSeconds(deathDate.getSeconds() - limit); | ||
result = await this._collection.updateOne({ | ||
@@ -162,3 +166,4 @@ // if no active master | ||
function runActivityQueue(tickTimeInSeconds, isInit = false) { | ||
function runActivityQueue(tickTimeInSeconds, marginTimeForRenew, isInit = false) { | ||
if (isInit && this._timeout !== null) { | ||
@@ -168,9 +173,9 @@ // there is only one loop for activity per process | ||
} | ||
this._timeout = setTimeout(async () => { | ||
const doTick = async () => { | ||
const wasMaster = this._isMaster; | ||
try { | ||
if (wasMaster) { | ||
this._isMaster = await renewingMasterReservation.call(this, tickTimeInSeconds); | ||
this._isMaster = await renewingMasterReservation.call(this, tickTimeInSeconds, marginTimeForRenew); | ||
} else { | ||
this._isMaster = await tryBeMaster.call(this, tickTimeInSeconds, isInit); | ||
this._isMaster = await tryBeMaster.call(this, tickTimeInSeconds, marginTimeForRenew, isInit); | ||
} | ||
@@ -182,3 +187,3 @@ await oneHeartbeat.call(this, tickTimeInSeconds); | ||
finally { | ||
runActivityQueue.call(this, tickTimeInSeconds); | ||
runActivityQueue.call(this, tickTimeInSeconds, marginTimeForRenew); | ||
this._emitter.emit(EVENTS.TICK); | ||
@@ -192,4 +197,8 @@ if (!wasMaster && this._isMaster) { | ||
} | ||
}, isInit ? 0 : tickTimeInSeconds * 1000); | ||
}; | ||
if (isInit) { | ||
doTick(); | ||
} else { | ||
this._timeout = setTimeout(doTick, tickTimeInSeconds * 1000); | ||
} | ||
return _stop.bind(this); | ||
@@ -196,0 +205,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30292
618
96