Class: EventQ::Amazon::EventQClient

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EventQClient

Returns a new instance of EventQClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eventq_aws/aws_eventq_client.rb', line 7

def initialize(options)

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

  @client = options[:client]

  @serialization_manager = EventQ::SerializationProviders::Manager.new
  @signature_manager = EventQ::SignatureProviders::Manager.new

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

end

Instance Method Details

#new_messageObject



78
79
80
# File 'lib/eventq_aws/aws_eventq_client.rb', line 78

def new_message
  EventQ::QueueMessage.new
end

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



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

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eventq_aws/aws_eventq_client.rb', line 41

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

  with_prepared_message(event_type, event, context) do |message|

    response = @client.sns.publish(
      topic_arn: topic_arn(event_type),
      message: message,
      subject: event_type
    )

    EventQ.logger.debug do
      "[#{self.class} #raise_event] - Published to SNS with topic_arn: #{topic_arn(event_type)} | event_type: #{event_type} | Message: #{message}"
    end

    response
  end
end

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/eventq_aws/aws_eventq_client.rb', line 60

def raise_event_in_queue(event_type, event, queue, delay, context = {})
  queue_url = @client.get_queue_url(queue)
  with_prepared_message(event_type, event, context) do |message|

    response = @client.sqs.send_message(
      queue_url: queue_url,
      message_body: sqs_message_body_for(message),
      delay_seconds: delay
    )

    EventQ.logger.debug do
      "[#{self.class} #raise_event_in_queue] - Raised event to SQS queue: #{queue_url} | event_type: #{event_type} | Message: #{message}"
    end

    response
  end
end

#register_event(event_type) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/eventq_aws/aws_eventq_client.rb', line 27

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

  @client.create_topic_arn(event_type)
  @known_event_types << event_type
  true
end

#registered?(event_type) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/eventq_aws/aws_eventq_client.rb', line 23

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