Module: Cuniculus

Extended by:
CuniculusMethods
Defined in:
lib/cuniculus.rb,
lib/cuniculus/cli.rb,
lib/cuniculus/core.rb,
lib/cuniculus/config.rb,
lib/cuniculus/logger.rb,
lib/cuniculus/worker.rb,
lib/cuniculus/plugins.rb,
lib/cuniculus/version.rb,
lib/cuniculus/consumer.rb,
lib/cuniculus/job_queue.rb,
lib/cuniculus/dispatcher.rb,
lib/cuniculus/exceptions.rb,
lib/cuniculus/pub_worker.rb,
lib/cuniculus/supervisor.rb,
lib/cuniculus/queue_config.rb,
lib/cuniculus/plugins/health_check.rb

Overview

Base definition of the Cuniculus Module

Defined Under Namespace

Modules: CuniculusMethods, Plugins, SupervisorMethods, Worker Classes: CLI, Config, Consumer, Dispatcher, Error, JobQueue, Logger, PubWorker, QueueConfig, Supervisor

Constant Summary collapse

CUNICULUS_EXCHANGE =
"cuniculus"
CUNICULUS_DLX_EXCHANGE =

Dead Letter Exchange

"cuniculus_dlx"
MAJOR =

The major version of Cuniculus. Only bumped for major changes.

0
MINOR =

The minor version of Cuniculus. Bumped for every non-patch level release.

2
TINY =

The tiny version of Cuniculus. Usually 0, only bumped for bugfix releases that fix regressions from previous versions.

1
VERSION =

The version of Cuniculus you are using, as a string (e.g. “2.11.0”)

[MAJOR, MINOR, TINY].join(".").freeze
VERSION_NUMBER =

The version of Cuniculus you are using, as a number (2.11.0 -> 20110)

MAJOR * 10_000 + MINOR * 10 + TINY

Class Method Summary collapse

Methods included from CuniculusMethods

convert_exception_class, dump_job, load_job, mark_time

Class Method Details

.configCuniculus::Config

Current config of Cuniculus

Returns config for read-only purpose. Use Cuniculus.configure to change the configured values.

Returns:



52
53
54
# File 'lib/cuniculus.rb', line 52

def self.config
  @config ||= Cuniculus::Config.new
end

.configure {|Cuniculus::Config| ... } ⇒ Object

Configure Cuniculus. Check Config for the available options.

Examples:

Change RabbitMQ connection details.

Cuniculus.configure do |cfg|
  cfg.rabbitmq_opts = { host: 'rmq.mycompany.com', user: 'guest', pass: 'guest' }
  cfg.add_queue({ name: "new_queue", max_retry: 4 })
end

Yields:



26
27
28
29
30
31
32
# File 'lib/cuniculus.rb', line 26

def self.configure
  cfg = Cuniculus::Config.new
  yield cfg
  cfg.declare!
  @config = cfg
  @dispatcher = Cuniculus::Dispatcher.new(cfg)
end

.dispatcherObject



43
44
45
# File 'lib/cuniculus.rb', line 43

def self.dispatcher
  @dispatcher ||= Cuniculus::Dispatcher.new(config)
end

.enqueue(job) ⇒ Object



34
35
36
37
# File 'lib/cuniculus.rb', line 34

def self.enqueue(job)
  dispatcher.job_queue << job
  dispatcher.start!
end

.error_handler(&block) ⇒ Object

Receives a block that is called when the job consumer encounters an error. The block receives the exception object and runs in the context of the consumer instance.

Note that overriding the default error handler does not affect the retry mechanism. This error handler is designed to be used for logging.

The default error handler is defined in Cuniculus::Consumer#handle_error.

Examples:

Send error info to an external service.

Cuniculus.error_handler do |e|
  err = "#{e.class.name}: #{e.message}"
  bt = e.backtrace.join("\n") unless e.backtrace.nil?
  MyLogginService.error(err, bt)
end


77
78
79
80
81
82
83
# File 'lib/cuniculus.rb', line 77

def self.error_handler(&block)
  Cuniculus::Consumer.define_method(:handle_error, &block)
  Cuniculus::Consumer.instance_eval { private :handle_error }

  Cuniculus::Dispatcher.define_method(:handle_error, &block)
  Cuniculus::Dispatcher.instance_eval { private :handle_error }
end

.loggerCuniculus::Logger

Current Cuniculus logger

Returns:



59
60
61
# File 'lib/cuniculus.rb', line 59

def self.logger
  @logger ||= Cuniculus::Logger.new($stdout, level: Logger::INFO)
end

.plugin(plugin, *args, &block) ⇒ Object

Load a plugin. If plugin is a Module, it is loaded directly. If it is a symbol, then it needs to satisfy the following:

The additional arguments and block are passed to the plugin’s ‘configure` method, if it exists.

Examples:

Enable ‘:health_check` plugin

Cuniculus.plugin(:health_check)

Parameters:

  • plugin (Symbol, Module)
  • args (Array<Object>)

    *args passed to the plugin’s ‘configure` method

  • block (Block)

    passed to the plugin’s ‘configure` method

Raises:



98
99
100
101
102
103
104
105
# File 'lib/cuniculus.rb', line 98

def self.plugin(plugin, *args, &block)
  plugin = Cuniculus::Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)
  raise Cuniculus::Error, "Invalid plugin type: #{plugin.class.inspect}. It must be a module" unless plugin.is_a?(Module)

  self::Supervisor.send(:include, plugin::SupervisorMethods) if defined?(plugin::SupervisorMethods)
  self::Supervisor.send(:extend, plugin::SupervisorClassMethods) if defined?(plugin::SupervisorClassMethods)
  plugin.configure(config.opts, *args, &block) if plugin.respond_to?(:configure)
end

.shutdownObject



39
40
41
# File 'lib/cuniculus.rb', line 39

def self.shutdown
  dispatcher.shutdown
end

.versionObject

The version of Cuniculus you are using, as a string (e.g. “2.11.0”)



22
23
24
# File 'lib/cuniculus/version.rb', line 22

def self.version
  VERSION
end