Skip to content

Commit

Permalink
Merge pull request #1 from sloops77/0.1
Browse files Browse the repository at this point in the history
0.1
  • Loading branch information
sloops77 authored Nov 21, 2016
2 parents 10fcb39 + 68b0f92 commit e5d160a
Show file tree
Hide file tree
Showing 6 changed files with 1,419 additions and 0 deletions.
46 changes: 46 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _redlock = require('redlock');

var _redlock2 = _interopRequireDefault(_redlock);

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

class RedisExclusiveTask {
constructor(taskName, task, interval) {
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(() => this.log.info(`@RedisExclusiveTask(${ this.taskName }): Unable to extend lock`)).finally(() => this.log.warn(`@RedisExclusiveTask(${ this.taskName }): Leaving extendAndUpdate`));
// only on error bail out
}

lockAndRun() {
return this.redlock.lock(`${ this.taskName }:run`, this.interval * 2).then(lock => {
log.info(`@RedisExclusiveTask(${ this.taskName }): I have the lock!`);
return this.extendAndRun(lock);
}).catch(() => {});
}

static configure(clients, log = console) {
this.log = log;
this.redlock = new _redlock2.default(clients, { retryCount: 0 });
}

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.
*/
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Created by arolave on 21/11/2016.
*/
require('dist/index');
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "redis-exclusive-task",
"version": "0.1.0",
"private": true,
"scripts": {
"build": "babel src/index.js --out-file dist/index.js --presets node6"
},
"main": "dist/index.js",
"dependencies": {
"redlock": "2.0.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-node6": "^11.0.0"
}
}
43 changes: 43 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Created by arolave on 19/09/2016.
*/
import Redlock from 'redlock';

export default class RedisExclusiveTask {
constructor(taskName, task, interval) {
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(() => this.log.info(`@RedisExclusiveTask(${this.taskName}): Unable to extend lock`))
.finally(() => this.log.warn(`@RedisExclusiveTask(${this.taskName}): Leaving extendAndUpdate`));
// only on error bail out
}

lockAndRun() {
return this.redlock.lock(`${this.taskName}:run`, this.interval * 2)
.then((lock) => {
log.info(`@RedisExclusiveTask(${this.taskName}): I have the lock!`);
return this.extendAndRun(lock);
})
.catch(() => {});
}

static configure(clients, log = console) {
this.log = log;
this.redlock = new Redlock(clients, { retryCount: 0 });
}

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
}
}
46 changes: 46 additions & 0 deletions yarn-error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Arguments:
/usr/local/bin/node /usr/local/bin/yarn install

PATH:
/Users/arolave/.rbenv/shims:/usr/local/heroku/bin:./bin:/Users/arolave/Library/apache-maven-2.2.1/bin:/Users/arolave/Library/play:/Users/arolave/Library/android-sdk-macosx/tools:/Users/arolave/Library/android-sdk-macosx/platform-tools:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin:/Users/arolave/Library/Trigger Toolkit:/Users/arolave/utils/imobiledevice-macosx

Yarn version:
0.16.1

Node version:
6.6.0

Platform:
darwin x64

npm manifest:
{
"name": "redis-exclusive-task",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "",
"release": ""
},
"main": "dist/index.js",
"dependencies": {
"redlock": "2.0.0",
}
}

yarn manifest:
No manifest

bower manifest:
No manifest

Lockfile:
No lockfile

Trace:
SyntaxError: /Users/arolave/IdeaProjects/redis-exclusive-task/package.json: Unexpected token } in JSON at position 205
at Object.parse (native)
at /usr/local/lib/node_modules/yarn/lib/util/fs.js:271:57
at next (native)
at step (/usr/local/lib/node_modules/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /usr/local/lib/node_modules/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
Loading

0 comments on commit e5d160a

Please sign in to comment.