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.11.2'

Class Method Summary collapse

Class Method Details

.channelBunny::Channel

The channel for the current thread.

Returns:

  • (Bunny::Channel)


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

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:



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

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

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

Yields the global configuration instance so you can modify it.

Yield Parameters:



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

def configure
  yield(configuration)
  configuration.validate!
end

.connectionBunny::Session

The global RabbitMQ connection used by Ears.

Returns:

  • (Bunny::Session)


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

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.



70
71
72
73
# File 'lib/ears.rb', line 70

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

.reset!Object

Used internally for testing.



76
77
78
79
80
# File 'lib/ears.rb', line 76

def reset!
  @connection = nil
  @configuration = nil
  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)


53
54
55
56
57
58
# File 'lib/ears.rb', line 53

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.



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

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

.stop!Object

Closes the connection, removing the consumers.



61
62
63
64
65
# File 'lib/ears.rb', line 61

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