celerb 0.0.2.1 → 0.0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/celerb.rb +1 -1
- data/lib/celerb/result_consumer.rb +19 -12
- data/lib/celerb/task.rb +7 -2
- data/lib/celerb/task_publisher.rb +2 -4
- data/test/test_celerb.rb +1 -1
- metadata +3 -3
data/lib/celerb.rb
CHANGED
@@ -1,36 +1,43 @@
|
|
1
1
|
module Celerb
|
2
2
|
class ResultConsumer
|
3
|
+
|
3
4
|
def initialize(opts)
|
4
5
|
@exchange = MQ.direct(opts[:results], :auto_delete => true)
|
5
6
|
@handlers = {}
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@handlers[task_id] = blk
|
7
|
+
EM.add_periodic_timer(60) do
|
8
|
+
now = Time.now
|
9
|
+
@handlers.delete_if {|h| h[:expires] > now}
|
10
|
+
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
13
|
+
def register(task_id, expiration, &blk)
|
14
|
+
@handlers[task_id] = {
|
15
|
+
:queue => subscribe(task_id),
|
16
|
+
:expires => Time.now + expiration,
|
17
|
+
:proc => blk
|
18
|
+
}
|
16
19
|
end
|
17
20
|
|
18
21
|
def consume(header, body)
|
19
22
|
result = Result.new(MessagePack.unpack(body))
|
20
|
-
queue = @queues.delete result.task_id
|
21
|
-
queue.unsubscribe
|
22
23
|
if @handlers.include? result.task_id
|
23
24
|
handler = @handlers.delete result.task_id
|
24
|
-
handler.
|
25
|
+
handler[:queue].unsubscribe
|
26
|
+
handler[:proc].call result
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
29
31
|
|
32
|
+
def subscribe(task_id)
|
33
|
+
queue = task_id_to_queue(task_id)
|
34
|
+
queue.subscribe &method(:consume)
|
35
|
+
end
|
36
|
+
|
30
37
|
def task_id_to_queue(task_id)
|
31
38
|
queue = MQ.queue(task_id_to_queue_name(task_id), :auto_delete => true)
|
32
39
|
queue.bind(@exchange)
|
33
|
-
|
40
|
+
queue
|
34
41
|
end
|
35
42
|
|
36
43
|
def task_id_to_queue_name(task_id)
|
data/lib/celerb/task.rb
CHANGED
@@ -6,10 +6,12 @@ module Celerb
|
|
6
6
|
base.extend ClassMethods
|
7
7
|
end
|
8
8
|
|
9
|
+
# Sends task to celery worker
|
9
10
|
def delay
|
10
11
|
self.class.delay self.to_celery
|
11
12
|
end
|
12
13
|
|
14
|
+
# Should return valid arguments for task specified by #task_name
|
13
15
|
def to_celery
|
14
16
|
raise NotImplementedError, "You have to return Celery task arguments here"
|
15
17
|
end
|
@@ -41,8 +43,11 @@ module Celerb
|
|
41
43
|
@task_id = task_id
|
42
44
|
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
+
# Awaits a task result and calls block when the result is available.
|
47
|
+
# Expiration needs to be explicitly specified in seconds to prevent memory
|
48
|
+
# leaks. Result handlers are periodically checked and expired ones are deleted.
|
49
|
+
def wait(expiration, &blk)
|
50
|
+
TaskPublisher.register_result_handler(@task_id, expiration, &blk)
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
@@ -11,7 +11,6 @@ module Celerb
|
|
11
11
|
task_id=nil, taskset_id=nil, expires=nil, eta=nil,
|
12
12
|
exchange=nil, exchange_type=nil, retries=0)
|
13
13
|
task_id ||= TaskPublisher.uniq_id
|
14
|
-
@@results.subscribe task_id
|
15
14
|
publish({
|
16
15
|
:task => task_name,
|
17
16
|
:id => task_id,
|
@@ -24,8 +23,8 @@ module Celerb
|
|
24
23
|
return task_id
|
25
24
|
end
|
26
25
|
|
27
|
-
def self.register_result_handler(task_id, &blk)
|
28
|
-
@@results.register(task_id, &blk)
|
26
|
+
def self.register_result_handler(task_id, expiry, &blk)
|
27
|
+
@@results.register(task_id, expiry, &blk)
|
29
28
|
end
|
30
29
|
|
31
30
|
private
|
@@ -37,7 +36,6 @@ module Celerb
|
|
37
36
|
}
|
38
37
|
end
|
39
38
|
|
40
|
-
|
41
39
|
def self.uniq_id
|
42
40
|
return UUID.create_v4.to_s
|
43
41
|
end
|
data/test/test_celerb.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: celerb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 67
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- 2
|
10
|
-
-
|
11
|
-
version: 0.0.2.
|
10
|
+
- 2
|
11
|
+
version: 0.0.2.2
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Dusan Maliarik
|