Sha256: c1faeaded99642dbb8ed71ec03fecd13d06f2180d590e1216b6c2cd17a427cfc

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Totoro
  class BaseWorker
    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

    def initialize
      setup
    end

    def execute
      Rails.logger.info 'Listening to the Rabbitmq'
      STDOUT.flush
      subscribe_service.subscribe(@queue_name) do |delivery_info, metadata, payload|
        Rails.logger.debug "#{@queue_name} received message"
        STDOUT.flush
        payload_hash = JSON.parse(payload).with_indifferent_access
        process(payload_hash, metadata, delivery_info)
      end
      subscribe_service.channel.work_pool.join
    rescue SignalException
      puts 'Terminating process ..'
      subscribe_service.channel.work_pool.shutdown(true)
      puts 'Stopped.'
    end

    def process; end

    private

    def config
      @config ||= Totoro::Config.new(@prefix)
    end

    def subscribe_service
      @subscribe_service ||= Totoro::SubscribeService.new(config)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
totoro-1.0.4 lib/totoro/base_worker.rb
totoro-1.0.3 lib/totoro/base_worker.rb