nexia_worker_roulette 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/worker_roulette/tradesman.rb +60 -0
- data/lib/worker_roulette/version.rb +1 -1
- data/spec/integration/worker_roulette_spec.rb +26 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0f447f3680d7e1b31c9bb64288bfcd272e06e0b
|
4
|
+
data.tar.gz: 37a831cc363e6ecab5aee296d4fb0790e05f2900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f60bf31b75564128c79229d3504e4b7f1b22d79f20be3b9c4ed9845685c917d7fd7c478b277aead096a58dc4d2355f11b855edb411de12b2a130ba2bbb7bc0ab
|
7
|
+
data.tar.gz: 268b3765ffe7bc6dba03668a6a43184607640dfdd4a8376b157374bbbadc4a35c443f0155bc7e961c97f923aaf51dc377e004e89e8d6076924142ce0af4823c2
|
@@ -65,6 +65,43 @@ module WorkerRoulette
|
|
65
65
|
return drain_work_orders(job_board_key, last_sender_key, empty_string)
|
66
66
|
HERE
|
67
67
|
|
68
|
+
LUA_DRAIN_WORK_ORDERS_FOR_SENDER = <<-THERE
|
69
|
+
local empty_string = ""
|
70
|
+
local job_board_key = KEYS[1]
|
71
|
+
local sender_key = KEYS[2] or empty_string
|
72
|
+
local redis_call = redis.call
|
73
|
+
local lock_key_prefix = "L*:"
|
74
|
+
local lock_value = 1
|
75
|
+
local get = "GET"
|
76
|
+
local del = "DEL"
|
77
|
+
local lrange = "LRANGE"
|
78
|
+
local zrange = "ZRANGE"
|
79
|
+
local zrem = "ZREM"
|
80
|
+
|
81
|
+
local function drain_work_orders_for_sender(job_board_key, sender_key)
|
82
|
+
|
83
|
+
if sender_key == empty_string then
|
84
|
+
return {empty_string, {}, 0}
|
85
|
+
end
|
86
|
+
|
87
|
+
local lock_key = lock_key_prefix .. sender_key
|
88
|
+
was_locked = redis_call(get, lock_key)
|
89
|
+
|
90
|
+
if was_locked == 1 then
|
91
|
+
local work_orders = redis_call(lrange, sender_key, 0, -1)
|
92
|
+
redis_call(del, sender_key)
|
93
|
+
|
94
|
+
redis_call(zrem, job_board_key, sender_key)
|
95
|
+
|
96
|
+
return { sender_key, work_orders }
|
97
|
+
else
|
98
|
+
return { sender_key, {} }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
return drain_work_orders_for_sender(job_board_key, sender_key)
|
103
|
+
THERE
|
104
|
+
|
68
105
|
def initialize(redis_pool, evented, namespace = nil, polling_time = WorkerRoulette::DEFAULT_POLLING_TIME)
|
69
106
|
@evented = evented
|
70
107
|
@polling_time = polling_time
|
@@ -99,6 +136,29 @@ module WorkerRoulette
|
|
99
136
|
end
|
100
137
|
end
|
101
138
|
|
139
|
+
def get_more_work_for_last_sender(&on_message_callback)
|
140
|
+
return unless on_message_callback
|
141
|
+
more_work_orders! do |work|
|
142
|
+
on_message_callback.call(work) if work.any?
|
143
|
+
if @evented
|
144
|
+
evented_drain_work_queue!(&on_message_callback)
|
145
|
+
else
|
146
|
+
non_evented_drain_work_queue!(&on_message_callback)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def more_work_orders!(&callback)
|
152
|
+
@lua.call(LUA_DRAIN_WORK_ORDERS_FOR_SENDER, [job_board_key, @last_sender]) do |results|
|
153
|
+
sender_key = results[0]
|
154
|
+
raise "wrong sender key returned from LUA_DRAIN_WORK_ORDERS_FOR_SENDER" unless sender_key == @last_sender
|
155
|
+
work_orders = results[1]
|
156
|
+
work = work_orders.map {|work_order| WorkerRoulette.load(work_order)}
|
157
|
+
callback.call work if callback
|
158
|
+
work
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
102
162
|
def job_board_key
|
103
163
|
@job_board_key ||= WorkerRoulette.job_board_key(@namespace)
|
104
164
|
end
|
@@ -1,5 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
module WorkerRoulette
|
3
|
+
class Tradesman
|
4
|
+
attr_reader :lua
|
5
|
+
end
|
6
|
+
|
3
7
|
describe WorkerRoulette do
|
4
8
|
let(:sender) {'katie_80'}
|
5
9
|
let(:work_orders) {["hello", "foreman"]}
|
@@ -69,8 +73,8 @@ module WorkerRoulette
|
|
69
73
|
end
|
70
74
|
|
71
75
|
context Tradesman do
|
72
|
-
let(:foreman)
|
73
|
-
|
76
|
+
let(:foreman) { worker_roulette.foreman(sender) }
|
77
|
+
subject(:tradesman) { worker_roulette.tradesman }
|
74
78
|
|
75
79
|
before do
|
76
80
|
foreman.enqueue_work_order(work_orders)
|
@@ -153,6 +157,26 @@ module WorkerRoulette
|
|
153
157
|
end
|
154
158
|
end
|
155
159
|
|
160
|
+
it "goes back to the channel to get more work for the same sender" do
|
161
|
+
tradesman.wait_for_work_orders do |redis_work_orders|
|
162
|
+
expect(redis_work_orders).to eq([work_orders_with_headers])
|
163
|
+
expect(tradesman.last_sender).to eq('katie_80')
|
164
|
+
allow(tradesman).to receive(:wait_for_work_orders)
|
165
|
+
end
|
166
|
+
|
167
|
+
expect(tradesman.lua).to receive(:call).with(Tradesman::LUA_DRAIN_WORK_ORDERS_FOR_SENDER, [instance_of(String), sender])
|
168
|
+
tradesman.get_more_work_for_last_sender do |redis_work_orders|
|
169
|
+
expect(redis_work_orders).to eq([])
|
170
|
+
end
|
171
|
+
|
172
|
+
foreman.enqueue_work_order("more_work_orders")
|
173
|
+
expect(tradesman.lua).to receive(:call).with(Tradesman::LUA_DRAIN_WORK_ORDERS_FOR_SENDER, [instance_of(String), sender])
|
174
|
+
tradesman.get_more_work_for_last_sender do |redis_work_orders|
|
175
|
+
expect(redis_work_orders).to eq(["more_work_orders"])
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
156
180
|
context "Failure" do
|
157
181
|
it "should not put the sender_id and work_orders back if processing fails bc new work_orders may have been processed while that process failed" do; end
|
158
182
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nexia_worker_roulette
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Saieg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|