pigeon 0.4.4 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/pigeon/processor.rb +20 -13
- data/lib/pigeon/queue.rb +22 -9
- data/lib/pigeon/task.rb +4 -2
- data/pigeon.gemspec +2 -2
- data/test/unit/pigeon_processor_test.rb +13 -0
- data/test/unit/pigeon_queue_test.rb +2 -0
- data/test/unit/pigeon_task_test.rb +5 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.6
|
data/lib/pigeon/processor.rb
CHANGED
@@ -5,6 +5,8 @@ class Pigeon::Processor
|
|
5
5
|
|
6
6
|
# == Properties ===========================================================
|
7
7
|
|
8
|
+
attr_accessor :context
|
9
|
+
attr_reader :queue
|
8
10
|
attr_reader :task
|
9
11
|
attr_reader :id
|
10
12
|
|
@@ -15,10 +17,11 @@ class Pigeon::Processor
|
|
15
17
|
# Creates a new processor. An optional queue can be specified in which case
|
16
18
|
# the processor will register itself as an observer of that queue. A block
|
17
19
|
# can be given to filter the tasks contained in the associated queue.
|
18
|
-
def initialize(queue = nil, &filter)
|
20
|
+
def initialize(queue = nil, context = nil, &filter)
|
19
21
|
@id = Pigeon::Support.unique_id
|
20
22
|
@lock = Mutex.new
|
21
23
|
@filter = filter || lambda { |task| true }
|
24
|
+
@context = context
|
22
25
|
|
23
26
|
if (queue)
|
24
27
|
self.queue = queue
|
@@ -31,24 +34,24 @@ class Pigeon::Processor
|
|
31
34
|
# then the observer callback for that queue will be removed.
|
32
35
|
def queue=(queue)
|
33
36
|
if (@queue)
|
34
|
-
@queue.
|
37
|
+
@queue.remove_processor(self, &@claim)
|
35
38
|
end
|
36
39
|
|
37
|
-
@queue = queue
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
@task = queue.claim(task)
|
40
|
+
if (@queue = queue)
|
41
|
+
@claim = lambda do |task|
|
42
|
+
@lock.synchronize do
|
43
|
+
if (!@task and @filter.call(task))
|
44
|
+
@task = queue.claim(task)
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
+
@task.run!(self) do
|
47
|
+
switch_to_next_task!
|
48
|
+
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
52
|
+
|
53
|
+
@queue.add_processor(self, &@claim)
|
49
54
|
end
|
50
|
-
|
51
|
-
@queue.observe(&@claim)
|
52
55
|
end
|
53
56
|
|
54
57
|
# Returns true if the given task would be accepted by the filter defined
|
@@ -62,13 +65,17 @@ class Pigeon::Processor
|
|
62
65
|
!!@task
|
63
66
|
end
|
64
67
|
|
68
|
+
def inspect
|
69
|
+
"<#{self.class}\##{@id} queue=#{@queue.inspect} task=#{@task} context=#{@context}>"
|
70
|
+
end
|
71
|
+
|
65
72
|
protected
|
66
73
|
def switch_to_next_task!
|
67
74
|
@lock.synchronize do
|
68
75
|
@task = nil
|
69
76
|
|
70
77
|
if (@task = @queue.pop(&@filter))
|
71
|
-
@task.run! do
|
78
|
+
@task.run!(self) do
|
72
79
|
switch_to_next_task!
|
73
80
|
end
|
74
81
|
end
|
data/lib/pigeon/queue.rb
CHANGED
@@ -17,15 +17,9 @@ class Pigeon::Queue
|
|
17
17
|
alias_method :to_s, :inspect
|
18
18
|
end
|
19
19
|
|
20
|
-
# ==
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# == Scopes ===============================================================
|
25
|
-
|
26
|
-
# == Callbacks ============================================================
|
27
|
-
|
28
|
-
# == Validations ==========================================================
|
20
|
+
# == Properties ===========================================================
|
21
|
+
|
22
|
+
attr_reader :processors
|
29
23
|
|
30
24
|
# == Class Methods ========================================================
|
31
25
|
|
@@ -56,6 +50,7 @@ class Pigeon::Queue
|
|
56
50
|
@claimable_task = { }
|
57
51
|
@filters = self.class.filters.dup
|
58
52
|
@observers = { }
|
53
|
+
@processors = [ ]
|
59
54
|
@next_task = { }
|
60
55
|
@insert_backlog = [ ]
|
61
56
|
|
@@ -107,6 +102,24 @@ class Pigeon::Queue
|
|
107
102
|
end
|
108
103
|
end
|
109
104
|
|
105
|
+
# Adds a processor to the queue and adds an observer claim method.
|
106
|
+
def add_processor(processor, &claim)
|
107
|
+
@observer_lock.synchronize do
|
108
|
+
@processors << processor
|
109
|
+
end
|
110
|
+
|
111
|
+
observe(&claim) if (claim)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Removes a processor from the queue and removes an observer claim method.
|
115
|
+
def remove_processor(processor, &claim)
|
116
|
+
@observer_lock.synchronize do
|
117
|
+
@processors.delete(processor)
|
118
|
+
end
|
119
|
+
|
120
|
+
remove_observer(&claim) if (claim)
|
121
|
+
end
|
122
|
+
|
110
123
|
# Creates a named filter for the queue using the provided block to select
|
111
124
|
# the tasks which should match.
|
112
125
|
def filter(filter_name, &block)
|
data/lib/pigeon/task.rb
CHANGED
@@ -38,14 +38,16 @@ class Pigeon::Task
|
|
38
38
|
after_initialized
|
39
39
|
end
|
40
40
|
|
41
|
-
# Kicks off the task.
|
41
|
+
# Kicks off the task. The processor execurting the task should be supplied
|
42
|
+
# as the first argument. An optional callback is executed just before each
|
42
43
|
# state is excuted and is passed the state name as a symbol.
|
43
|
-
def run!(initial_state = nil, &callback)
|
44
|
+
def run!(processor = nil, initial_state = nil, &callback)
|
44
45
|
@callback = callback if (block_given?)
|
45
46
|
|
46
47
|
@state = initial_state || self.class.initial_state
|
47
48
|
@started_at = Time.now
|
48
49
|
|
50
|
+
@processor = processor
|
49
51
|
run_state!(@state)
|
50
52
|
end
|
51
53
|
|
data/pigeon.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pigeon}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["tadman"]
|
12
|
-
s.date = %q{2010-12-
|
12
|
+
s.date = %q{2010-12-17}
|
13
13
|
s.default_executable = %q{launcher.example}
|
14
14
|
s.description = %q{Pigeon is a simple way to get started building an EventMachine engine that's intended to run as a background job.}
|
15
15
|
s.email = %q{github@tadman.ca}
|
@@ -110,6 +110,19 @@ class PigeonProcessorTest < Test::Unit::TestCase
|
|
110
110
|
|
111
111
|
assert_equal false, task_c.finished?
|
112
112
|
end
|
113
|
+
|
114
|
+
def test_can_unassign_queue_from_processor
|
115
|
+
queue = Pigeon::Queue.new
|
116
|
+
processor = Pigeon::Processor.new(queue)
|
117
|
+
|
118
|
+
assert_equal queue, processor.queue
|
119
|
+
assert_equal [ processor ], queue.processors
|
120
|
+
|
121
|
+
processor.queue = nil
|
122
|
+
|
123
|
+
assert_equal nil, processor.queue
|
124
|
+
assert_equal [ ], queue.processors
|
125
|
+
end
|
113
126
|
|
114
127
|
def test_multiple_processors
|
115
128
|
queue = Pigeon::Queue.new
|
@@ -140,7 +140,7 @@ class PigeonTaskTest < Test::Unit::TestCase
|
|
140
140
|
assert task.exception?
|
141
141
|
end
|
142
142
|
|
143
|
-
def
|
143
|
+
def test_with_context
|
144
144
|
options = {
|
145
145
|
:example => 'example1',
|
146
146
|
:optional => 1
|
@@ -149,6 +149,10 @@ class PigeonTaskTest < Test::Unit::TestCase
|
|
149
149
|
task = Pigeon::Task.new(options)
|
150
150
|
|
151
151
|
assert_equal options, task.context
|
152
|
+
|
153
|
+
task.context = 'test'
|
154
|
+
|
155
|
+
assert_equal 'test', task.context
|
152
156
|
end
|
153
157
|
|
154
158
|
def test_block_notification
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 6
|
9
|
+
version: 0.4.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- tadman
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-17 00:00:00 -05:00
|
18
18
|
default_executable: launcher.example
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|