Class: EventQ::RabbitMq::EventQClient

Inherits:
Object
  • Object
show all
Defined in:
lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb

Overview

Implements a general interface to raise an event EventQ::Amazon::EventQClient is the sister-class which does the same for AWS

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EventQClient

Returns a new instance of EventQClient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 8

def initialize(options={})

  if options[:client] == nil
    raise ':client (QueueClient) must be specified.'.freeze
  end

  @client = options[:client]
  @queue_manager = QueueManager.new
  @event_raised_exchange = EventRaisedExchange.new
  @serialization_manager = EventQ::SerializationProviders::Manager.new
  @signature_manager = EventQ::SignatureProviders::Manager.new

  # this array is used to record known event types
  @known_event_types = []

  @connection_pool = ::Queue.new
end

Instance Method Details

#check_in_connection(connection) ⇒ Object



32
33
34
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 32

def check_in_connection(connection)
  @connection_pool.push(connection)
end

#check_out_connectionObject



26
27
28
29
30
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 26

def check_out_connection
  @connection_pool.pop(true)
rescue
  @client.get_connection
end

#new_messageObject



99
100
101
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 99

def new_message
  EventQ::QueueMessage.new
end

#publish(topic:, event:, context: {}) ⇒ Object



49
50
51
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 49

def publish(topic:, event:, context: {})
  raise_event(topic, event, context)
end

#raise_event(event_type, event, context = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 53

def raise_event(event_type, event, context = {})
  register_event(event_type)

  _event_type = EventQ.create_event_type(event_type)

  with_connection do |channel|
    exchange = @queue_manager.get_exchange(channel, @event_raised_exchange)

    message = serialized_message(event_type, event, context)

    exchange.publish(message, routing_key: _event_type)

    EventQ.logger.debug do
      "[#{self.class}] - Raised event to Exchange. Routing Key: #{_event_type} | Message: #{message}."
    end
  end
end

#raise_event_in_queue(event_type, event, queue, delay, context = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 71

def raise_event_in_queue(event_type, event, queue, delay, context = {})
  register_event(event_type)

  _event_type = EventQ.create_event_type(event_type)

  with_connection do |channel|
    exchange = @queue_manager.get_queue_exchange(channel, queue)

    delay_exchange = @queue_manager.get_delay_exchange(channel, queue, delay)

    delay_queue = @queue_manager.create_delay_queue(channel, queue, exchange.name, delay)
    delay_queue.bind(delay_exchange, routing_key: _event_type)

    _queue_name = EventQ.create_queue_name(queue)

    q = channel.queue(_queue_name, durable: @queue_manager.durable)
    q.bind(exchange, routing_key: _event_type)

    message = serialized_message(event_type, event, context)

    delay_exchange.publish(message, routing_key: _event_type)

    EventQ.logger.debug do
      "[#{self.class}] - Raised event to Queue: #{_queue_name} | Message: #{message} | Delay: #{delay}."
    end
  end
end

#register_event(event_type) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 40

def register_event(event_type)
  if registered?(event_type)
    return true
  end

  @known_event_types << event_type
  true
end

#registered?(event_type) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/eventq/eventq_rabbitmq/rabbitmq_eventq_client.rb', line 36

def registered?(event_type)
  @known_event_types.include?(event_type)
end