Class: SMQ::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/smq/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#batchObject

Returns the value of attribute batch.



7
8
9
# File 'lib/smq/worker.rb', line 7

def batch
  @batch
end

#batchesObject

Returns the value of attribute batches.



7
8
9
# File 'lib/smq/worker.rb', line 7

def batches
  @batches
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/smq/worker.rb', line 7

def name
  @name
end

#queueObject

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/smq/worker.rb', line 53

def is_stopping?
  @stopping
end

#is_working?Boolean

Returns:

  • (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_sObject



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 work_one_message(&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

Yields:

  • (msg)


39
40
41
42
43
# File 'lib/smq/worker.rb', line 39

def work_one_message
  msg = self.queue.reserve(self)
  yield msg if !msg.nil? && block_given?
  msg
end