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.

Files changed (99) hide show
  1. package/.babelrc.es5 +5 -0
  2. package/.babelrc.lib +9 -0
  3. package/.env +2 -0
  4. package/.travis.yml +25 -0
  5. package/LICENSE +20 -0
  6. package/README.md +474 -2
  7. package/bower.json +30 -0
  8. package/es5.js +5064 -0
  9. package/lib/Batcher.js +1 -0
  10. package/lib/DLList.js +1 -0
  11. package/lib/Events.js +1 -0
  12. package/lib/Group.js +1 -0
  13. package/lib/IORedisConnection.js +1 -0
  14. package/lib/Job.js +1 -0
  15. package/lib/LocalDatastore.js +1 -0
  16. package/lib/Queues.js +1 -0
  17. package/lib/RedisConnection.js +1 -0
  18. package/lib/RedisDatastore.js +1 -0
  19. package/lib/Scripts.js +1 -0
  20. package/lib/States.js +1 -0
  21. package/lib/Sync.js +1 -0
  22. package/lib/Taskpace.js +1 -0
  23. package/lib/TaskpaceError.js +1 -0
  24. package/lib/common.js +1 -0
  25. package/lib/es5.js +1 -0
  26. package/lib/index.js +1 -0
  27. package/lib/limit.js +1 -0
  28. package/lib/lua.json +24 -0
  29. package/lib/parser.js +1 -0
  30. package/lib/redis.js +1 -0
  31. package/lib/validate.js +1 -0
  32. package/lib/version.json +1 -0
  33. package/light.js +1524 -0
  34. package/package.json +53 -4
  35. package/rollup.config.es5.js +34 -0
  36. package/rollup.config.light.js +44 -0
  37. package/scripts/assemble_lua.js +25 -0
  38. package/scripts/build.sh +81 -0
  39. package/scripts/test_all.sh +20 -0
  40. package/scripts/version.js +3 -0
  41. package/src/Batcher.coffee +39 -0
  42. package/src/DLList.coffee +38 -0
  43. package/src/Events.coffee +38 -0
  44. package/src/Group.coffee +80 -0
  45. package/src/IORedisConnection.coffee +84 -0
  46. package/src/Job.coffee +98 -0
  47. package/src/LocalDatastore.coffee +140 -0
  48. package/src/Queues.coffee +28 -0
  49. package/src/RedisConnection.coffee +91 -0
  50. package/src/RedisDatastore.coffee +158 -0
  51. package/src/Scripts.coffee +151 -0
  52. package/src/States.coffee +43 -0
  53. package/src/Sync.coffee +28 -0
  54. package/src/Taskpace.coffee +298 -0
  55. package/src/TaskpaceError.coffee +3 -0
  56. package/src/es5.coffee +3 -0
  57. package/src/index.coffee +1 -0
  58. package/src/parser.coffee +10 -0
  59. package/src/redis/blacklist_client.lua +8 -0
  60. package/src/redis/check.lua +6 -0
  61. package/src/redis/conditions_check.lua +3 -0
  62. package/src/redis/current_reservoir.lua +1 -0
  63. package/src/redis/done.lua +3 -0
  64. package/src/redis/free.lua +5 -0
  65. package/src/redis/get_time.lua +7 -0
  66. package/src/redis/group_check.lua +1 -0
  67. package/src/redis/heartbeat.lua +1 -0
  68. package/src/redis/increment_reservoir.lua +10 -0
  69. package/src/redis/init.lua +105 -0
  70. package/src/redis/process_tick.lua +214 -0
  71. package/src/redis/queued.lua +10 -0
  72. package/src/redis/refresh_expiration.lua +11 -0
  73. package/src/redis/refs.lua +13 -0
  74. package/src/redis/register.lua +51 -0
  75. package/src/redis/register_client.lua +12 -0
  76. package/src/redis/running.lua +1 -0
  77. package/src/redis/submit.lua +74 -0
  78. package/src/redis/update_settings.lua +14 -0
  79. package/src/redis/validate_client.lua +5 -0
  80. package/src/redis/validate_keys.lua +3 -0
  81. package/taskpace.d.ts +629 -0
  82. package/taskpace.d.ts.ejs +588 -0
  83. package/test/DLList.js +148 -0
  84. package/test/batcher.js +209 -0
  85. package/test/cluster.js +1549 -0
  86. package/test/context.js +142 -0
  87. package/test/general.js +867 -0
  88. package/test/group.js +255 -0
  89. package/test/ioredis.js +135 -0
  90. package/test/node_redis.js +100 -0
  91. package/test/priority.js +184 -0
  92. package/test/promises.js +202 -0
  93. package/test/retries.js +237 -0
  94. package/test/spawn/increaseKeepAlive.js +17 -0
  95. package/test/spawn/refreshKeepAlive.js +17 -0
  96. package/test/states.js +103 -0
  97. package/test/stop.js +208 -0
  98. package/test/taskpace.js +7 -0
  99. package/test.ts +335 -0
