Module: Ears

Defined in:
lib/ears.rb,
lib/ears/setup.rb,
lib/ears/errors.rb,
lib/ears/testing.rb,
lib/ears/version.rb,
lib/ears/consumer.rb,
lib/ears/publisher.rb,
lib/ears/middleware.rb,
lib/ears/configuration.rb,
lib/ears/consumer_wrapper.rb,
lib/ears/middlewares/json.rb,
lib/ears/testing/test_helper.rb,
lib/ears/middlewares/appsignal.rb,
lib/ears/publisher_channel_pool.rb,
lib/ears/testing/publisher_mock.rb,
lib/ears/middlewares/max_retries.rb,
lib/ears/publisher_retry_handler.rb,
lib/ears/testing/message_capture.rb,
lib/ears/publisher_confirmation_handler.rb

Defined Under Namespace

Modules: Middlewares, Testing Classes: Configuration, Consumer, ConsumerWrapper, MaxRecoveryAttemptsExhaustedError, Middleware, PublishConfirmationTimeout, PublishError, PublishNacked, Publisher, PublisherChannelPool, PublisherConfirmationHandler, PublisherRetryHandler, Setup

Constant Summary collapse

VERSION =
'0.22.2'

Class Method Summary collapse

Class Method Details

.channelBunny::Channel

The channel for the current thread.

Returns:

  • (Bunny::Channel)


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

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:



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

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

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

Yields the global configuration instance so you can modify it.

Yield Parameters:



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

def configure
  yield(configuration)
  configuration.validate!
end

.connectionBunny::Session

The global RabbitMQ connection used by Ears.

Returns:

  • (Bunny::Session)


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

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.



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

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

.reset!Object

Used internally for testing.



82
83
84
# File 'lib/ears.rb', line 82

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)


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

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

.setupObject

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



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

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

.setup_consumers(*consumer_classes) ⇒ Object

Quick setup your consumers (including exchanges and queues).



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

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

.stop!Object

Closes the connection, removing the consumers.



67
68
69
70
71
# File 'lib/ears.rb', line 67

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