minion 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/examples/math.rb +1 -1
- data/examples/when.rb +25 -0
- data/lib/minion.rb +25 -15
- data/lib/minion/handler.rb +30 -0
- metadata +6 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/examples/math.rb
CHANGED
data/examples/when.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'minion'
|
5
|
+
|
6
|
+
include Minion
|
7
|
+
|
8
|
+
error do |e|
|
9
|
+
puts "got an error!"
|
10
|
+
end
|
11
|
+
|
12
|
+
logger do |msg|
|
13
|
+
puts "--> #{msg}"
|
14
|
+
end
|
15
|
+
|
16
|
+
$listen = true
|
17
|
+
|
18
|
+
job "do.once", :when => lambda { $listen } do |args|
|
19
|
+
puts "Do this one action - then unsubscribe..."
|
20
|
+
$listen = false
|
21
|
+
end
|
22
|
+
|
23
|
+
enqueue("do.once",[])
|
24
|
+
enqueue("do.once",[])
|
25
|
+
|
data/lib/minion.rb
CHANGED
@@ -2,6 +2,7 @@ require 'uri'
|
|
2
2
|
require 'json'
|
3
3
|
require 'mq'
|
4
4
|
require 'bunny'
|
5
|
+
require 'minion/handler'
|
5
6
|
|
6
7
|
module Minion
|
7
8
|
extend self
|
@@ -19,7 +20,12 @@ module Minion
|
|
19
20
|
bunny.queue(queue, :durable => true, :auto_delete => false).publish(data.to_json)
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
+
def log(msg)
|
24
|
+
@@logger ||= proc { |m| puts "#{Time.now} :minion: #{m}" }
|
25
|
+
@@logger.call(msg)
|
26
|
+
end
|
27
|
+
|
28
|
+
def error(&blk)
|
23
29
|
@@error_handler = blk
|
24
30
|
end
|
25
31
|
|
@@ -27,8 +33,15 @@ module Minion
|
|
27
33
|
@@logger = blk
|
28
34
|
end
|
29
35
|
|
30
|
-
def job(queue, &blk)
|
31
|
-
handler
|
36
|
+
def job(queue, options = {}, &blk)
|
37
|
+
handler = Minion::Handler.new queue
|
38
|
+
handler.when = options[:when] if options[:when]
|
39
|
+
handler.unsub = lambda do
|
40
|
+
log "unsubscribing to #{queue}"
|
41
|
+
MQ.queue(queue).unsubscribe
|
42
|
+
end
|
43
|
+
handler.sub = lambda do
|
44
|
+
log "subscribing to #{queue}"
|
32
45
|
MQ.queue(queue).subscribe(:ack => true) do |h,m|
|
33
46
|
return if AMQP.closing?
|
34
47
|
begin
|
@@ -44,8 +57,16 @@ module Minion
|
|
44
57
|
error_handler.call(e)
|
45
58
|
end
|
46
59
|
h.ack
|
60
|
+
check_all
|
47
61
|
end
|
48
62
|
end
|
63
|
+
@@handlers ||= []
|
64
|
+
at_exit { Minion.run } if @@handlers.size == 0
|
65
|
+
@@handlers << handler
|
66
|
+
end
|
67
|
+
|
68
|
+
def check_all
|
69
|
+
@@handlers.each { |h| h.check }
|
49
70
|
end
|
50
71
|
|
51
72
|
def run
|
@@ -57,7 +78,7 @@ module Minion
|
|
57
78
|
EM.run do
|
58
79
|
AMQP.start(amqp_config) do
|
59
80
|
MQ.prefetch(1)
|
60
|
-
|
81
|
+
check_all
|
61
82
|
end
|
62
83
|
end
|
63
84
|
end
|
@@ -91,17 +112,6 @@ module Minion
|
|
91
112
|
@@bunny ||= new_bunny
|
92
113
|
end
|
93
114
|
|
94
|
-
def log(msg)
|
95
|
-
@@logger ||= proc { |m| puts "#{Time.now} :minion: #{m}" }
|
96
|
-
@@logger.call(msg)
|
97
|
-
end
|
98
|
-
|
99
|
-
def handler(&blk)
|
100
|
-
@@handlers ||= []
|
101
|
-
at_exit { Minion.run } if @@handlers.size == 0
|
102
|
-
@@handlers << blk
|
103
|
-
end
|
104
|
-
|
105
115
|
def next_job(args, response)
|
106
116
|
queue = args.delete("next_job")
|
107
117
|
enqueue(queue,args.merge(response)) if queue
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Minion
|
2
|
+
class Handler
|
3
|
+
attr_accessor :queue, :sub, :unsub, :when, :on
|
4
|
+
def initialize(queue)
|
5
|
+
@queue = queue
|
6
|
+
@when = lambda { true }
|
7
|
+
@sub = lambda {}
|
8
|
+
@unsub = lambda {}
|
9
|
+
@on = false
|
10
|
+
end
|
11
|
+
|
12
|
+
def should_sub?
|
13
|
+
@when.call
|
14
|
+
end
|
15
|
+
|
16
|
+
def check
|
17
|
+
if should_sub?
|
18
|
+
@sub.call unless @on
|
19
|
+
@on = true
|
20
|
+
else
|
21
|
+
@unsub.call if @on
|
22
|
+
@on = false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"<handler queue=#{@queue} on=#{@on}>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Orion Henry
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-31 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -55,8 +55,10 @@ files:
|
|
55
55
|
- VERSION
|
56
56
|
- examples/math.rb
|
57
57
|
- examples/sandwich.rb
|
58
|
+
- examples/when.rb
|
58
59
|
- lib/minion.rb
|
59
|
-
|
60
|
+
- lib/minion/handler.rb
|
61
|
+
has_rdoc: false
|
60
62
|
homepage: http://github.com/orionz/minion
|
61
63
|
post_install_message:
|
62
64
|
rdoc_options:
|
@@ -85,3 +87,4 @@ summary: Super simple job queue over AMQP
|
|
85
87
|
test_files:
|
86
88
|
- examples/math.rb
|
87
89
|
- examples/sandwich.rb
|
90
|
+
- examples/when.rb
|