Module: Ears

Defined in:
lib/ears.rb,
lib/ears/setup.rb,
lib/ears/errors.rb,
lib/ears/version.rb,
lib/ears/consumer.rb,
lib/ears/middleware.rb,
lib/ears/configuration.rb,
lib/ears/consumer_wrapper.rb,
lib/ears/middlewares/json.rb,
lib/ears/middlewares/appsignal.rb,
lib/ears/middlewares/max_retries.rb

Defined Under Namespace

Modules: Middlewares Classes: Configuration, Consumer, ConsumerWrapper, MaxRecoveryAttemptsExhaustedError, Middleware, Setup

Constant Summary collapse

VERSION =
'0.14.1'

Class Method Summary collapse

Class Method Details

.channelBunny::Channel

The channel for the current thread.

Returns:

  • (Bunny::Channel)


36
37
38
39
40
41
42
43
# File 'lib/ears.rb', line 36

def channel
  Thread.current[:ears_channel] ||= connection
    .create_channel(nil, 1, true)
    .tap do |channel|
      channel.prefetch(1)
      channel.on_uncaught_exception { |error| Ears.error!(error) }
    end
end

.configurationEars::Configuration

The global configuration for Ears.

Returns:



12
13
14
# File 'lib/ears.rb', line 12

def configuration
  @configuration ||= Ears::Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields the global configuration instance so you can modify it.

Yield Parameters:



18
19
20
21
# File 'lib/ears.rb', line 18

def configure
  yield(configuration)
  configuration.validate!
end

.connectionBunny::Session

The global RabbitMQ connection used by Ears.

Returns:

  • (Bunny::Session)


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

def connection
  @connection ||=
    Bunny
      .new(configuration.rabbitmq_url, **connection_config)
      .tap { |conn| conn.start }
end

.error!(error) ⇒ Object

Signals that an uncaught error has occurred and the process should be stopped.

Parameters:

  • error (Exception)

    The unhandled error that occurred.



74
75
76
77
# File 'lib/ears.rb', line 74

def error!(error)
  puts(error.full_message)
  @error = error
end

.reset!Object

Used internally for testing.



80
81
82
# File 'lib/ears.rb', line 80

def reset!
  @configuration = @connection = Thread.current[:ears_channel] = nil
end

.run!Object

Blocks the calling thread until +SIGTERM+ or +SIGINT+ is received. Used to keep the process alive while processing messages.

Raises:

  • (@error)


57
58
59
60
61
62
# File 'lib/ears.rb', line 57

def run!
  @running = true
  setup_traps
  sleep 1 while @running && @error.nil?
  raise @error if @error
end

.setup(&block) ⇒ Object

Used to set up your exchanges, queues and consumers. See Setup for implementation details.



46
47
48
# File 'lib/ears.rb', line 46

def setup(&block)
  Ears::Setup.new.instance_eval(&block)
end

.setup_consumers(*consumer_classes) ⇒ Object

Quick setup your consumers (including exchanges and queues).



51
52
53
# File 'lib/ears.rb', line 51

def setup_consumers(*consumer_classes)
  Ears::Setup.new.setup_consumers(*consumer_classes)
end

.stop!Object

Closes the connection, removing the consumers.



65
66
67
68
69
# File 'lib/ears.rb', line 65

def stop!
  connection.close
  @connection = nil
  Thread.current[:ears_channel] = nil
end