taskpace 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of taskpace might be problematic. Click here for more details.
- package/.babelrc.es5 +5 -0
- package/.babelrc.lib +9 -0
- package/.env +2 -0
- package/.travis.yml +25 -0
- package/LICENSE +20 -0
- package/README.md +474 -2
- package/bower.json +30 -0
- package/es5.js +5064 -0
- package/lib/Batcher.js +1 -0
- package/lib/DLList.js +1 -0
- package/lib/Events.js +1 -0
- package/lib/Group.js +1 -0
- package/lib/IORedisConnection.js +1 -0
- package/lib/Job.js +1 -0
- package/lib/LocalDatastore.js +1 -0
- package/lib/Queues.js +1 -0
- package/lib/RedisConnection.js +1 -0
- package/lib/RedisDatastore.js +1 -0
- package/lib/Scripts.js +1 -0
- package/lib/States.js +1 -0
- package/lib/Sync.js +1 -0
- package/lib/Taskpace.js +1 -0
- package/lib/TaskpaceError.js +1 -0
- package/lib/common.js +1 -0
- package/lib/es5.js +1 -0
- package/lib/index.js +1 -0
- package/lib/limit.js +1 -0
- package/lib/lua.json +24 -0
- package/lib/parser.js +1 -0
- package/lib/redis.js +1 -0
- package/lib/validate.js +1 -0
- package/lib/version.json +1 -0
- package/light.js +1524 -0
- package/package.json +53 -4
- package/rollup.config.es5.js +34 -0
- package/rollup.config.light.js +44 -0
- package/scripts/assemble_lua.js +25 -0
- package/scripts/build.sh +81 -0
- package/scripts/test_all.sh +20 -0
- package/scripts/version.js +3 -0
- package/src/Batcher.coffee +39 -0
- package/src/DLList.coffee +38 -0
- package/src/Events.coffee +38 -0
- package/src/Group.coffee +80 -0
- package/src/IORedisConnection.coffee +84 -0
- package/src/Job.coffee +98 -0
- package/src/LocalDatastore.coffee +140 -0
- package/src/Queues.coffee +28 -0
- package/src/RedisConnection.coffee +91 -0
- package/src/RedisDatastore.coffee +158 -0
- package/src/Scripts.coffee +151 -0
- package/src/States.coffee +43 -0
- package/src/Sync.coffee +28 -0
- package/src/Taskpace.coffee +298 -0
- package/src/TaskpaceError.coffee +3 -0
- package/src/es5.coffee +3 -0
- package/src/index.coffee +1 -0
- package/src/parser.coffee +10 -0
- package/src/redis/blacklist_client.lua +8 -0
- package/src/redis/check.lua +6 -0
- package/src/redis/conditions_check.lua +3 -0
- package/src/redis/current_reservoir.lua +1 -0
- package/src/redis/done.lua +3 -0
- package/src/redis/free.lua +5 -0
- package/src/redis/get_time.lua +7 -0
- package/src/redis/group_check.lua +1 -0
- package/src/redis/heartbeat.lua +1 -0
- package/src/redis/increment_reservoir.lua +10 -0
- package/src/redis/init.lua +105 -0
- package/src/redis/process_tick.lua +214 -0
- package/src/redis/queued.lua +10 -0
- package/src/redis/refresh_expiration.lua +11 -0
- package/src/redis/refs.lua +13 -0
- package/src/redis/register.lua +51 -0
- package/src/redis/register_client.lua +12 -0
- package/src/redis/running.lua +1 -0
- package/src/redis/submit.lua +74 -0
- package/src/redis/update_settings.lua +14 -0
- package/src/redis/validate_client.lua +5 -0
- package/src/redis/validate_keys.lua +3 -0
- package/taskpace.d.ts +629 -0
- package/taskpace.d.ts.ejs +588 -0
- package/test/DLList.js +148 -0
- package/test/batcher.js +209 -0
- package/test/cluster.js +1549 -0
- package/test/context.js +142 -0
- package/test/general.js +867 -0
- package/test/group.js +255 -0
- package/test/ioredis.js +135 -0
- package/test/node_redis.js +100 -0
- package/test/priority.js +184 -0
- package/test/promises.js +202 -0
- package/test/retries.js +237 -0
- package/test/spawn/increaseKeepAlive.js +17 -0
- package/test/spawn/refreshKeepAlive.js +17 -0
- package/test/states.js +103 -0
- package/test/stop.js +208 -0
- package/test/taskpace.js +7 -0
- package/test.ts +335 -0
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "taskpace",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Distributed task scheduler and rate limiter",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "taskpace.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha test",
|
|
9
|
+
"test-all": "./scripts/test_all.sh"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/PaceJS/taskpace"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"async rate limiter",
|
|
17
|
+
"rate limiter",
|
|
18
|
+
"rate limiting",
|
|
19
|
+
"async",
|
|
20
|
+
"rate",
|
|
21
|
+
"limiting",
|
|
22
|
+
"limiter",
|
|
23
|
+
"throttle",
|
|
24
|
+
"throttling",
|
|
25
|
+
"throttler",
|
|
26
|
+
"load",
|
|
27
|
+
"clustering"
|
|
28
|
+
],
|
|
29
|
+
"author": {
|
|
30
|
+
"name": "Tom Hundley"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/PaceJS/taskpace/issues"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/core": "^7.5.0",
|
|
38
|
+
"@babel/preset-env": "^7.5.0",
|
|
39
|
+
"@types/es6-promise": "0.0.33",
|
|
40
|
+
"assert": "^1.5.0",
|
|
41
|
+
"coffeescript": "2.4.x",
|
|
42
|
+
"ioredis": "^4.11.1",
|
|
43
|
+
"leakage": "^0.4.0",
|
|
44
|
+
"mocha": "^6.1.4",
|
|
45
|
+
"redis": "^2.8.0",
|
|
46
|
+
"regenerator-runtime": "^0.12.1",
|
|
47
|
+
"rollup": "^0.66.6",
|
|
48
|
+
"rollup-plugin-babel": "^4.3.3",
|
|
49
|
+
"rollup-plugin-commonjs": "^9.3.4",
|
|
50
|
+
"rollup-plugin-json": "^3.1.0",
|
|
51
|
+
"rollup-plugin-node-resolve": "^3.4.0",
|
|
52
|
+
"typescript": "^2.6.2"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {}
|
|
55
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import json from 'rollup-plugin-json';
|
|
2
|
+
import resolve from 'rollup-plugin-node-resolve';
|
|
3
|
+
import commonjs from 'rollup-plugin-commonjs';
|
|
4
|
+
import babel from 'rollup-plugin-babel';
|
|
5
|
+
|
|
6
|
+
const bannerLines = [
|
|
7
|
+
'This file contains the full Taskpace library (MIT) compiled to ES5.',
|
|
8
|
+
'https://github.com/PaceJS/taskpace',
|
|
9
|
+
'It also contains the regenerator-runtime (MIT), necessary for Babel-generated ES5 code to execute promise and async/await code.',
|
|
10
|
+
'See the following link for Copyright and License information:',
|
|
11
|
+
'https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js',
|
|
12
|
+
].map(x => ` * ${x}`).join('\n');
|
|
13
|
+
const banner = `/**\n${bannerLines}\n */`;
|
|
14
|
+
|
|
15
|
+
export default {
|
|
16
|
+
input: 'lib/es5.js',
|
|
17
|
+
output: {
|
|
18
|
+
name: 'Taskpace',
|
|
19
|
+
file: 'es5.js',
|
|
20
|
+
sourcemap: false,
|
|
21
|
+
globals: {},
|
|
22
|
+
format: 'umd',
|
|
23
|
+
banner
|
|
24
|
+
},
|
|
25
|
+
external: [],
|
|
26
|
+
plugins: [
|
|
27
|
+
json(),
|
|
28
|
+
resolve(),
|
|
29
|
+
commonjs(),
|
|
30
|
+
babel({
|
|
31
|
+
exclude: 'node_modules/**'
|
|
32
|
+
})
|
|
33
|
+
]
|
|
34
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import commonjs from 'rollup-plugin-commonjs';
|
|
2
|
+
import json from 'rollup-plugin-json';
|
|
3
|
+
import resolve from 'rollup-plugin-node-resolve';
|
|
4
|
+
|
|
5
|
+
const bannerLines = [
|
|
6
|
+
'This file contains the Taskpace library (MIT), compiled to ES2017, and without Clustering support.',
|
|
7
|
+
'https://github.com/PaceJS/taskpace',
|
|
8
|
+
].map(x => ` * ${x}`).join('\n');
|
|
9
|
+
const banner = `/**\n${bannerLines}\n */`;
|
|
10
|
+
|
|
11
|
+
const missing = `export default () => console.log('You must import the full version of Taskpace in order to use this feature.');`;
|
|
12
|
+
const exclude = [
|
|
13
|
+
'RedisDatastore.js',
|
|
14
|
+
'RedisConnection.js',
|
|
15
|
+
'IORedisConnection.js',
|
|
16
|
+
'Scripts.js'
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
input: 'lib/index.js',
|
|
21
|
+
output: {
|
|
22
|
+
name: 'Taskpace',
|
|
23
|
+
file: 'light.js',
|
|
24
|
+
sourcemap: false,
|
|
25
|
+
globals: {},
|
|
26
|
+
format: 'umd',
|
|
27
|
+
banner
|
|
28
|
+
},
|
|
29
|
+
external: [],
|
|
30
|
+
plugins: [
|
|
31
|
+
json(),
|
|
32
|
+
{
|
|
33
|
+
load: id => {
|
|
34
|
+
const chunks = id.split('/');
|
|
35
|
+
const file = chunks[chunks.length - 1];
|
|
36
|
+
if (exclude.indexOf(file) >= 0) {
|
|
37
|
+
return missing
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
resolve(),
|
|
42
|
+
commonjs()
|
|
43
|
+
]
|
|
44
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var fs = require('fs')
|
|
2
|
+
|
|
3
|
+
var input = __dirname + '/../src/redis'
|
|
4
|
+
var loaded = {}
|
|
5
|
+
|
|
6
|
+
var promises = fs.readdirSync(input).map(function (file) {
|
|
7
|
+
return new Promise(function (resolve, reject) {
|
|
8
|
+
fs.readFile(input + '/' + file, function (err, data) {
|
|
9
|
+
if (err != null) {
|
|
10
|
+
return reject(err)
|
|
11
|
+
}
|
|
12
|
+
loaded[file] = data.toString('utf8')
|
|
13
|
+
return resolve()
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
Promise.all(promises)
|
|
19
|
+
.then(function () {
|
|
20
|
+
console.log(JSON.stringify(loaded, Object.keys(loaded).sort(), 2))
|
|
21
|
+
})
|
|
22
|
+
.catch(function (err) {
|
|
23
|
+
console.error(err)
|
|
24
|
+
process.exit(1)
|
|
25
|
+
})
|
package/scripts/build.sh
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -e
|
|
4
|
+
|
|
5
|
+
if [ ! -d node_modules ]; then
|
|
6
|
+
echo "[B] Run 'npm install' first"
|
|
7
|
+
exit 1
|
|
8
|
+
fi
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
clean() {
|
|
12
|
+
rm -f .babelrc
|
|
13
|
+
rm -rf lib/*
|
|
14
|
+
node scripts/version.js > lib/version.json
|
|
15
|
+
node scripts/assemble_lua.js > lib/lua.json
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
makeLib10() {
|
|
19
|
+
echo '[B] Compiling Taskpace to Node 10+...'
|
|
20
|
+
npx coffee --compile --bare --no-header src/*.coffee
|
|
21
|
+
mv src/*.js lib/
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
makeLib6() {
|
|
25
|
+
echo '[B] Compiling Taskpace to Node 6+...'
|
|
26
|
+
ln -s .babelrc.lib .babelrc
|
|
27
|
+
npx coffee --compile --bare --no-header --transpile src/*.coffee
|
|
28
|
+
mv src/*.js lib/
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
makeES5() {
|
|
32
|
+
echo '[B] Compiling Taskpace to ES5...'
|
|
33
|
+
ln -s .babelrc.es5 .babelrc
|
|
34
|
+
npx coffee --compile --bare --no-header src/*.coffee
|
|
35
|
+
mv src/*.js lib/
|
|
36
|
+
|
|
37
|
+
echo '[B] Assembling ES5 bundle...'
|
|
38
|
+
npx rollup -c rollup.config.es5.js
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
makeLight() {
|
|
42
|
+
makeLib10
|
|
43
|
+
|
|
44
|
+
echo '[B] Assembling light bundle...'
|
|
45
|
+
npx rollup -c rollup.config.light.js
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
makeTypings() {
|
|
49
|
+
echo '[B] Testing TS typings...'
|
|
50
|
+
npx tsc --noEmit --strict test.ts
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if [ "$1" = 'dev' ]; then
|
|
54
|
+
clean
|
|
55
|
+
makeLib10
|
|
56
|
+
elif [ "$1" = 'bench' ]; then
|
|
57
|
+
clean
|
|
58
|
+
makeLib6
|
|
59
|
+
elif [ "$1" = 'es5' ]; then
|
|
60
|
+
clean
|
|
61
|
+
makeES5
|
|
62
|
+
elif [ "$1" = 'light' ]; then
|
|
63
|
+
clean
|
|
64
|
+
makeLight
|
|
65
|
+
elif [ "$1" = 'typings' ]; then
|
|
66
|
+
makeTypings
|
|
67
|
+
else
|
|
68
|
+
clean
|
|
69
|
+
makeES5
|
|
70
|
+
|
|
71
|
+
clean
|
|
72
|
+
makeLight
|
|
73
|
+
|
|
74
|
+
clean
|
|
75
|
+
makeLib6
|
|
76
|
+
makeTypings
|
|
77
|
+
fi
|
|
78
|
+
|
|
79
|
+
rm -f .babelrc
|
|
80
|
+
|
|
81
|
+
echo '[B] Done!'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -e
|
|
4
|
+
|
|
5
|
+
source .env
|
|
6
|
+
|
|
7
|
+
echo 'ioredis tests'
|
|
8
|
+
DATASTORE=ioredis npm test
|
|
9
|
+
|
|
10
|
+
echo 'NodeRedis tests'
|
|
11
|
+
DATASTORE=redis npm test
|
|
12
|
+
|
|
13
|
+
echo 'ES5 bundle tests'
|
|
14
|
+
BUILD=es5 npm test
|
|
15
|
+
|
|
16
|
+
echo 'Light bundle tests'
|
|
17
|
+
BUILD=light npm test
|
|
18
|
+
|
|
19
|
+
echo 'Local tests'
|
|
20
|
+
npm test
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
parser = require "./parser"
|
|
2
|
+
Events = require "./Events"
|
|
3
|
+
|
|
4
|
+
class Batcher
|
|
5
|
+
defaults:
|
|
6
|
+
maxTime: null
|
|
7
|
+
maxSize: null
|
|
8
|
+
Promise: Promise
|
|
9
|
+
|
|
10
|
+
constructor: (@options={}) ->
|
|
11
|
+
parser.load @options, @defaults, @
|
|
12
|
+
@Events = new Events @
|
|
13
|
+
@_arr = []
|
|
14
|
+
@_resetPromise()
|
|
15
|
+
@_lastFlush = Date.now()
|
|
16
|
+
|
|
17
|
+
_resetPromise: ->
|
|
18
|
+
@_promise = new @Promise (res, rej) => @_resolve = res
|
|
19
|
+
|
|
20
|
+
_flush: ->
|
|
21
|
+
clearTimeout @_timeout
|
|
22
|
+
@_lastFlush = Date.now()
|
|
23
|
+
@_resolve()
|
|
24
|
+
@Events.trigger "batch", @_arr
|
|
25
|
+
@_arr = []
|
|
26
|
+
@_resetPromise()
|
|
27
|
+
|
|
28
|
+
add: (data) ->
|
|
29
|
+
@_arr.push data
|
|
30
|
+
ret = @_promise
|
|
31
|
+
if @_arr.length == @maxSize
|
|
32
|
+
@_flush()
|
|
33
|
+
else if @maxTime? and @_arr.length == 1
|
|
34
|
+
@_timeout = setTimeout =>
|
|
35
|
+
@_flush()
|
|
36
|
+
, @maxTime
|
|
37
|
+
ret
|
|
38
|
+
|
|
39
|
+
module.exports = Batcher
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class DLList
|
|
2
|
+
constructor: (@incr, @decr) ->
|
|
3
|
+
@_first = null
|
|
4
|
+
@_last = null
|
|
5
|
+
@length = 0
|
|
6
|
+
push: (value) ->
|
|
7
|
+
@length++
|
|
8
|
+
@incr?()
|
|
9
|
+
node = { value, prev: @_last, next: null }
|
|
10
|
+
if @_last?
|
|
11
|
+
@_last.next = node
|
|
12
|
+
@_last = node
|
|
13
|
+
else @_first = @_last = node
|
|
14
|
+
undefined
|
|
15
|
+
shift: () ->
|
|
16
|
+
if not @_first? then return
|
|
17
|
+
else
|
|
18
|
+
@length--
|
|
19
|
+
@decr?()
|
|
20
|
+
value = @_first.value
|
|
21
|
+
if (@_first = @_first.next)?
|
|
22
|
+
@_first.prev = null
|
|
23
|
+
else
|
|
24
|
+
@_last = null
|
|
25
|
+
value
|
|
26
|
+
first: () -> if @_first? then @_first.value
|
|
27
|
+
getArray: () ->
|
|
28
|
+
node = @_first
|
|
29
|
+
while node? then (ref = node; node = node.next; ref.value)
|
|
30
|
+
forEachShift: (cb) ->
|
|
31
|
+
node = @shift()
|
|
32
|
+
while node? then (cb node; node = @shift())
|
|
33
|
+
undefined
|
|
34
|
+
debug: () ->
|
|
35
|
+
node = @_first
|
|
36
|
+
while node? then (ref = node; node = node.next; { value: ref.value, prev: ref.prev?.value, next: ref.next?.value })
|
|
37
|
+
|
|
38
|
+
module.exports = DLList
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class Events
|
|
2
|
+
constructor: (@instance) ->
|
|
3
|
+
@_events = {}
|
|
4
|
+
if @instance.on? or @instance.once? or @instance.removeAllListeners?
|
|
5
|
+
throw new Error "An Emitter already exists for this object"
|
|
6
|
+
@instance.on = (name, cb) => @_addListener name, "many", cb
|
|
7
|
+
@instance.once = (name, cb) => @_addListener name, "once", cb
|
|
8
|
+
@instance.removeAllListeners = (name=null) =>
|
|
9
|
+
if name? then delete @_events[name] else @_events = {}
|
|
10
|
+
_addListener: (name, status, cb) ->
|
|
11
|
+
@_events[name] ?= []
|
|
12
|
+
@_events[name].push {cb, status}
|
|
13
|
+
@instance
|
|
14
|
+
listenerCount: (name) ->
|
|
15
|
+
if @_events[name]? then @_events[name].length else 0
|
|
16
|
+
trigger: (name, args...) ->
|
|
17
|
+
try
|
|
18
|
+
if name != "debug" then @trigger "debug", "Event triggered: #{name}", args
|
|
19
|
+
return unless @_events[name]?
|
|
20
|
+
@_events[name] = @_events[name].filter (listener) -> listener.status != "none"
|
|
21
|
+
promises = @_events[name].map (listener) =>
|
|
22
|
+
return if listener.status == "none"
|
|
23
|
+
if listener.status == "once" then listener.status = "none"
|
|
24
|
+
try
|
|
25
|
+
returned = listener.cb?(args...)
|
|
26
|
+
if typeof returned?.then == "function"
|
|
27
|
+
await returned
|
|
28
|
+
else
|
|
29
|
+
returned
|
|
30
|
+
catch e
|
|
31
|
+
if "name" != "error" then @trigger "error", e
|
|
32
|
+
null
|
|
33
|
+
(await Promise.all promises).find (x) -> x?
|
|
34
|
+
catch e
|
|
35
|
+
if "name" != "error" then @trigger "error", e
|
|
36
|
+
null
|
|
37
|
+
|
|
38
|
+
module.exports = Events
|
package/src/Group.coffee
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
parser = require "./parser"
|
|
2
|
+
Events = require "./Events"
|
|
3
|
+
RedisConnection = require "./RedisConnection"
|
|
4
|
+
IORedisConnection = require "./IORedisConnection"
|
|
5
|
+
Scripts = require "./Scripts"
|
|
6
|
+
|
|
7
|
+
class Group
|
|
8
|
+
defaults:
|
|
9
|
+
timeout: 1000 * 60 * 5
|
|
10
|
+
connection: null
|
|
11
|
+
Promise: Promise
|
|
12
|
+
id: "group-key"
|
|
13
|
+
|
|
14
|
+
constructor: (@limiterOptions={}) ->
|
|
15
|
+
parser.load @limiterOptions, @defaults, @
|
|
16
|
+
@Events = new Events @
|
|
17
|
+
@instances = {}
|
|
18
|
+
@Taskpace = require "./Taskpace"
|
|
19
|
+
@_startAutoCleanup()
|
|
20
|
+
@sharedConnection = @connection?
|
|
21
|
+
|
|
22
|
+
if !@connection?
|
|
23
|
+
if @limiterOptions.datastore == "redis"
|
|
24
|
+
@connection = new RedisConnection Object.assign {}, @limiterOptions, { @Events }
|
|
25
|
+
else if @limiterOptions.datastore == "ioredis"
|
|
26
|
+
@connection = new IORedisConnection Object.assign {}, @limiterOptions, { @Events }
|
|
27
|
+
|
|
28
|
+
key: (key="") -> @instances[key] ? do =>
|
|
29
|
+
limiter = @instances[key] = new @Taskpace Object.assign @limiterOptions, {
|
|
30
|
+
id: "#{@id}-#{key}",
|
|
31
|
+
@timeout,
|
|
32
|
+
@connection
|
|
33
|
+
}
|
|
34
|
+
@Events.trigger "created", limiter, key
|
|
35
|
+
limiter
|
|
36
|
+
|
|
37
|
+
deleteKey: (key="") =>
|
|
38
|
+
instance = @instances[key]
|
|
39
|
+
if @connection
|
|
40
|
+
deleted = await @connection.__runCommand__ ['del', Scripts.allKeys("#{@id}-#{key}")...]
|
|
41
|
+
if instance?
|
|
42
|
+
delete @instances[key]
|
|
43
|
+
await instance.disconnect()
|
|
44
|
+
instance? or deleted > 0
|
|
45
|
+
|
|
46
|
+
limiters: -> { key: k, limiter: v } for k, v of @instances
|
|
47
|
+
|
|
48
|
+
keys: -> Object.keys @instances
|
|
49
|
+
|
|
50
|
+
clusterKeys: ->
|
|
51
|
+
if !@connection? then return @Promise.resolve @keys()
|
|
52
|
+
keys = []
|
|
53
|
+
cursor = null
|
|
54
|
+
start = "b_#{@id}-".length
|
|
55
|
+
end = "_settings".length
|
|
56
|
+
until cursor == 0
|
|
57
|
+
[next, found] = await @connection.__runCommand__ ["scan", (cursor ? 0), "match", "b_#{@id}-*_settings", "count", 10000]
|
|
58
|
+
cursor = ~~next
|
|
59
|
+
keys.push(k.slice(start, -end)) for k in found
|
|
60
|
+
keys
|
|
61
|
+
|
|
62
|
+
_startAutoCleanup: ->
|
|
63
|
+
clearInterval @interval
|
|
64
|
+
(@interval = setInterval =>
|
|
65
|
+
time = Date.now()
|
|
66
|
+
for k, v of @instances
|
|
67
|
+
try if await v._store.__groupCheck__(time) then @deleteKey k
|
|
68
|
+
catch e then v.Events.trigger "error", e
|
|
69
|
+
, (@timeout / 2)).unref?()
|
|
70
|
+
|
|
71
|
+
updateSettings: (options={}) ->
|
|
72
|
+
parser.overwrite options, @defaults, @
|
|
73
|
+
parser.overwrite options, options, @limiterOptions
|
|
74
|
+
@_startAutoCleanup() if options.timeout?
|
|
75
|
+
|
|
76
|
+
disconnect: (flush=true) ->
|
|
77
|
+
if !@sharedConnection
|
|
78
|
+
@connection?.disconnect flush
|
|
79
|
+
|
|
80
|
+
module.exports = Group
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
parser = require "./parser"
|
|
2
|
+
Events = require "./Events"
|
|
3
|
+
Scripts = require "./Scripts"
|
|
4
|
+
|
|
5
|
+
class IORedisConnection
|
|
6
|
+
datastore: "ioredis"
|
|
7
|
+
defaults:
|
|
8
|
+
Redis: null
|
|
9
|
+
clientOptions: {}
|
|
10
|
+
clusterNodes: null
|
|
11
|
+
client: null
|
|
12
|
+
Promise: Promise
|
|
13
|
+
Events: null
|
|
14
|
+
|
|
15
|
+
constructor: (options={}) ->
|
|
16
|
+
parser.load options, @defaults, @
|
|
17
|
+
@Redis ?= eval("require")("ioredis") # Obfuscated or else Webpack/Angular will try to inline the optional ioredis module. To override this behavior: pass the ioredis module to Taskpace as the 'Redis' option.
|
|
18
|
+
@Events ?= new Events @
|
|
19
|
+
@terminated = false
|
|
20
|
+
|
|
21
|
+
if @clusterNodes?
|
|
22
|
+
@client = new @Redis.Cluster @clusterNodes, @clientOptions
|
|
23
|
+
@subscriber = new @Redis.Cluster @clusterNodes, @clientOptions
|
|
24
|
+
else if @client? and !@client.duplicate?
|
|
25
|
+
@subscriber = new @Redis.Cluster @client.startupNodes, @client.options
|
|
26
|
+
else
|
|
27
|
+
@client ?= new @Redis @clientOptions
|
|
28
|
+
@subscriber = @client.duplicate()
|
|
29
|
+
@limiters = {}
|
|
30
|
+
|
|
31
|
+
@ready = @Promise.all [@_setup(@client, false), @_setup(@subscriber, true)]
|
|
32
|
+
.then =>
|
|
33
|
+
@_loadScripts()
|
|
34
|
+
{ @client, @subscriber }
|
|
35
|
+
|
|
36
|
+
_setup: (client, sub) ->
|
|
37
|
+
client.setMaxListeners 0
|
|
38
|
+
new @Promise (resolve, reject) =>
|
|
39
|
+
client.on "error", (e) => @Events.trigger "error", e
|
|
40
|
+
if sub
|
|
41
|
+
client.on "message", (channel, message) =>
|
|
42
|
+
@limiters[channel]?._store.onMessage channel, message
|
|
43
|
+
if client.status == "ready" then resolve()
|
|
44
|
+
else client.once "ready", resolve
|
|
45
|
+
|
|
46
|
+
_loadScripts: -> Scripts.names.forEach (name) => @client.defineCommand name, { lua: Scripts.payload(name) }
|
|
47
|
+
|
|
48
|
+
__runCommand__: (cmd) ->
|
|
49
|
+
await @ready
|
|
50
|
+
[[_, deleted]] = await @client.pipeline([cmd]).exec()
|
|
51
|
+
deleted
|
|
52
|
+
|
|
53
|
+
__addLimiter__: (instance) ->
|
|
54
|
+
@Promise.all [instance.channel(), instance.channel_client()].map (channel) =>
|
|
55
|
+
new @Promise (resolve, reject) =>
|
|
56
|
+
@subscriber.subscribe channel, =>
|
|
57
|
+
@limiters[channel] = instance
|
|
58
|
+
resolve()
|
|
59
|
+
|
|
60
|
+
__removeLimiter__: (instance) ->
|
|
61
|
+
[instance.channel(), instance.channel_client()].forEach (channel) =>
|
|
62
|
+
await @subscriber.unsubscribe channel unless @terminated
|
|
63
|
+
delete @limiters[channel]
|
|
64
|
+
|
|
65
|
+
__scriptArgs__: (name, id, args, cb) ->
|
|
66
|
+
keys = Scripts.keys name, id
|
|
67
|
+
[keys.length].concat keys, args, cb
|
|
68
|
+
|
|
69
|
+
__scriptFn__: (name) ->
|
|
70
|
+
@client[name].bind(@client)
|
|
71
|
+
|
|
72
|
+
disconnect: (flush=true) ->
|
|
73
|
+
clearInterval(@limiters[k]._store.heartbeat) for k in Object.keys @limiters
|
|
74
|
+
@limiters = {}
|
|
75
|
+
@terminated = true
|
|
76
|
+
|
|
77
|
+
if flush
|
|
78
|
+
@Promise.all [@client.quit(), @subscriber.quit()]
|
|
79
|
+
else
|
|
80
|
+
@client.disconnect()
|
|
81
|
+
@subscriber.disconnect()
|
|
82
|
+
@Promise.resolve()
|
|
83
|
+
|
|
84
|
+
module.exports = IORedisConnection
|
package/src/Job.coffee
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
NUM_PRIORITIES = 10
|
|
2
|
+
DEFAULT_PRIORITY = 5
|
|
3
|
+
|
|
4
|
+
parser = require "./parser"
|
|
5
|
+
TaskpaceError = require "./TaskpaceError"
|
|
6
|
+
|
|
7
|
+
class Job
|
|
8
|
+
constructor: (@task, @args, options, jobDefaults, @rejectOnDrop, @Events, @_states, @Promise) ->
|
|
9
|
+
@options = parser.load options, jobDefaults
|
|
10
|
+
@options.priority = @_sanitizePriority @options.priority
|
|
11
|
+
if @options.id == jobDefaults.id then @options.id = "#{@options.id}-#{@_randomIndex()}"
|
|
12
|
+
@promise = new @Promise (@_resolve, @_reject) =>
|
|
13
|
+
@retryCount = 0
|
|
14
|
+
|
|
15
|
+
_sanitizePriority: (priority) ->
|
|
16
|
+
sProperty = if ~~priority != priority then DEFAULT_PRIORITY else priority
|
|
17
|
+
if sProperty < 0 then 0 else if sProperty > NUM_PRIORITIES-1 then NUM_PRIORITIES-1 else sProperty
|
|
18
|
+
|
|
19
|
+
_randomIndex: -> Math.random().toString(36).slice(2)
|
|
20
|
+
|
|
21
|
+
doDrop: ({ error, message="This job has been dropped by Taskpace" } = {}) ->
|
|
22
|
+
if @_states.remove @options.id
|
|
23
|
+
if @rejectOnDrop then @_reject (error ? new TaskpaceError message)
|
|
24
|
+
@Events.trigger "dropped", { @args, @options, @task, @promise }
|
|
25
|
+
true
|
|
26
|
+
else
|
|
27
|
+
false
|
|
28
|
+
|
|
29
|
+
_assertStatus: (expected) ->
|
|
30
|
+
status = @_states.jobStatus @options.id
|
|
31
|
+
if not (status == expected or (expected == "DONE" and status == null))
|
|
32
|
+
throw new TaskpaceError "Invalid job status #{status}, expected #{expected}. Please open an issue at https://github.com/PaceJS/taskpace/issues"
|
|
33
|
+
|
|
34
|
+
doReceive: () ->
|
|
35
|
+
@_states.start @options.id
|
|
36
|
+
@Events.trigger "received", { @args, @options }
|
|
37
|
+
|
|
38
|
+
doQueue: (reachedHWM, blocked) ->
|
|
39
|
+
@_assertStatus "RECEIVED"
|
|
40
|
+
@_states.next @options.id
|
|
41
|
+
@Events.trigger "queued", { @args, @options, reachedHWM, blocked }
|
|
42
|
+
|
|
43
|
+
doRun: () ->
|
|
44
|
+
if @retryCount == 0
|
|
45
|
+
@_assertStatus "QUEUED"
|
|
46
|
+
@_states.next @options.id
|
|
47
|
+
else @_assertStatus "EXECUTING"
|
|
48
|
+
@Events.trigger "scheduled", { @args, @options }
|
|
49
|
+
|
|
50
|
+
doExecute: (chained, clearGlobalState, run, free) ->
|
|
51
|
+
if @retryCount == 0
|
|
52
|
+
@_assertStatus "RUNNING"
|
|
53
|
+
@_states.next @options.id
|
|
54
|
+
else @_assertStatus "EXECUTING"
|
|
55
|
+
eventInfo = { @args, @options, @retryCount }
|
|
56
|
+
@Events.trigger "executing", eventInfo
|
|
57
|
+
|
|
58
|
+
try
|
|
59
|
+
passed = await if chained?
|
|
60
|
+
chained.schedule @options, @task, @args...
|
|
61
|
+
else @task @args...
|
|
62
|
+
|
|
63
|
+
if clearGlobalState()
|
|
64
|
+
@doDone eventInfo
|
|
65
|
+
await free @options, eventInfo
|
|
66
|
+
@_assertStatus "DONE"
|
|
67
|
+
@_resolve passed
|
|
68
|
+
catch error
|
|
69
|
+
@_onFailure error, eventInfo, clearGlobalState, run, free
|
|
70
|
+
|
|
71
|
+
doExpire: (clearGlobalState, run, free) ->
|
|
72
|
+
if @_states.jobStatus @options.id == "RUNNING"
|
|
73
|
+
@_states.next @options.id
|
|
74
|
+
@_assertStatus "EXECUTING"
|
|
75
|
+
eventInfo = { @args, @options, @retryCount }
|
|
76
|
+
error = new TaskpaceError "This job timed out after #{@options.expiration} ms."
|
|
77
|
+
@_onFailure error, eventInfo, clearGlobalState, run, free
|
|
78
|
+
|
|
79
|
+
_onFailure: (error, eventInfo, clearGlobalState, run, free) ->
|
|
80
|
+
if clearGlobalState()
|
|
81
|
+
retry = await @Events.trigger "failed", error, eventInfo
|
|
82
|
+
if retry?
|
|
83
|
+
retryAfter = ~~retry
|
|
84
|
+
@Events.trigger "retry", "Retrying #{@options.id} after #{retryAfter} ms", eventInfo
|
|
85
|
+
@retryCount++
|
|
86
|
+
run retryAfter
|
|
87
|
+
else
|
|
88
|
+
@doDone eventInfo
|
|
89
|
+
await free @options, eventInfo
|
|
90
|
+
@_assertStatus "DONE"
|
|
91
|
+
@_reject error
|
|
92
|
+
|
|
93
|
+
doDone: (eventInfo) ->
|
|
94
|
+
@_assertStatus "EXECUTING"
|
|
95
|
+
@_states.next @options.id
|
|
96
|
+
@Events.trigger "done", eventInfo
|
|
97
|
+
|
|
98
|
+
module.exports = Job
|