Class: SMQ::Worker
- Inherits:
-
Object
- Object
- SMQ::Worker
- Defined in:
- lib/smq/worker.rb
Instance Attribute Summary collapse
-
#batch ⇒ Object
Returns the value of attribute batch.
-
#batches ⇒ Object
Returns the value of attribute batches.
-
#name ⇒ Object
Returns the value of attribute name.
-
#queue ⇒ Object
Returns the value of attribute queue.
Instance Method Summary collapse
-
#initialize(queue, batches = 1, batch = 1) ⇒ Worker
constructor
A new instance of Worker.
- #is_stopping? ⇒ Boolean
- #is_working? ⇒ Boolean
- #stop! ⇒ Object
- #to_s ⇒ Object
- #work(until_empty = false, &block) ⇒ Object
- #work_one_message {|msg| ... } ⇒ Object
Constructor Details
#initialize(queue, batches = 1, batch = 1) ⇒ Worker
Returns a new instance of Worker.
12 13 14 15 16 17 |
# File 'lib/smq/worker.rb', line 12 def initialize(queue, batches=1, batch=1) self.name = "#{Socket.gethostname}:#{Process.pid}" rescue "pid:#{Process.pid}" self.queue = queue.instance_of?(SMQ::Queue) ? queue : SMQ::Queue.new(queue) self.batches = batches self.batch = batch end |
Instance Attribute Details
#batch ⇒ Object
Returns the value of attribute batch.
7 8 9 |
# File 'lib/smq/worker.rb', line 7 def batch @batch end |
#batches ⇒ Object
Returns the value of attribute batches.
7 8 9 |
# File 'lib/smq/worker.rb', line 7 def batches @batches end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/smq/worker.rb', line 7 def name @name end |
#queue ⇒ Object
Returns the value of attribute queue.
7 8 9 |
# File 'lib/smq/worker.rb', line 7 def queue @queue end |
Instance Method Details
#is_stopping? ⇒ Boolean
53 54 55 |
# File 'lib/smq/worker.rb', line 53 def is_stopping? @stopping end |
#is_working? ⇒ Boolean
49 50 51 |
# File 'lib/smq/worker.rb', line 49 def is_working? @working end |
#stop! ⇒ Object
45 46 47 |
# File 'lib/smq/worker.rb', line 45 def stop! @stopping = true end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/smq/worker.rb', line 19 def to_s self.name end |
#work(until_empty = false, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/smq/worker.rb', line 23 def work(until_empty = false, &block) @working = true empty = false empty_for = 0 while(!empty && !@stopping) do if (&block).nil? empty = true if until_empty sleep 1 unless (empty && (empty_for += 1) >= 10) else empty_for = 0 end end @stopping = false @working = false end |
#work_one_message {|msg| ... } ⇒ Object
39 40 41 42 43 |
# File 'lib/smq/worker.rb', line 39 def msg = self.queue.reserve(self) yield msg if !msg.nil? && block_given? msg end |