Skip to content

Commit

Permalink
Merge pull request #3 from sloops77/011
Browse files Browse the repository at this point in the history
compile to es2015
  • Loading branch information
sloops77 authored Nov 22, 2016
2 parents 19ca0b8 + e62f23d commit 647294d
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 42 deletions.
84 changes: 58 additions & 26 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,75 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

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; }; }(); /**
* Created by arolave on 19/09/2016.
*/


var _redlock = require('redlock');

var _redlock2 = _interopRequireDefault(_redlock);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class RedisExclusiveTask {
constructor(taskName, task, interval) {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var RedisExclusiveTask = function () {
function RedisExclusiveTask(taskName, task, interval) {
_classCallCheck(this, RedisExclusiveTask);

this.taskName = taskName;
this.task = task;
this.interval = interval;
}

extendAndRun(lock) {
return lock.extend(this.interval * 2).then(() => this.task()).delay(this.interval).then(() => this.extendAndRun(lock)) // recurse
.catch(() => RedisExclusiveTask.log.info(`@RedisExclusiveTask(${ this.taskName }): Unable to extend lock`)).finally(() => RedisExclusiveTask.log.warn(`@RedisExclusiveTask(${ this.taskName }): Leaving extendAndUpdate`));
// only on error bail out
}
_createClass(RedisExclusiveTask, [{
key: 'extendAndRun',
value: function extendAndRun(lock) {
var _this = this;

lockAndRun() {
return RedisExclusiveTask.redlockInstance.lock(`${ this.taskName }:run`, this.interval * 2).then(lock => {
RedisExclusiveTask.log.info(`@RedisExclusiveTask(${ this.taskName }): I have the lock!`);
return this.extendAndRun(lock);
}).catch(() => {});
}
return lock.extend(this.interval * 2).then(function () {
return _this.task();
}).delay(this.interval).then(function () {
return _this.extendAndRun(lock);
}) // recurse
.catch(function () {
return RedisExclusiveTask.log.info('@RedisExclusiveTask(' + _this.taskName + '): Unable to extend lock');
}).finally(function () {
return RedisExclusiveTask.log.warn('@RedisExclusiveTask(' + _this.taskName + '): Leaving extendAndUpdate');
});
// only on error bail out
}
}, {
key: 'lockAndRun',
value: function lockAndRun() {
var _this2 = this;

static configure(clients, log = console) {
this.log = log;
this.redlockInstance = new _redlock2.default(clients, { retryCount: 0 });
}
return RedisExclusiveTask.redlockInstance.lock(this.taskName + ':run', this.interval * 2).then(function (lock) {
RedisExclusiveTask.log.info('@RedisExclusiveTask(' + _this2.taskName + '): I have the lock!');
return _this2.extendAndRun(lock);
}).catch(function () {});
}
}], [{
key: 'configure',
value: function configure(clients) {
var log = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : console;

static run(taskName, task, interval) {
const executor = new RedisExclusiveTask(taskName, task, interval);
executor.lockAndRun();
setInterval(() => executor.lockAndRun(), interval * 2.5); // max outage should be 120s
}
}
exports.default = RedisExclusiveTask; /**
* Created by arolave on 19/09/2016.
*/
this.log = log;
this.redlockInstance = new _redlock2.default(clients, { retryCount: 0 });
}
}, {
key: 'run',
value: function run(taskName, task, interval) {
var executor = new RedisExclusiveTask(taskName, task, interval);
executor.lockAndRun();
setInterval(function () {
return executor.lockAndRun();
}, interval * 2.5); // max outage should be 120s
}
}]);

return RedisExclusiveTask;
}();

exports.default = RedisExclusiveTask;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "redis-exclusive-task",
"version": "0.1.1",
"version": "0.1.2",
"scripts": {
"build": "babel src/index.js --out-file dist/index.js --presets node6"
"build": "babel src/index.js --out-file dist/index.js --presets es2015"
},
"main": "dist/index.js",
"dependencies": {
"redlock": "2.0.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-node6": "^11.0.0"
"babel-preset-es2015": "^6.18.0"
}
}
Loading

0 comments on commit 647294d

Please sign in to comment.