Class: BunnyEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny_events.rb

Constant Summary collapse

@@defaults =
{
  exchange: '',
  exchange_type: :direct,
  routing_key: 'message_queue_event'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bunny_connectionObject

Returns the value of attribute bunny_connection.



4
5
6
# File 'lib/bunny_events.rb', line 4

def bunny_connection
  @bunny_connection
end

#channelsObject

Returns the value of attribute channels.



4
5
6
# File 'lib/bunny_events.rb', line 4

def channels
  @channels
end

#initialized_eventsObject

Keeps track of which events have been intiailized by this BunnyEvents worker. Used to ensure that the queue and exchange creation is only performed once.



8
9
10
# File 'lib/bunny_events.rb', line 8

def initialized_events
  @initialized_events
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/bunny_events.rb', line 37

def connected?
  @bunny_connection&.connected? || false
end

#init(bunny_connection) ⇒ Object

Initialise the BunnyEvents system by accepting a bunny connection.

Example:

NOTE: This can also accept bunnymock for testing bunny_events = BunnyEvents.new bunny_events.init BunnyMock.new.start



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bunny_events.rb', line 24

def init(bunny_connection)
  # Ensure the bunny_connection is valid
  if bunny_connection.nil? || !bunny_connection.respond_to?(:connected?)
    raise Exceptions::InvalidBunnyConnection
  end

  @channels = {}

  @initialized_exchanges = {}

  @bunny_connection = bunny_connection
end

#publish(message, routing_key = nil) ⇒ Object

Public message. message should be an instance of BaseMessage (or a class with BaseMessage included)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bunny_events.rb', line 42

def publish(message, routing_key = nil)
  unless message.class.included_modules.include?(BunnyEvent)
    raise Exceptions::InvalidBunnyEvent
  end

  raise Exceptions::InvalidBunnyConnection unless connected?

  # get the options defined by the message queue event class
  opts = @@defaults.merge message.class.options

  # Use the class name to determine which channel to use
  unless @channels.key?(message.class.name)
    @channels[message.class.name] = @bunny_connection.create_channel
  end

  channel = @channels[message.class.name]

  #  Ensure that the exchange, queue and binding creation is only performed once
  if !@initialized_exchanges.key?(message.class.name) || opts[:always_create_when_publishing]
    # If the event was sent with an exchange name, create and submit this to the exchange, otherwise, just use the default exchange
    x = if !opts[:exchange].nil? && !opts[:exchange].empty?
          channel.exchange(opts[:exchange], opts[:exchange_opts] || {})
        else
          channel.default_exchange
        end
    # if the event was sent with queue definitions, ensure to create the bindings
    handle_queue_definitions channel, x, opts[:queues] unless opts[:queues].nil?

    # ensure this event's creation params are not processed again
    @initialized_exchanges[message.class.name] ||= x
  end

  x ||= @initialized_exchanges[message.class.name]

  # ensure exchange is not null
  if x.nil? || !@bunny_connection.exchange_exists?(opts[:exchange])
    raise Exceptions::InvalidExchange
  end

  # publish message along with the optional routing key
  x.publish message.message, routing_key: routing_key || opts[:routing_key]
end