Class: Totoro::BaseWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/totoro/base_worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseWorker

Returns a new instance of BaseWorker.



16
17
18
# File 'lib/totoro/base_worker.rb', line 16

def initialize
  setup
end

Class Method Details

.setup(attrs) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/totoro/base_worker.rb', line 5

def self.setup(attrs)
  prefix = attrs[:prefix]
  queue_name = attrs[:queue_name]
  define_method('setup') do
    raise(Totoro::NeedQueueNameError) if queue_name.nil?

    @prefix = prefix
    @queue_name = queue_name
  end
end

Instance Method Details

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/totoro/base_worker.rb', line 20

def execute
  Rails.logger.info 'Listening to the Rabbitmq'
  STDOUT.flush
  subscribe_service.subscribe(@queue_name) do |delivery_info, , payload|
    Rails.logger.debug "#{@queue_name} received message"
    STDOUT.flush
    payload_hash = JSON.parse(payload).with_indifferent_access
    process(payload_hash, , delivery_info)
  end

  handle_usr1_n_usr2

  subscribe_service.channel.work_pool.join
rescue SignalException
  puts 'Terminating process ..'
  subscribe_service.channel.work_pool.shutdown(true)
  puts 'Stopped.'
end

#processObject



39
# File 'lib/totoro/base_worker.rb', line 39

def process; end