cyclone_lariat 0.3.0 → 0.3.4

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
  SHA256:
3
- metadata.gz: 51c2975656b4c950bc5dd8959d06450260fb5ac823005d9c2372b263944b64e4
4
- data.tar.gz: 4ac631178ba3087ba437cbf9c2e0a502032c53ee133b71d46adcdceb7e1079bc
3
+ metadata.gz: de7e05c5fba57a9db6c827549404f12b1f88be0336b00d2d5dfb4dc77132b328
4
+ data.tar.gz: 54d81b79a812fdf91513cdee886933d32055fd405c55bb032aae4dec81ac1ff3
5
5
  SHA512:
6
- metadata.gz: 125fdc253fdbd93896c7de8c715f0d1cf64288e658f0b870ef1d6ae32351e85d24611be9ababca096b813c1c863f346ca92af604ac91dc60dc0282c8f2c4e3dc
7
- data.tar.gz: 7435eeadbf5047dc7b9e99488d32a8886cd2a90cef40f627f65aaa20459f220893bd417fbfe1b6b4f2d1a5665051d2cbdc34e7b96968a4a303352f579456b7ea
6
+ metadata.gz: 3d8ce1ebcd03c53309165592b7e4e0cbe342a3033694afdc639e033fb76a88dd1f340ee2204c5602f02399a5e9e56f47a7483dba867d9aafcd9947b3958933c1
7
+ data.tar.gz: 8c8668274a9435e56804739e6625703b733810e46401aca602c04f51763274dafa9456f625fc315ae7b322c62d4a426a582a62db8b6061716ad46aced0ab5ef0
data/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.3.3] - 2021-07-14
8
+ Changed
9
+ - Bugfix of message equality check
10
+
11
+ ## [0.3.2] - 2021-06-11
12
+ Changed
13
+ - Bugfix
14
+
15
+ ## [0.3.1] - 2021-06-11
16
+ Changed
17
+ - Command
18
+ - SqsClient
7
19
 
8
20
  ## [0.3.0] - 2021-06-09
9
21
  Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cyclone_lariat (0.3.0)
4
+ cyclone_lariat (0.3.2)
5
5
  aws-sdk-sns
6
6
  aws-sdk-sqs
7
7
  luna_park (~> 0.11)
data/README.md CHANGED
@@ -128,7 +128,8 @@ As you see all params identity. And you can easily change your sqs-queue to sns-
128
128
  subscribes. But you should define destination.
129
129
 
130
130
  ```ruby
131
- client.publish_event 'email_is_created', data: { mail: 'john.doe@example.com' }, dest: 'notify_service'
131
+ client.publish_event 'email_is_created', data: { mail: 'john.doe@example.com' }, dest: 'notify_service'
132
+ # prod-event-queue-pilot-email_is_created-notify_service
132
133
  ```
133
134
 
134
135
  Or you can define topic directly:
@@ -158,7 +159,6 @@ class Receiver
158
159
  queue: 'your_sqs_queue_name'
159
160
 
160
161
  server_middleware do |chain|
161
-
162
162
  # Options dataset, errors_notifier and message_notifier is optionals.
163
163
  # If you dont define notifiers - middleware does not notify
164
164
  # If you dont define dataset - middleware does store events in db
@@ -9,13 +9,8 @@ module CycloneLariat
9
9
  attr :uuid, String, :new
10
10
  attr :publisher, String, :new
11
11
  attr :type, String, :new
12
- attr :client_error
13
- attr :version
14
- attr :data
15
-
16
- attr_reader :sent_at,
17
- :processed_at,
18
- :received_at
12
+ attrs :client_error, :version, :data,
13
+ :sent_at, :processed_at, :received_at
19
14
 
20
15
  def kind
21
16
  raise LunaPark::Errors::AbstractMethod
@@ -37,6 +32,10 @@ module CycloneLariat
37
32
  @processed_at = wrap_time(value)
38
33
  end
39
34
 
35
+ def processed?
36
+ !@processed_at.nil?
37
+ end
38
+
40
39
  def client_error_message=(txt)
41
40
  return unless txt
42
41
 
@@ -57,11 +56,10 @@ module CycloneLariat
57
56
  publisher == other.publisher &&
58
57
  type == other.type &&
