eventq_rabbitmq 1.13.0 → 1.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c9acbc79c90b23e20f046b6e08cbab5699dee7c
4
- data.tar.gz: 23f1c79ae679115d6ec2274e0fe266f2b644237e
3
+ metadata.gz: 5ffa54724256f83b66fec1203d2d7c378f507b1f
4
+ data.tar.gz: 8df00ee11ae9fd66f25ce064479492ab778aa96c
5
5
  SHA512:
6
- metadata.gz: d0b027367ed624d32b88c1ace1bf563b26c6426cfd2cc0ba74518936a70ba79a4d29b3d0bd6c9173c321ea38cb0c585f86550edd38a28736b4aa6e1be2506e32
7
- data.tar.gz: ee68a5641e73fd630eb701eb9669064cd754c3e6b685df427f861db52e0afac061d72983da75936d62a15e67997934a27b98da59d0541d5f95ee0b70be6ae52f
6
+ metadata.gz: 8d476e366f439033b370b31ce9e5c3e642629e5ec24be6371a50f9bf0ab2503d356aa7139a500a4cf5e53ea21399de65eb102bc44fa44eb0c71097ff92532fdd
7
+ data.tar.gz: 58f26ad9602a576b7bd01b7732a38d2e06a9c10ce8ec01bb019a3f681939f1a87cfc203585d52965e7edc91ed8544f6ce12689f284f6ab4fafcdf75c3de27018
@@ -1,5 +1,7 @@
1
1
  module EventQ
2
2
  module RabbitMq
3
+ # Implements a general interface to raise an event
4
+ # EventQ::Amazon::EventQClient is the sister-class which does the same for AWS
3
5
  class EventQClient
4
6
 
5
7
  def initialize(options={})
@@ -13,52 +15,102 @@ module EventQ
13
15
  @event_raised_exchange = EventRaisedExchange.new
14
16
  @serialization_manager = EventQ::SerializationProviders::Manager.new
15
17
  @signature_manager = EventQ::SignatureProviders::Manager.new
18
+
19
+ #this array is used to record known event types
20
+ @known_event_types = []
16
21
  end
17
22
 
18
- def raise_event(event_type, event)
23
+ def registered?(event_type)
24
+ @known_event_types.include?(event_type)
25
+ end
19
26
 
20
- connection = @client.get_connection
27
+ def register_event(event_type)
28
+ if registered?(event_type)
29
+ return true
30
+ end
31
+
32
+ @known_event_types << event_type
33
+ true
34
+ end
35
+
36
+ def raise_event(event_type, event)
37
+ register_event(event_type)
21
38
 
22
39
  _event_type = EventQ.create_event_type(event_type)
23
40
 
24
- begin
25
- channel = connection.create_channel
41
+ with_connection do |channel|
42
+ exchange = @queue_manager.get_exchange(channel, @event_raised_exchange)
26
43
 
27
- ex = @queue_manager.get_exchange(channel, @event_raised_exchange)
44
+ message = serialized_message(_event_type, event)
28
45
 
29
- qm = new_message
30
- qm.content = event
31
- qm.type = _event_type
46
+ exchange.publish(message, routing_key: _event_type)
32
47
 
33
- if EventQ::Configuration.signature_secret != nil
34
- provider = @signature_manager.get_provider(EventQ::Configuration.signature_provider)
35
- qm.signature = provider.write(message: qm, secret: EventQ::Configuration.signature_secret)
48
+ EventQ.logger.debug "[#{self.class}] - Raised event. Message: #{message} | Type: #{event_type}."
36
49
  end
50
+ end
37
51
 
38
- serialization_provider = @serialization_manager.get_provider(EventQ::Configuration.serialization_provider)
52
+ def raise_event_in_queue(event_type, event, queue, delay)
53
+ register_event(event_type)
39
54
 
40
- message = serialization_provider.serialize(qm)
55
+ _event_type = EventQ.create_event_type(event_type)
41
56
 
42
- ex.publish(message, :routing_key => _event_type)
43
- rescue => e
57
+ with_connection do |channel|
58
+ exchange = @queue_manager.get_queue_exchange(channel, queue)
44
59
 
