adrian 1.0.1 → 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.
- data/lib/adrian/dispatcher.rb +11 -4
- data/lib/adrian/version.rb +1 -1
- data/test/dispatcher_test.rb +32 -16
- metadata +1 -1
data/lib/adrian/dispatcher.rb
CHANGED
@@ -5,10 +5,11 @@ module Adrian
|
|
5
5
|
attr_reader :running
|
6
6
|
|
7
7
|
def initialize(options = {})
|
8
|
-
@failure_handler
|
9
|
-
@stop_when_done
|
10
|
-
@
|
11
|
-
@
|
8
|
+
@failure_handler = FailureHandler.new
|
9
|
+
@stop_when_done = !!options[:stop_when_done]
|
10
|
+
@stop_when_signalled = options.fetch(:stop_when_signalled, true)
|
11
|
+
@sleep = options[:sleep] || 0.5
|
12
|
+
@options = options
|
12
13
|
end
|
13
14
|
|
14
15
|
def on_failure(*exceptions)
|
@@ -20,6 +21,7 @@ module Adrian
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def start(queue, worker_class)
|
24
|
+
trap_stop_signals if @stop_when_signalled
|
23
25
|
@running = true
|
24
26
|
|
25
27
|
while @running do
|
@@ -39,6 +41,11 @@ module Adrian
|
|
39
41
|
@running = false
|
40
42
|
end
|
41
43
|
|
44
|
+
def trap_stop_signals
|
45
|
+
Signal.trap('TERM') { stop }
|
46
|
+
Signal.trap('INT') { stop }
|
47
|
+
end
|
48
|
+
|
42
49
|
def delegate_work(item, worker_class)
|
43
50
|
worker = worker_class.new(item)
|
44
51
|
worker.report_to(self)
|
data/lib/adrian/version.rb
CHANGED
data/test/dispatcher_test.rb
CHANGED
@@ -3,37 +3,53 @@ require_relative 'test_helper'
|
|
3
3
|
describe Adrian::Dispatcher do
|
4
4
|
before do
|
5
5
|
$done_items = []
|
6
|
-
@q
|
6
|
+
@q = Adrian::ArrayQueue.new
|
7
7
|
@dispatcher = Adrian::Dispatcher.new(:stop_when_done => true)
|
8
|
+
@worker = TestWorker
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestWorker
|
12
|
+
|
13
|
+
def initialize(item)
|
14
|
+
@item = item
|
15
|
+
@done_items = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def perform
|
19
|
+
$done_items << [@boss, @item.value]
|
20
|
+
end
|
21
|
+
|
22
|
+
def report_to(boss)
|
23
|
+
@boss = boss
|
24
|
+
end
|
25
|
+
|
8
26
|
end
|
9
27
|
|
10
28
|
describe "work delegation" do
|
11
29
|
it "should instantiate an instance of the worker for each item and ask it to perform" do
|
12
|
-
worker = Class.new do
|
13
|
-
def initialize(item)
|
14
|
-
@item = item
|
15
|
-
end
|
16
|
-
|
17
|
-
def perform
|
18
|
-
$done_items << [@boss, @item.value]
|
19
|
-
end
|
20
|
-
|
21
|
-
def report_to(boss)
|
22
|
-
@boss = boss
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
30
|
@q.push(1)
|
27
31
|
@q.push(2)
|
28
32
|
@q.push(3)
|
29
33
|
|
30
|
-
@dispatcher.start(@q, worker)
|
34
|
+
@dispatcher.start(@q, @worker)
|
31
35
|
|
32
36
|
$done_items.must_equal([[@dispatcher, 1], [@dispatcher, 2], [@dispatcher, 3]])
|
33
37
|
end
|
34
38
|
end
|
35
39
|
|
36
40
|
describe "work evaluation" do
|
41
|
+
|
42
|
+
it "stops when receiving a termination signal" do
|
43
|
+
@dispatcher = Adrian::Dispatcher.new(:stop_when_done => false)
|
44
|
+
dispatch_thread = Thread.new { @dispatcher.start(@q, @worker) }
|
45
|
+
sleep(0.1)
|
46
|
+
assert_equal true, @dispatcher.running
|
47
|
+
|
48
|
+
Process.kill('TERM', Process.pid)
|
49
|
+
assert_equal false, @dispatcher.running
|
50
|
+
dispatch_thread.exit
|
51
|
+
end
|
52
|
+
|
37
53
|
it "should use the failure handler to handle the result" do
|
38
54
|
@dispatcher.on_failure(RuntimeError) do |item, worker, exception|
|
39
55
|
@q.push(item)
|