59
58
  client_error&.message == other.client_error&.message &&
60
- client_error&.details == other.client_error&.details &&
61
59
  version == other.version &&
62
60
  sent_at.to_i == other.sent_at.to_i &&
63
- received_at.to_i == other.received_at.to_i
64
- processed_at.to_i == other.processed_at.to_i
61
+ received_at.to_i == other.received_at.to_i &&
62
+ processed_at.to_i == other.processed_at.to_i
65
63
  end
66
64
 
67
65
  def to_json(*args)
@@ -77,7 +75,7 @@ module CycloneLariat
77
75
  when String then Time.parse(value)
78
76
  when Time then value
79
77
  when NilClass then nil
80
- else raise Argumentevent.rbError, "Unknown type `#{value.class}`"
78
+ else raise ArgumentError, "Unknown type `#{value.class}`"
81
79
  end
82
80
  end
83
81
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CycloneLariat
4
+ class MessagesMapper
5
+ class << self
6
+ def from_row(row)
7
+ return if row.nil?
8
+
9
+ row[:data] = hash_from_json_column(row[:data])
10
+ row[:client_error_details] = hash_from_json_column(row[:client_error_details]) if row[:client_error_details]
11
+ row
12
+ end
13
+
14
+ def to_row(input)
15
+ row = {
16
+ uuid: input.uuid,
17
+ kind: input.kind,
18
+ type: input.type,
19
+ publisher: input.publisher,
20
+ data: JSON.generate(input.data),
21
+ client_error_message: input.client_error&.message,
22
+ client_error_details: JSON.generate(input.client_error&.details),
23
+ version: input.version,
24
+ sent_at: input.sent_at
25
+ }
26
+ row
27
+ end
28
+
29
+ private
30
+
31
+ def hash_from_json_column(data)
32
+ return JSON.parse(data) if data.is_a?(String)
33
+
34
+ if pg_json_extension_enabled?
35
+ return data.to_h if data.is_a?(Sequel::Postgres::JSONHash)
36
+ return JSON.parse(data.to_s) if data.is_a?(Sequel::Postgres::JSONString)
37
+ end
38
+
39
+ raise ArgumentError, "Unknown type of `#{data}`"
40
+ end
41
+
42
+ def pg_json_extension_enabled?
43
+ Object.const_defined?('Sequel::Postgres::JSONHash')
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'event'
4
+ require_relative 'messages_mapper'
4
5
 
5
6
  module CycloneLariat
6
7
  class MessagesRepo
@@ -11,17 +12,7 @@ module CycloneLariat
11
12
  end
12
13
 
13
14
  def create(msg)
14
- dataset.insert(
15
- uuid: msg.uuid,
16
- kind: msg.kind,
17
- type: msg.type,
18
- publisher: msg.publisher,
19
- data: JSON.generate(msg.data),
20
- client_error_message: msg.client_error&.message,
21
- client_error_details: JSON.generate(msg.client_error&.details),
22
- version: msg.version,
23
- sent_at: msg.sent_at
24
- )
15
+ dataset.insert MessagesMapper.to_row(msg)
25
16
  end
26
17
 
27
18
  def exists?(uuid:)
@@ -36,32 +27,20 @@ module CycloneLariat
36
27
  end
37
28
 
38
29
  def find(uuid:)
39
- raw = dataset.where(uuid: uuid).first
40
- raw[:data] = JSON.parse(raw[:data], symbolize_names: true)
41
- if raw[:client_error_details]
42
- raw[:client_error_details] = JSON.parse(raw[:client_error_details], symbolize_names: true)
43
- end
44
- build raw
30
+ row = dataset.where(uuid: uuid).first
31
+ build MessagesMapper.from_row(row)
45
32
  end
46
33
 
47
34
  def each_unprocessed
48
- dataset.where(processed_at: nil).each do |raw|
49
- raw[:data] = JSON.parse(raw[:data], symbolize_names: true)
50
- if raw[:client_error_details]
51
- raw[:client_error_details] = JSON.parse(raw[:client_error_details], symbolize_names: true)
52
- end
53
- msg = build raw
35
+ dataset.where(processed_at: nil).each do |row|
36
+ msg = build MessagesMapper.from_row(row)
54
37
  yield(msg)
55
38
  end
56
39
  end
57
40
 