45
- channel.close
46
- connection.close
47
- raise e
48
- end
60
+ delay_exchange = @queue_manager.get_delay_exchange(channel, queue, delay)
61
+
62
+ delay_queue = @queue_manager.create_delay_queue(channel, queue, exchange.name, delay)
63
+ delay_queue.bind(delay_exchange, routing_key: _event_type)
64
+
65
+ _queue_name = EventQ.create_queue_name(queue.name)
66
+
67
+ q = channel.queue(_queue_name, durable: @queue_manager.durable)
68
+ q.bind(exchange, routing_key: _event_type)
49
69
 
50
- channel.close
51
- connection.close
70
+ message = serialized_message(_event_type, event)
52
71
 
53
- EventQ.logger.debug "[#{self.class}] - Raised event. Message: #{message} | Type: #{event_type}."
72
+ delay_exchange.publish(message, routing_key: _event_type)
54
73
 
55
- return true
74
+ EventQ.logger.debug "[#{self.class}] - Raised event. Message: #{message} | Type: #{event_type} | Delay: #{delay}."
75
+ end
56
76
  end
57
77
 
58
78
  def new_message
59
79
  EventQ::QueueMessage.new
60
80
  end
61
81
 
82
+ private
83
+
84
+ def with_connection
85
+ connection = @client.get_connection
86
+
87
+ begin
88
+ channel = connection.create_channel
89
+
90
+ yield(channel)
91
+
92
+ ensure
93
+ channel&.close
94
+ connection.close
95
+ end
96
+
97
+ true
98
+ end
99
+
100
+ def serialized_message(event_type, event)
101
+ qm = new_message
102
+ qm.content = event
103
+ qm.type = event_type
104
+
105
+ if EventQ::Configuration.signature_secret != nil
106
+ provider = @signature_manager.get_provider(EventQ::Configuration.signature_provider)
107
+ qm.signature = provider.write(message: qm, secret: EventQ::Configuration.signature_secret)
108
+ end
109
+
110
+ serialization_provider = @serialization_manager.get_provider(EventQ::Configuration.serialization_provider)
111
+
112
+ serialization_provider.serialize(qm)
113
+ end
62
114
  end
63
115
  end
64
116
  end
@@ -32,6 +32,11 @@ module EventQ
32
32
  return q
33
33
  end
34
34
 
35
+ def get_queue_exchange(channel, queue)
36
+ _exchange_name = EventQ.create_exchange_name(queue.name)
37
+ channel.direct("#{_exchange_name}.ex")
38
+ end
39
+
35
40
  def get_retry_exchange(channel, queue)
36
41
  _queue_name = EventQ.create_queue_name(queue.name)
37
42
  return channel.fanout("#{_queue_name}.r.ex")
@@ -42,6 +47,11 @@ module EventQ
42
47
  return channel.fanout("#{_queue_name}.ex")
43
48
  end
44
49
 
50
+ def get_delay_exchange(channel, queue, delay)
51
+ _queue_name = EventQ.create_queue_name(queue.name)
52
+ channel.direct("#{_queue_name}.#{delay}.d.ex")
53
+ end
54
+
45
55
  def get_retry_queue(channel, queue)
46
56
  subscriber_exchange = get_subscriber_exchange(channel, queue)
47
57
 
@@ -63,11 +73,16 @@ module EventQ
63
73
 
64
74
  end
65
75
 
76
+ def create_delay_queue(channel, queue, dlx_name, delay=0)
77
+ queue_name = EventQ.create_queue_name(queue.name)
78
+ channel.queue("#{queue_name}.#{delay}.delay", durable: @durable,
79
+ arguments: { X_DEAD_LETTER_EXCHANGE => dlx_name, X_MESSAGE_TTL => delay * 1000 })
80
+ end
81
+
66
82
  def get_exchange(channel, exchange)
67
83
  _exchange_name = EventQ.create_exchange_name(exchange.name)
68
84
  return channel.direct(_exchange_name, :durable => @durable)
69
85
  end
70
-
71
86
  end
72
87
  end
73
88
  end
@@ -1,3 +1,3 @@
1
1
  module EventqRabbitmq
2
- VERSION = "1.13.0"
2
+ VERSION = "1.14.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventq_rabbitmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-01 00:00:00.000000000 Z
11
+ date: 2017-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler