Skip to content

Overview

dwkoogt edited this page Apr 22, 2018 · 5 revisions

Setup

Worker needs perform method implemented and typical setup will look like this:

class PokeWorker
  include PikaQue::Worker
  from_queue 'poke'
  config codec: PikaQue::Codecs::JSON

  def perform(msg)
    # do something with msg
    ack!
  end
end

If you need access to RabbitMQ stuff like delivery info and metadata, then you can override/overload work method like this:

class PokeWorker
  include PikaQue::Worker
  from_queue 'poke'
  config codec: PikaQue::Codecs::JSON

  def work(delivery_info, metadata, msg)
    # do something with msg
    ack!
  end
end

To enqueue jobs, you can do either of the following:

PokeWorker.enqueue({ greeting: "I Challenge You!" })
# or
PikaQue::Publisher.new.publish({ greeting: "Hello World!" }, to_queue: 'poke')

To configure PikaQue, pass options to configure method. The middleware system from Sidekiq is incorporated and can be setup the same way.

PikaQue.configure :amqp => 'amqp://guest:guest@localhost:5672',
                  :vhost => '/',
                  :metrics => ThirdParty::Instrumentation
                  # see PikaQue::Configuration for more options

PikaQue.middleware do |chain|
  chain.add SomeMiddleware
end

PikaQue.reporters << ThirdParty::ErrorReporter.new

PikaQue.logger.level = ::Logger::WARN

PikaQue will not start workers on server without setting up processors. Processors can be setup in PikaQue.config or with -w command line input with comma separated values or -C with a yaml configuration file. (For Rails, it will try to pick up config/pika_que.yml and run it as default.) Each processor is setup with its own channel, thread pool and can have any number of workers.

# my_config.yml

delay: true
concurrency: 5
middlewares:
  - 'SomeModule::SomeMiddlaware'
reporters:
  - 'ThirdParty::ErrorReporter'
processors:
  - workers:
    - worker: HighLoadWorker
    handler_class: PikaQue::Handlers::RetryHandler
    handler_options:
      retry_mode: :const
    channel_options:
      prefetch: 10
    concurrency: 10
  - workers:
    - worker: MedLoadWorker
    - worker: LowLoadWorker
    handler_class: PikaQue::Handlers::RetryHandler
    handler_options:
      retry_mode: :exp
    channel_options:
      prefetch: 5
    concurrency: 10
  - workers:
    - SeldomUsedWorker
    - NotDoingMuchWorker
    handler_class: PikaQue::Handlers::ErrorHandler
    channel_options:
      prefetch: 2
    concurrency: 2

# workers can be defined with or without worker key. (or queue/queue_name for rails)

Then run the server with config file.

bundle exec pika_que -C ./my_conifg.yml
Clone this wiki locally