Sha256: 4f63631566af64bfb59e54ae47c1505197719996c97f4fbc7518752e8faa46e9

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Totoro
  class BaseWorker
    def self.setup(attrs)
      prefix = attrs[:prefix].present? ? attrs[:prefix] : :default
      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
      @queue_class = queue_class
    end

    def execute
      @queue_class.subscribe(@queue_name) do |delivery_info, metadata, payload|
        Rails.logger.info "#{@queue_name} Received: #{payload}"
        payload_hash = JSON.parse(payload).with_indifferent_access
        process(payload_hash, metadata, delivery_info)
      end
      Rails.logger.info 'Listening to the Rabbitmq'
      @queue_class.channel.work_pool.join
    end

    def process; end

    private

    def queue_class
      if @prefix == :default
        Totoro::Queue
      else
        "Totoro::#{@prefix.to_s.camelize}::Queue".constantize
      end
    end
  end

  class NeedQueueNameError < RuntimeError; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
totoro-0.2.2 lib/totoro/base_worker.rb