58
41
  def each_with_client_errors
59
- dataset.where { (processed_at !~ nil) & (client_error_message !~ nil) }.each do |raw|
60
- raw[:data] = JSON.parse(raw[:data], symbolize_names: true)
61
- if raw[:client_error_details]
62
- raw[:client_error_details] = JSON.parse(raw[:client_error_details], symbolize_names: true)
63
- end
64
- msg = build raw
42
+ dataset.where { (processed_at !~ nil) & (client_error_message !~ nil) }.each do |row|
43
+ msg = build MessagesMapper.from_row(row)
65
44
  yield(msg)
66
45
  end
67
46
  end
@@ -13,15 +13,15 @@ module CycloneLariat
13
13
  end
14
14
 
15
15
  def call(_worker_instance, queue, _sqs_msg, body, &block)
16
- log_received_message queue, body
16
+ msg = receive_message(body)
17
17
 
18
- catch_standard_error(queue, body) do
19
- return true unless check(body[:Message])
18
+ message_notifier&.info 'Receive message', message: msg, queue: queue
20
19
 
21
- event = Event.wrap(JSON.parse(body[:Message]))
20
+ catch_standard_error(queue, msg) do
21
+ event = Event.wrap(msg)
22
22
 
23
- catch_business_error(event) do
24
- store_in_dataset(event, &block)
23
+ store_in_dataset(event) do
24
+ catch_business_error(event, &block)
25
25
  end
26
26
  end
27
27
  end
@@ -30,15 +30,17 @@ module CycloneLariat
30
30
 
31
31
  attr_reader :errors_notifier, :message_notifier, :events_repo
32
32
 
33
- def log_received_message(queue, body)
34
- message_notifier&.info 'Receive message', queue: queue, aws_message_id: body[:MessageId], message: body[:Message]
33
+ def receive_message(body)
34
+ body[:Message] ? JSON.parse(body[:Message], symbolize_names: true ) : body
35
35
  end
36
36
 
37
37
  def store_in_dataset(event)
38
38
  return yield if events_repo.nil?
39
- return true if events_repo.exists?(uuid: event.uuid)
40
39
 
41
- events_repo.create(event)
40
+ existed = events_repo.find(uuid: event.uuid)
41
+ return true if existed&.processed?
42
+
43
+ events_repo.create(event) unless existed
42
44
  yield
43
45
  events_repo.processed! uuid: event.uuid, error: event.client_error
44
46
  end
@@ -50,20 +52,11 @@ module CycloneLariat
50
52
  event.client_error = e
51
53
  end
52
54
 
53
- def catch_standard_error(queue, body)
55
+ def catch_standard_error(queue, msg)
54
56
  yield
55
- rescue StandardError => e
56
- errors_notifier&.error(e, queue: queue, aws_message_id: body[:MessageId], message: body[:Message])
57
+ rescue Exception => e
58
+ errors_notifier&.error(e, queue: queue, message: msg)
57
59
  raise e
58
60
  end
59
-
60
- def check(msg)
61
- if msg.nil? || msg.empty?
62
- errors_notifier&.error(Errors::EmptyMessage.new)
63
- false
64
- else
65
- true
66
- end
67
- end
68
61
  end
69
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CycloneLariat
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.4'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require_relative 'cyclone_lariat/sns_client'
4
4
  require_relative 'cyclone_lariat/errors'
5
5
  require_relative 'cyclone_lariat/event'
6
+ require_relative 'cyclone_lariat/messages_mapper'
6
7
  require_relative 'cyclone_lariat/messages_repo'
7
8
  require_relative 'cyclone_lariat/middleware'
8
9
  require_relative 'cyclone_lariat/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyclone_lariat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kudrin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-06-10 00:00:00.000000000 Z
12
+ date: 2021-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk-sns
@@ -279,6 +279,7 @@ files:
279
279
  - lib/cyclone_lariat/command.rb
280
280
  - lib/cyclone_lariat/errors.rb
281
281
  - lib/cyclone_lariat/event.rb
282
+ - lib/cyclone_lariat/messages_mapper.rb
282
283
  - lib/cyclone_lariat/messages_repo.rb
283
284
  - lib/cyclone_lariat/middleware.rb
284
285
  - lib/cyclone_lariat/sns_client.rb