module Cuniculus::Worker

Constants

DEFAULT_OPTS
VALID_OPT_KEYS

Attributes

cun_opts[R]

Public Class Methods

extended(base) click to toggle source
Calls superclass method
   # File lib/cuniculus/worker.rb
11 def self.extended(base)
12   base.instance_variable_set(:@cun_opts, DEFAULT_OPTS)
13   super
14 end

Public Instance Methods

cuniculus_options(opts) click to toggle source

Worker-specific options for running cuniculus.

Note that options set on a worker class are inherited by its subclasses.

@param opts [Hash] @option opts [String] “queue” (“cun_default”) Name of the underlying RabbitMQ queue.

@example Change the queue name of a worker

class MyWorker
  include Cuniculus::Worker

  cuniculus_options queue: "critical"

  def perform
    # run the task
  end
end
   # File lib/cuniculus/worker.rb
40 def cuniculus_options(opts)
41   opts = validate_opts!(opts)
42   @cun_opts = opts
43 end
inherited(mod) click to toggle source
Calls superclass method
   # File lib/cuniculus/worker.rb
16 def inherited(mod)
17   mod.instance_variable_set(:@cun_opts, @cun_opts)
18   super
19 end
normalize_item(item) click to toggle source
   # File lib/cuniculus/worker.rb
62 def normalize_item(item)
63   Cuniculus.dump_job(item)
64 end
perform_async(*args) click to toggle source
   # File lib/cuniculus/worker.rb
52 def perform_async(*args)
53   publish({ "class" => self, "args" => args })
54 end
publish(item) click to toggle source
   # File lib/cuniculus/worker.rb
56 def publish(item)
57   routing_key = cun_opts[:queue]
58   payload = normalize_item(item)
59   Cuniculus.enqueue [Cuniculus::CUNICULUS_EXCHANGE, payload, routing_key]
60 end
validate_opts!(opts) click to toggle source
   # File lib/cuniculus/worker.rb
45 def validate_opts!(opts)
46   raise Cuniculus::WorkerOptionsError, "Argument passed to 'cuniculus_options' should be a Hash" unless opts.is_a?(Hash)
47   invalid_keys = opts.keys - VALID_OPT_KEYS
48   raise Cuniculus::WorkerOptionsError, "Invalid keys passed to 'cuniculus_options': #{invalid_keys.inspect}" unless invalid_keys.empty?
49   opts
50 end