fairway 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/fairway/queue_reader.rb +4 -0
- data/lib/fairway/sidekiq/queue_fetch.rb +6 -1
- data/lib/fairway/version.rb +1 -1
- data/redis/fairway_deliver.lua +1 -0
- data/redis/fairway_pull.lua +4 -0
- data/spec/lib/fairway/connection_spec.rb +9 -15
- data/spec/lib/fairway/queue_reader_spec.rb +30 -11
- data/spec/lib/fairway/sidekiq/queue_fetch_spec.rb +5 -5
- data/spec/spec_helper.rb +2 -1
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/lib/fairway/queue_reader.rb
CHANGED
@@ -16,7 +16,12 @@ module Fairway
|
|
16
16
|
|
17
17
|
if work
|
18
18
|
decoded_work = JSON.parse(work)
|
19
|
-
|
19
|
+
|
20
|
+
if @message_to_job
|
21
|
+
decoded_work = @message_to_job.call(fairway_queue, decoded_work)
|
22
|
+
work = decoded_work.to_json
|
23
|
+
end
|
24
|
+
|
20
25
|
unit_of_work = UnitOfWork.new(decoded_work["queue"], work)
|
21
26
|
end
|
22
27
|
|
data/lib/fairway/version.rb
CHANGED
data/redis/fairway_deliver.lua
CHANGED
@@ -15,6 +15,7 @@ for i = 1, #registered_queues, 2 do
|
|
15
15
|
local facet_queue = namespace .. queue_name .. ':facet_queue';
|
16
16
|
|
17
17
|
redis.call('lpush', namespace .. queue_name .. ':' .. facet, message)
|
18
|
+
redis.call('incr', namespace .. queue_name .. ':length');
|
18
19
|
|
19
20
|
if redis.call('sadd', active_facets, facet) == 1 then
|
20
21
|
redis.call('lpush', facet_queue, facet);
|
data/redis/fairway_pull.lua
CHANGED
@@ -10,6 +10,10 @@ for index, queue_name in ipairs(ARGV) do
|
|
10
10
|
local message_queue = namespace .. queue_name .. ':' .. facet;
|
11
11
|
local message = redis.call('rpop', message_queue);
|
12
12
|
|
13
|
+
if message then
|
14
|
+
redis.call('decr', namespace .. queue_name .. ':length');
|
15
|
+
end
|
16
|
+
|
13
17
|
if redis.call('llen', message_queue) == 0 then
|
14
18
|
redis.call('srem', active_facets, facet);
|
15
19
|
else
|
@@ -2,24 +2,18 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module Fairway
|
4
4
|
describe Connection do
|
5
|
-
let(:config)
|
6
|
-
|
7
|
-
c.facet { |message| message[:facet] }
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:connection) { Connection.new(config) }
|
12
|
-
let(:redis) { config.redis }
|
5
|
+
let(:connection) { Connection.new(Fairway.config) }
|
6
|
+
let(:redis) { Fairway.config.redis }
|
13
7
|
let(:message) { { facet: 1, topic: "event:helloworld" } }
|
14
8
|
|
15
9
|
describe "#initialize" do
|
16
10
|
it "registers queues from the config" do
|
17
|
-
config
|
18
|
-
config.
|
19
|
-
|
20
|
-
Connection.new(config)
|
11
|
+
Fairway.config.register_queue("myqueue", ".*")
|
12
|
+
Fairway.config.redis.hgetall("registered_queues").should == {}
|
13
|
+
|
14
|
+
Connection.new(Fairway.config)
|
21
15
|
|
22
|
-
config.redis.hgetall("registered_queues").should == {
|
16
|
+
Fairway.config.redis.hgetall("registered_queues").should == {
|
23
17
|
"myqueue" => ".*"
|
24
18
|
}
|
25
19
|
end
|
@@ -40,7 +34,7 @@ module Fairway
|
|
40
34
|
|
41
35
|
on.pmessage do |pattern, channel, received_message|
|
42
36
|
received_message.should == message.to_json
|
43
|
-
channel.should == "default"
|
37
|
+
channel.should == "test:fairway:default"
|
44
38
|
redis.punsubscribe(pattern)
|
45
39
|
end
|
46
40
|
end
|
@@ -48,7 +42,7 @@ module Fairway
|
|
48
42
|
|
49
43
|
context "registered queue exists for message type" do
|
50
44
|
before do
|
51
|
-
config.register_queue("myqueue")
|
45
|
+
Fairway.config.register_queue("myqueue")
|
52
46
|
end
|
53
47
|
|
54
48
|
it "adds message to the environment facet for the queue" do
|
@@ -2,13 +2,8 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module Fairway
|
4
4
|
describe QueueReader do
|
5
|
-
let(:config) do
|
6
|
-
Config.new do |c|
|
7
|
-
c.facet { |message| message[:facet] }
|
8
|
-
end
|
9
|
-
end
|
10
5
|
let(:connection) do
|
11
|
-
c = Connection.new(config)
|
6
|
+
c = Connection.new(Fairway.config)
|
12
7
|
ChanneledConnection.new(c) do |message|
|
13
8
|
message[:topic]
|
14
9
|
end
|
@@ -21,9 +16,33 @@ module Fairway
|
|
21
16
|
end
|
22
17
|
end
|
23
18
|
|
19
|
+
describe "#length" do
|
20
|
+
let(:reader) { QueueReader.new(connection, "myqueue") }
|
21
|
+
|
22
|
+
before do
|
23
|
+
Fairway.config.register_queue("myqueue", "event:helloworld")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns the number of queued messages across facets" do
|
27
|
+
reader.length.should == 0
|
28
|
+
|
29
|
+
connection.deliver(message.merge(facet: 1, message: 1))
|
30
|
+
connection.deliver(message.merge(facet: 1, message: 2))
|
31
|
+
connection.deliver(message.merge(facet: 2, message: 3))
|
32
|
+
|
33
|
+
reader.length.should == 3
|
34
|
+
|
35
|
+
reader.pull
|
36
|
+
reader.pull
|
37
|
+
reader.pull
|
38
|
+
|
39
|
+
reader.length.should == 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
24
43
|
describe "#pull" do
|
25
44
|
before do
|
26
|
-
config.register_queue("myqueue", "event:helloworld")
|
45
|
+
Fairway.config.register_queue("myqueue", "event:helloworld")
|
27
46
|
end
|
28
47
|
|
29
48
|
it "pulls a message off the queue using FIFO strategy" do
|
@@ -49,10 +68,10 @@ module Fairway
|
|
49
68
|
it "removes facet from active list if it becomes empty" do
|
50
69
|
connection.deliver(message)
|
51
70
|
|
52
|
-
config.redis.smembers("myqueue:active_facets").should == ["1"]
|
71
|
+
Fairway.config.redis.smembers("myqueue:active_facets").should == ["1"]
|
53
72
|
reader = QueueReader.new(connection, "myqueue")
|
54
73
|
reader.pull
|
55
|
-
config.redis.smembers("myqueue:active_facets").should be_empty
|
74
|
+
Fairway.config.redis.smembers("myqueue:active_facets").should be_empty
|
56
75
|
end
|
57
76
|
|
58
77
|
it "returns nil if there are no messages to retrieve" do
|
@@ -65,8 +84,8 @@ module Fairway
|
|
65
84
|
|
66
85
|
context "pulling from multiple queues" do
|
67
86
|
before do
|
68
|
-
config.register_queue("myqueue1", "event:1")
|
69
|
-
config.register_queue("myqueue2", "event:2")
|
87
|
+
Fairway.config.register_queue("myqueue1", "event:1")
|
88
|
+
Fairway.config.register_queue("myqueue2", "event:2")
|
70
89
|
end
|
71
90
|
|
72
91
|
it "pulls messages off first queue with a message" do
|
@@ -18,17 +18,17 @@ module Fairway
|
|
18
18
|
|
19
19
|
it "allows transforming of the message into a job" do
|
20
20
|
fetch = QueueFetch.new(reader) do |fairway_queue, message|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
{
|
22
|
+
"queue" => "my_#{message["queue"]}",
|
23
|
+
"class" => "GolfEventJob"
|
24
|
+
}
|
25
25
|
end
|
26
26
|
|
27
27
|
reader.stub(pull: ["fairway", work])
|
28
28
|
|
29
29
|
unit_of_work = fetch.retrieve_work
|
30
30
|
unit_of_work.queue_name.should == "my_golf_events"
|
31
|
-
unit_of_work.message.should ==
|
31
|
+
unit_of_work.message.should == { "queue" => "my_golf_events", "class" => "GolfEventJob" }.to_json
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,7 +23,8 @@ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f}
|
|
23
23
|
RSpec.configure do |config|
|
24
24
|
config.before(:each) do
|
25
25
|
Fairway.configure do |config|
|
26
|
-
config.namespace = "test:
|
26
|
+
config.namespace = "test:fairway"
|
27
|
+
config.facet { |message| message[:facet] }
|
27
28
|
end
|
28
29
|
|
29
30
|
Fairway::Config.new.redis.flushdb
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fairway
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|