@@ -0,0 +1,214 @@
1
+ local process_tick = function (now, always_publish)
2
+
3
+ local compute_capacity = function (maxConcurrent, running, reservoir)
4
+ if maxConcurrent ~= nil and reservoir ~= nil then
5
+ return math.min((maxConcurrent - running), reservoir)
6
+ elseif maxConcurrent ~= nil then
7
+ return maxConcurrent - running
8
+ elseif reservoir ~= nil then
9
+ return reservoir
10
+ else
11
+ return nil
12
+ end
13
+ end
14
+
15
+ local settings = redis.call('hmget', settings_key,
16
+ 'id',
17
+ 'maxConcurrent',
18
+ 'running',
19
+ 'reservoir',
20
+ 'reservoirRefreshInterval',
21
+ 'reservoirRefreshAmount',
22
+ 'lastReservoirRefresh',
23
+ 'reservoirIncreaseInterval',
24
+ 'reservoirIncreaseAmount',
25
+ 'reservoirIncreaseMaximum',
26
+ 'lastReservoirIncrease',
27
+ 'capacityPriorityCounter',
28
+ 'clientTimeout'
29
+ )
30
+ local id = settings[1]
31
+ local maxConcurrent = tonumber(settings[2])
32
+ local running = tonumber(settings[3])
33
+ local reservoir = tonumber(settings[4])
34
+ local reservoirRefreshInterval = tonumber(settings[5])
35
+ local reservoirRefreshAmount = tonumber(settings[6])
36
+ local lastReservoirRefresh = tonumber(settings[7])
37
+ local reservoirIncreaseInterval = tonumber(settings[8])
38
+ local reservoirIncreaseAmount = tonumber(settings[9])
39
+ local reservoirIncreaseMaximum = tonumber(settings[10])
40
+ local lastReservoirIncrease = tonumber(settings[11])
41
+ local capacityPriorityCounter = tonumber(settings[12])
42
+ local clientTimeout = tonumber(settings[13])
43
+
44
+ local initial_capacity = compute_capacity(maxConcurrent, running, reservoir)
45
+
46
+ --
47
+ -- Process 'running' changes
48
+ --
49
+ local expired = redis.call('zrangebyscore', job_expirations_key, '-inf', '('..now)
50
+
51
+ if #expired > 0 then
52
+ redis.call('zremrangebyscore', job_expirations_key, '-inf', '('..now)
53
+
54
+ local flush_batch = function (batch, acc)
55
+ local weights = redis.call('hmget', job_weights_key, unpack(batch))
56
+ redis.call('hdel', job_weights_key, unpack(batch))
57
+ local clients = redis.call('hmget', job_clients_key, unpack(batch))
58
+ redis.call('hdel', job_clients_key, unpack(batch))
59
+
60
+ -- Calculate sum of removed weights
61
+ for i = 1, #weights do
62
+ acc['total'] = acc['total'] + (tonumber(weights[i]) or 0)
63
+ end
64
+
65
+ -- Calculate sum of removed weights by client
66
+ local client_weights = {}
67
+ for i = 1, #clients do
68
+ local removed = tonumber(weights[i]) or 0
69
+ if removed > 0 then
70
+ acc['client_weights'][clients[i]] = (acc['client_weights'][clients[i]] or 0) + removed
71
+ end
72
+ end
73
+ end
74
+
75
+ local acc = {
76
+ ['total'] = 0,
77
+ ['client_weights'] = {}
78
+ }
79
+ local batch_size = 1000
80
+
81
+ -- Compute changes to Zsets and apply changes to Hashes
82
+ for i = 1, #expired, batch_size do
83
+ local batch = {}
84
+ for j = i, math.min(i + batch_size - 1, #expired) do
85
+ table.insert(batch, expired[j])
86
+ end
87
+
88
+ flush_batch(batch, acc)
89
+ end
90
+
91
+ -- Apply changes to Zsets
92
+ if acc['total'] > 0 then
93
+ redis.call('hincrby', settings_key, 'done', acc['total'])
94
+ running = tonumber(redis.call('hincrby', settings_key, 'running', -acc['total']))
95
+ end
96
+
97
+ for client, weight in pairs(acc['client_weights']) do
98
+ redis.call('zincrby', client_running_key, -weight, client)
99
+ end
100
+ end
101
+
102
+ --
103
+ -- Process 'reservoir' changes
104
+ --
105
+ local reservoirRefreshActive = reservoirRefreshInterval ~= nil and reservoirRefreshAmount ~= nil
106
+ if reservoirRefreshActive and now >= lastReservoirRefresh + reservoirRefreshInterval then
107
+ reservoir = reservoirRefreshAmount
108
+ redis.call('hmset', settings_key,
109
+ 'reservoir', reservoir,
110
+ 'lastReservoirRefresh', now
111
+ )
112
+ end
113
+
114
+ local reservoirIncreaseActive = reservoirIncreaseInterval ~= nil and reservoirIncreaseAmount ~= nil
115
+ if reservoirIncreaseActive and now >= lastReservoirIncrease + reservoirIncreaseInterval then
116
+ local num_intervals = math.floor((now - lastReservoirIncrease) / reservoirIncreaseInterval)
117
+ local incr = reservoirIncreaseAmount * num_intervals
118
+ if reservoirIncreaseMaximum ~= nil then
119
+ incr = math.min(incr, reservoirIncreaseMaximum - (reservoir or 0))
120
+ end
121
+ if incr > 0 then
122
+ reservoir = (reservoir or 0) + incr
123
+ end
124
+ redis.call('hmset', settings_key,
125
+ 'reservoir', reservoir,
126
+ 'lastReservoirIncrease', lastReservoirIncrease + (num_intervals * reservoirIncreaseInterval)
127
+ )
128
+ end
129
+
130
+ --
131
+ -- Clear unresponsive clients
132
+ --
133
+ local unresponsive = redis.call('zrangebyscore', client_last_seen_key, '-inf', (now - clientTimeout))
134
+ local unresponsive_lookup = {}
135
+ local terminated_clients = {}
136
+ for i = 1, #unresponsive do
137
+ unresponsive_lookup[unresponsive[i]] = true
138
+ if tonumber(redis.call('zscore', client_running_key, unresponsive[i])) == 0 then
139
+ table.insert(terminated_clients, unresponsive[i])
140
+ end
141
+ end
142
+ if #terminated_clients > 0 then
143
+ redis.call('zrem', client_running_key, unpack(terminated_clients))
144
+ redis.call('hdel', client_num_queued_key, unpack(terminated_clients))
145
+ redis.call('zrem', client_last_registered_key, unpack(terminated_clients))
146
+ redis.call('zrem', client_last_seen_key, unpack(terminated_clients))
147
+ end
148
+
149
+ --
150
+ -- Broadcast capacity changes
151
+ --
152
+ local final_capacity = compute_capacity(maxConcurrent, running, reservoir)
153
+
154
+ if always_publish or (initial_capacity ~= nil and final_capacity == nil) then
155
+ -- always_publish or was not unlimited, now unlimited
156
+ redis.call('publish', 'b_'..id, 'capacity:'..(final_capacity or ''))
157
+
158
+ elseif initial_capacity ~= nil and final_capacity ~= nil and final_capacity > initial_capacity then
159
+ -- capacity was increased
160
+ -- send the capacity message to the limiter having the lowest number of running jobs
161
+ -- the tiebreaker is the limiter having not registered a job in the longest time
162
+
163
+ local lowest_concurrency_value = nil
164
+ local lowest_concurrency_clients = {}
165
+ local lowest_concurrency_last_registered = {}
166
+ local client_concurrencies = redis.call('zrange', client_running_key, 0, -1, 'withscores')
167
+
168
+ for i = 1, #client_concurrencies, 2 do
169
+ local client = client_concurrencies[i]
170
+ local concurrency = tonumber(client_concurrencies[i+1])
171
+
172
+ if (
173
+ lowest_concurrency_value == nil or lowest_concurrency_value == concurrency
174
+ ) and (
175
+ not unresponsive_lookup[client]
176
+ ) and (
177
+ tonumber(redis.call('hget', client_num_queued_key, client)) > 0
178
+ ) then
179
+ lowest_concurrency_value = concurrency
180
+ table.insert(lowest_concurrency_clients, client)
181
+ local last_registered = tonumber(redis.call('zscore', client_last_registered_key, client))
182
+ table.insert(lowest_concurrency_last_registered, last_registered)
183
+ end
184
+ end
185
+
186
+ if #lowest_concurrency_clients > 0 then
187
+ local position = 1
188
+ local earliest = lowest_concurrency_last_registered[1]
189
+
190
+ for i,v in ipairs(lowest_concurrency_last_registered) do
191
+ if v < earliest then
192
+ position = i
193
+ earliest = v
194
+ end
195
+ end
196
+
197
+ local next_client = lowest_concurrency_clients[position]
198
+ redis.call('publish', 'b_'..id,
199
+ 'capacity-priority:'..(final_capacity or '')..
200
+ ':'..next_client..
201
+ ':'..capacityPriorityCounter
202
+ )
203
+ redis.call('hincrby', settings_key, 'capacityPriorityCounter', '1')
204
+ else
205
+ redis.call('publish', 'b_'..id, 'capacity:'..(final_capacity or ''))
206
+ end
207
+ end
208
+
209
+ return {
210
+ ['capacity'] = final_capacity,
211
+ ['running'] = running,
212
+ ['reservoir'] = reservoir
213
+ }
214
+ end
@@ -0,0 +1,10 @@
1
+ local clientTimeout = tonumber(redis.call('hget', settings_key, 'clientTimeout'))
2
+ local valid_clients = redis.call('zrangebyscore', client_last_seen_key, (now - clientTimeout), 'inf')
3
+ local client_queued = redis.call('hmget', client_num_queued_key, unpack(valid_clients))
4
+
5
+ local sum = 0
6
+ for i = 1, #client_queued do
7
+ sum = sum + tonumber(client_queued[i])
8
+ end
9
+
10
+ return sum
@@ -0,0 +1,11 @@
1
+ local refresh_expiration = function (now, nextRequest, groupTimeout)
2
+
3
+ if groupTimeout ~= nil then
4
+ local ttl = (nextRequest + groupTimeout) - now
5
+
6
+ for i = 1, #KEYS do
7
+ redis.call('pexpire', KEYS[i], ttl)
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,13 @@
1
+ local settings_key = KEYS[1]
2
+ local job_weights_key = KEYS[2]
3
+ local job_expirations_key = KEYS[3]
4
+ local job_clients_key = KEYS[4]
5
+ local client_running_key = KEYS[5]
6
+ local client_num_queued_key = KEYS[6]
7
+ local client_last_registered_key = KEYS[7]
8
+ local client_last_seen_key = KEYS[8]
9
+
10
+ local now = tonumber(ARGV[1])
11
+ local client = ARGV[2]
12
+
13
+ local num_static_argv = 2
@@ -0,0 +1,51 @@
1
+ local index = ARGV[num_static_argv + 1]
2
+ local weight = tonumber(ARGV[num_static_argv + 2])
3
+ local expiration = tonumber(ARGV[num_static_argv + 3])
4
+
5
+ local state = process_tick(now, false)
6
+ local capacity = state['capacity']
7
+ local reservoir = state['reservoir']
8
+
9
+ local settings = redis.call('hmget', settings_key,
10
+ 'nextRequest',
11
+ 'minTime',
12
+ 'groupTimeout'
13
+ )
14
+ local nextRequest = tonumber(settings[1])
15
+ local minTime = tonumber(settings[2])
16
+ local groupTimeout = tonumber(settings[3])
17
+
18
+ if conditions_check(capacity, weight) then
19
+
20
+ redis.call('hincrby', settings_key, 'running', weight)
21
+ redis.call('hset', job_weights_key, index, weight)
22
+ if expiration ~= nil then
23
+ redis.call('zadd', job_expirations_key, now + expiration, index)
24
+ end
25
+ redis.call('hset', job_clients_key, index, client)
26
+ redis.call('zincrby', client_running_key, weight, client)
27
+ redis.call('hincrby', client_num_queued_key, client, -1)
28
+ redis.call('zadd', client_last_registered_key, now, client)
29
+
30
+ local wait = math.max(nextRequest - now, 0)
31
+ local newNextRequest = now + wait + minTime
32
+
33
+ if reservoir == nil then
34
+ redis.call('hset', settings_key,
35
+ 'nextRequest', newNextRequest
36
+ )
37
+ else
38
+ reservoir = reservoir - weight
39
+ redis.call('hmset', settings_key,
40
+ 'reservoir', reservoir,
41
+ 'nextRequest', newNextRequest
42
+ )
43
+ end
44
+
45
+ refresh_expiration(now, newNextRequest, groupTimeout)
46
+
47
+ return {true, wait, reservoir}
48
+
49
+ else
50
+ return {false}
51
+ end
@@ -0,0 +1,12 @@
1
+ local queued = tonumber(ARGV[num_static_argv + 1])
2
+
3
+ -- Could have been re-registered concurrently
4
+ if not redis.call('zscore', client_last_seen_key, client) then
5
+ redis.call('zadd', client_running_key, 0, client)
6
+ redis.call('hset', client_num_queued_key, client, queued)
7
+ redis.call('zadd', client_last_registered_key, 0, client)
8
+ end
9
+
10
+ redis.call('zadd', client_last_seen_key, now, client)
11
+
12
+ return {}
@@ -0,0 +1 @@
1
+ return process_tick(now, false)['running']
@@ -0,0 +1,74 @@
1
+ local queueLength = tonumber(ARGV[num_static_argv + 1])
2
+ local weight = tonumber(ARGV[num_static_argv + 2])
3
+
4
+ local capacity = process_tick(now, false)['capacity']
5
+
6
+ local settings = redis.call('hmget', settings_key,
7
+ 'id',
8
+ 'maxConcurrent',
9
+ 'highWater',
10
+ 'nextRequest',
11
+ 'strategy',
12
+ 'unblockTime',
13
+ 'penalty',
14
+ 'minTime',
15
+ 'groupTimeout'
16
+ )
17
+ local id = settings[1]
18
+ local maxConcurrent = tonumber(settings[2])
19
+ local highWater = tonumber(settings[3])
20
+ local nextRequest = tonumber(settings[4])
21
+ local strategy = tonumber(settings[5])
22
+ local unblockTime = tonumber(settings[6])
23
+ local penalty = tonumber(settings[7])
24
+ local minTime = tonumber(settings[8])
25
+ local groupTimeout = tonumber(settings[9])
26
+
27
+ if maxConcurrent ~= nil and weight > maxConcurrent then
28
+ return redis.error_reply('OVERWEIGHT:'..weight..':'..maxConcurrent)
29
+ end
30
+
31
+ local reachedHWM = (highWater ~= nil and queueLength == highWater
32
+ and not (
33
+ conditions_check(capacity, weight)
34
+ and nextRequest - now <= 0
35
+ )
36
+ )
37
+
38
+ local blocked = strategy == 3 and (reachedHWM or unblockTime >= now)
39
+
40
+ if blocked then
41
+ local computedPenalty = penalty
42
+ if computedPenalty == nil then
43
+ if minTime == 0 then
44
+ computedPenalty = 5000
45
+ else
46
+ computedPenalty = 15 * minTime
47
+ end
48
+ end
49
+
50
+ local newNextRequest = now + computedPenalty + minTime
51
+
52
+ redis.call('hmset', settings_key,
53
+ 'unblockTime', now + computedPenalty,
54
+ 'nextRequest', newNextRequest
55
+ )
56
+
57
+ local clients_queued_reset = redis.call('hkeys', client_num_queued_key)
58
+ local queued_reset = {}
59
+ for i = 1, #clients_queued_reset do
60
+ table.insert(queued_reset, clients_queued_reset[i])
61
+ table.insert(queued_reset, 0)
62
+ end
63
+ redis.call('hmset', client_num_queued_key, unpack(queued_reset))
64
+
65
+ redis.call('publish', 'b_'..id, 'blocked:')
66
+
67
+ refresh_expiration(now, newNextRequest, groupTimeout)
68
+ end
69
+
70
+ if not blocked and not reachedHWM then
71
+ redis.call('hincrby', client_num_queued_key, client, 1)
72
+ end
73
+
74
+ return {reachedHWM, blocked, strategy}
@@ -0,0 +1,14 @@
1
+ local args = {'hmset', settings_key}
2
+
3
+ for i = num_static_argv + 1, #ARGV do
4
+ table.insert(args, ARGV[i])
5
+ end
6
+
7
+ redis.call(unpack(args))
8
+
9
+ process_tick(now, true)
10
+
11
+ local groupTimeout = tonumber(redis.call('hget', settings_key, 'groupTimeout'))
12
+ refresh_expiration(0, 0, groupTimeout)
13
+
14
+ return {}
@@ -0,0 +1,5 @@
1
+ if not redis.call('zscore', client_last_seen_key, client) then
2
+ return redis.error_reply('UNKNOWN_CLIENT')
3
+ end
4
+
5
+ redis.call('zadd', client_last_seen_key, now, client)
@@ -0,0 +1,3 @@
1
+ if not (redis.call('exists', settings_key) == 1) then
2
+ return redis.error_reply('SETTINGS_KEY_NOT_FOUND')
3
+ end