-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sloops77/0.1
0.1
- Loading branch information
Showing
6 changed files
with
1,419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Created by arolave on 21/11/2016. | ||
*/ | ||
require('dist/index'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.