coordinator 0.0.9 → 0.0.10
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.
- checksums.yaml +4 -4
- data/lib/coordinator/base.rb +10 -12
- data/lib/coordinator/queue.rb +15 -40
- data/lib/coordinator/redis_queue.rb +2 -2
- data/lib/coordinator/version.rb +1 -1
- data/test/unit/base_test.rb +28 -0
- data/test/unit/queue_test.rb +7 -40
- data/test/unit/redis_queue_test.rb +10 -0
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 951f9d028d3168801aa1c5290154533a3d8052f1
|
4
|
+
data.tar.gz: 830fa381045f0024b419320258992b1896a0b8d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c522e99cdf40060bf51807190858eb825d3cc8ca3388940460511c9cf7a17f5fbc6d0e8a21e548eadcf09ea9d8bd1271ba5b9f41f480d930275053c361727c8
|
7
|
+
data.tar.gz: f814051065ef4f6d32149f5529538ec06345ca5157c21e1ff27db8d197111614f9cc94c6bae332847346600d558af3a9e48b9bd05e1ebba148c956947510b250
|
data/lib/coordinator/base.rb
CHANGED
@@ -5,15 +5,15 @@ module Coordinator
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def add_task(skill, task)
|
8
|
-
queue_for_skill(skill).
|
8
|
+
queue_for_skill(skill).push(task)
|
9
9
|
end
|
10
10
|
|
11
11
|
def add_priority_task(skill, task)
|
12
|
-
queue_for_skill(skill).
|
12
|
+
queue_for_skill(skill).left_push(task)
|
13
13
|
end
|
14
14
|
|
15
15
|
def remove_task(skill, task)
|
16
|
-
queue_for_skill(skill).
|
16
|
+
queue_for_skill(skill).remove(task)
|
17
17
|
end
|
18
18
|
|
19
19
|
def next_task(skills)
|
@@ -25,17 +25,15 @@ module Coordinator
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def set_capacity(skill, capacity)
|
28
|
-
queue_for_skill(skill).
|
28
|
+
queue_for_skill(skill).capacity = capacity
|
29
|
+
end
|
30
|
+
|
31
|
+
def full?(skill)
|
32
|
+
queue_for_skill(skill).full?
|
29
33
|
end
|
30
34
|
|
31
35
|
def info(skill)
|
32
|
-
|
33
|
-
{
|
34
|
-
"name" => queue.name,
|
35
|
-
"capacity" => queue.capacity,
|
36
|
-
"count" => queue.length,
|
37
|
-
"items" => queue.items
|
38
|
-
}
|
36
|
+
queue_for_skill(skill).details
|
39
37
|
end
|
40
38
|
|
41
39
|
def length_all
|
@@ -49,7 +47,7 @@ module Coordinator
|
|
49
47
|
private
|
50
48
|
|
51
49
|
def queue_for_skill(skill)
|
52
|
-
queue = @queues.find {|q| q.
|
50
|
+
queue = @queues.find {|q| q.skill == skill}
|
53
51
|
raise Coordinator::Error, "No matching queue for #{skill}" unless queue
|
54
52
|
queue
|
55
53
|
end
|
data/lib/coordinator/queue.rb
CHANGED
@@ -1,28 +1,19 @@
|
|
1
1
|
module Coordinator
|
2
|
-
class Queue
|
2
|
+
class Queue < RedisQueue
|
3
|
+
attr_reader :skill
|
4
|
+
|
3
5
|
def initialize(skill, capacity=nil, &block)
|
4
6
|
@skill = skill
|
5
|
-
|
6
|
-
@store.capacity = capacity if capacity
|
7
|
+
self.capacity = capacity if capacity
|
7
8
|
@custom_block = block if block_given?
|
8
|
-
end
|
9
9
|
|
10
|
-
|
11
|
-
@store.push(task)
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_priority_task(task)
|
15
|
-
@store.left_push(task)
|
16
|
-
end
|
17
|
-
|
18
|
-
def remove_task(task)
|
19
|
-
@store.remove(task)
|
10
|
+
super(skill)
|
20
11
|
end
|
21
12
|
|
22
13
|
def next_task(skills)
|
23
|
-
task =
|
14
|
+
task = peek
|
24
15
|
return nil unless task && eligible?(task, skills)
|
25
|
-
return task if
|
16
|
+
return task if remove(task)
|
26
17
|
next_task(skills)
|
27
18
|
end
|
28
19
|
|
@@ -34,30 +25,14 @@ module Coordinator
|
|
34
25
|
end
|
35
26
|
end
|
36
27
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
# store delegation methods
|
46
|
-
|
47
|
-
def items
|
48
|
-
@store.items
|
49
|
-
end
|
50
|
-
|
51
|
-
def capacity
|
52
|
-
@store.capacity
|
53
|
-
end
|
54
|
-
|
55
|
-
def length
|
56
|
-
@store.length
|
57
|
-
end
|
58
|
-
|
59
|
-
def peek
|
60
|
-
@store.peek
|
28
|
+
def details
|
29
|
+
{
|
30
|
+
"name" => @skill,
|
31
|
+
"full" => full?,
|
32
|
+
"capacity" => capacity,
|
33
|
+
"count" => length,
|
34
|
+
"items" => items
|
35
|
+
}
|
61
36
|
end
|
62
37
|
end
|
63
38
|
end
|
@@ -52,12 +52,12 @@ module Coordinator
|
|
52
52
|
@redis.lrange(@queue_name, 0, length).map { |i| deserialize(i) }
|
53
53
|
end
|
54
54
|
|
55
|
-
private
|
56
|
-
|
57
55
|
def full?
|
58
56
|
capacity && capacity <= length
|
59
57
|
end
|
60
58
|
|
59
|
+
private
|
60
|
+
|
61
61
|
def serialize(item)
|
62
62
|
item.is_a?(String) ? item : item.to_json
|
63
63
|
end
|
data/lib/coordinator/version.rb
CHANGED
data/test/unit/base_test.rb
CHANGED
@@ -38,6 +38,34 @@ describe 'Coordinator::Base' do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
describe 'capacity' do
|
42
|
+
before do
|
43
|
+
@coordinator.set_capacity("high", 1)
|
44
|
+
@coordinator.add_task("high", 1)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'recognizes full state' do
|
48
|
+
assert @coordinator.full?("high")
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'recognizes full state' do
|
52
|
+
@coordinator.set_capacity("high", 2)
|
53
|
+
refute @coordinator.full?("high")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'info' do
|
58
|
+
it 'returns queue specific details' do
|
59
|
+
@coordinator.add_task("high", "new task")
|
60
|
+
info = @coordinator.info("high")
|
61
|
+
assert_equal "high", info["name"]
|
62
|
+
assert_equal nil, info["full"]
|
63
|
+
assert_equal nil, info["capacity"]
|
64
|
+
assert_equal 1, info["count"]
|
65
|
+
assert_equal ["new task"], info["items"]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
41
69
|
describe 'length_all' do
|
42
70
|
it 'returns the total amount of tasks across all queues' do
|
43
71
|
@coordinator.add_task("medium", 1)
|
data/test/unit/queue_test.rb
CHANGED
@@ -6,37 +6,14 @@ describe "Coordinator::Queue" do
|
|
6
6
|
Redis.current.flushall
|
7
7
|
end
|
8
8
|
|
9
|
-
describe
|
10
|
-
it
|
11
|
-
@queue.
|
12
|
-
|
9
|
+
describe 'next_task' do
|
10
|
+
it 'returns task when eligible' do
|
11
|
+
@queue.push(1)
|
12
|
+
assert 1, @queue.next_task(["high"])
|
13
13
|
end
|
14
14
|
|
15
|
-
it
|
16
|
-
@queue.
|
17
|
-
assert_equal nil, @queue.next_task(["medium"])
|
18
|
-
end
|
19
|
-
|
20
|
-
it "returns nil when no work" do
|
21
|
-
assert_equal nil, @queue.next_task(["medium"])
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "add_task" do
|
26
|
-
it "returns true when skill present" do
|
27
|
-
@queue.add_task(5)
|
28
|
-
@queue.add_task(4)
|
29
|
-
|
30
|
-
assert_equal 5, @queue.next_task(["high"])
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "add_priority_task" do
|
35
|
-
it "returns true when skill present" do
|
36
|
-
@queue.add_task(5)
|
37
|
-
@queue.add_priority_task(4)
|
38
|
-
|
39
|
-
assert_equal 4, @queue.next_task(["high"])
|
15
|
+
it 'returns nil when not eligible' do
|
16
|
+
assert_equal nil, @queue.next_task(["high"])
|
40
17
|
end
|
41
18
|
end
|
42
19
|
|
@@ -51,7 +28,7 @@ describe "Coordinator::Queue" do
|
|
51
28
|
|
52
29
|
it "can override default behaviour" do
|
53
30
|
queue = Coordinator::Queue.new("normal") do |task, skills|
|
54
|
-
return true if skills.include?(
|
31
|
+
return true if skills.include?(skill) && skills.include?("online")
|
55
32
|
return true if skills.include?("low")
|
56
33
|
return true if task == 4
|
57
34
|
task == 3 && skills.include?("special")
|
@@ -63,14 +40,4 @@ describe "Coordinator::Queue" do
|
|
63
40
|
assert queue.eligible?(3, ["special"]), "override through both"
|
64
41
|
end
|
65
42
|
end
|
66
|
-
|
67
|
-
describe "peek" do
|
68
|
-
it "deserialize the top task but leaves it in the queue" do
|
69
|
-
task = {"id" => 123}
|
70
|
-
@queue.add_task(task)
|
71
|
-
|
72
|
-
assert_equal task, @queue.peek
|
73
|
-
assert_equal task, @queue.peek # ensure task is still enqueued
|
74
|
-
end
|
75
|
-
end
|
76
43
|
end
|
@@ -88,6 +88,16 @@ describe 'Coordinator::RedisQueue' do
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
describe '.full?' do
|
92
|
+
it 'false when under capacity' do
|
93
|
+
refute @queue.full?
|
94
|
+
end
|
95
|
+
it 'true when at capacity' do
|
96
|
+
@queue.capacity = 0
|
97
|
+
assert @queue.full?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
91
101
|
it 'allows for objects' do
|
92
102
|
[1000, "taco", {"a" => 1}, [1,2,3]].each do |o|
|
93
103
|
@queue.push(o)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coordinator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Mercier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -136,4 +136,3 @@ test_files:
|
|
136
136
|
- test/unit/parallelism_test.rb
|
137
137
|
- test/unit/queue_test.rb
|
138
138
|
- test/unit/redis_queue_test.rb
|
139
|
-
has_rdoc:
|