orionz-minion 0.1.1 → 0.1.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.3
@@ -5,7 +5,7 @@ require 'minion'
5
5
 
6
6
  include Minion
7
7
 
8
- on_error do |e|
8
+ error do |e|
9
9
  puts "got an error!"
10
10
  end
11
11
 
@@ -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 on_error(&blk)
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 do
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
- @@handlers.each { |h| h.call }
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: orionz-minion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Orion Henry
@@ -56,6 +56,7 @@ files:
56
56
  - examples/math.rb
57
57
  - examples/sandwich.rb
58
58
  - lib/minion.rb
59
+ - lib/minion/handler.rb
59
60
  has_rdoc: true
60
61
  homepage: http://github.com/orionz/minion
61
62
  licenses: