worker_roulette 0.1.3 → 0.1.4
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.
@@ -45,23 +45,24 @@ module WorkerRoulette
|
|
45
45
|
|
46
46
|
local work_order = ARGV[1]
|
47
47
|
local job_notification = ARGV[2]
|
48
|
+
local redis_call = redis.call
|
49
|
+
local zscore = 'ZSCORE'
|
50
|
+
local incr = 'INCR'
|
51
|
+
local zadd = 'ZADD'
|
52
|
+
local rpush = 'RPUSH'
|
53
|
+
local publish = 'PUBLISH'
|
48
54
|
|
49
55
|
local function enqueue_work_orders(work_order, job_notification)
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
if (sender_is_on_job_board == false) then
|
54
|
-
local count = redis.call('INCR', counter_key)
|
55
|
-
local job_added = redis.call('ZADD',job_board_key, count, sender_key)
|
56
|
-
result = sender_key .. ' added'
|
56
|
+
if (redis_call(zscore, job_board_key, sender_key) == false) then
|
57
|
+
local count = redis_call(incr, counter_key)
|
58
|
+
redis_call(zadd, job_board_key, count, sender_key)
|
57
59
|
end
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
-
return result
|
61
|
+
redis_call(rpush,sender_key, work_order)
|
62
|
+
redis_call(publish, channel, job_notification)
|
62
63
|
end
|
63
64
|
|
64
|
-
|
65
|
+
enqueue_work_orders(work_order, job_notification)
|
65
66
|
HERE
|
66
67
|
end
|
67
68
|
end
|
@@ -38,47 +38,56 @@ module WorkerRoulette
|
|
38
38
|
private
|
39
39
|
def self.lua_drain_work_orders
|
40
40
|
<<-HERE
|
41
|
+
local empty_string = ""
|
41
42
|
local job_board_key = KEYS[1]
|
42
|
-
local last_sender_key = KEYS[2]
|
43
|
-
local sender_key = ARGV[1]
|
43
|
+
local last_sender_key = KEYS[2] or empty_string
|
44
|
+
local sender_key = ARGV[1] or empty_string
|
45
|
+
local redis_call = redis.call
|
46
|
+
local lock_key_prefix = "L*:"
|
47
|
+
local lock_value = "L"
|
48
|
+
local ex = "EX"
|
49
|
+
local nx = "NX"
|
50
|
+
local get = "GET"
|
51
|
+
local set = "SET"
|
52
|
+
local del = "DEL"
|
53
|
+
local zrank = "ZRANK"
|
54
|
+
local zrange = "ZRANGE"
|
44
55
|
|
45
56
|
local function drain_work_orders(job_board_key, last_sender_key, sender_key)
|
46
|
-
if last_sender_key ~=
|
47
|
-
local last_sender_lock_key =
|
48
|
-
|
57
|
+
if last_sender_key ~= empty_string then
|
58
|
+
local last_sender_lock_key = lock_key_prefix .. last_sender_key
|
59
|
+
redis_call(del, last_sender_lock_key)
|
49
60
|
end
|
50
61
|
|
51
|
-
if
|
52
|
-
sender_key =
|
53
|
-
if
|
62
|
+
if sender_key == empty_string then
|
63
|
+
sender_key = redis_call(zrange, job_board_key, 0, 0)[1] or empty_string
|
64
|
+
if sender_key == empty_string then
|
54
65
|
return {}
|
55
66
|
end
|
56
67
|
end
|
57
68
|
|
58
|
-
local lock_key =
|
59
|
-
local locked = (
|
69
|
+
local lock_key = lock_key_prefix .. sender_key
|
70
|
+
local locked = (redis_call(get, lock_key) == lock_value)
|
60
71
|
|
61
72
|
if not locked then
|
62
|
-
local results = {}
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
redis.call('ZREM', job_board_key, sender_key)
|
67
|
-
redis.call('SET', lock_key, 'L', 'EX', 1, 'NX')
|
73
|
+
local results = {sender_key, redis_call('LRANGE', sender_key, 0, -1)}
|
74
|
+
redis_call(del, sender_key)
|
75
|
+
redis_call('ZREM', job_board_key, sender_key)
|
76
|
+
redis_call(set, lock_key, lock_value, ex, 1, nx)
|
68
77
|
return results
|
69
78
|
else
|
70
|
-
local sender_index =
|
79
|
+
local sender_index = redis_call(zrank, job_board_key, sender_key)
|
71
80
|
local next_index = sender_index + 1
|
72
|
-
local next_sender_key =
|
81
|
+
local next_sender_key = redis_call(zrange, job_board_key, next_index, next_index)[1]
|
73
82
|
if next_sender_key then
|
74
|
-
return drain_work_orders(job_board_key,
|
83
|
+
return drain_work_orders(job_board_key, empty_string, next_sender_key)
|
75
84
|
else
|
76
85
|
return {}
|
77
86
|
end
|
78
87
|
end
|
79
88
|
end
|
80
89
|
|
81
|
-
return drain_work_orders(job_board_key, last_sender_key,
|
90
|
+
return drain_work_orders(job_board_key, last_sender_key, empty_string)
|
82
91
|
HERE
|
83
92
|
end
|
84
93
|
end
|
@@ -25,7 +25,6 @@ describe WorkerRoulette do
|
|
25
25
|
foreman = WorkerRoulette.a_foreman('foreman')
|
26
26
|
foreman.enqueue_work_order('some old fashion work') do |redis_response, stuff|
|
27
27
|
called = true
|
28
|
-
redis_response.should == 'foreman added'
|
29
28
|
end
|
30
29
|
done(0.1) {called.should == true}
|
31
30
|
end
|