cyclone_lariat 0.4.0 → 1.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/gem-push.yml +4 -4
  3. data/.rubocop.yml +9 -5
  4. data/Gemfile.lock +123 -21
  5. data/Guardfile +42 -0
  6. data/README.md +417 -220
  7. data/bin/cyclone_lariat +75 -43
  8. data/cyclone_lariat.gemspec +10 -3
  9. data/lib/cyclone_lariat/clients/abstract.rb +40 -0
  10. data/lib/cyclone_lariat/clients/sns.rb +163 -0
  11. data/lib/cyclone_lariat/clients/sqs.rb +114 -0
  12. data/lib/cyclone_lariat/core.rb +21 -0
  13. data/lib/cyclone_lariat/errors.rb +16 -0
  14. data/lib/cyclone_lariat/fake.rb +19 -0
  15. data/lib/cyclone_lariat/generators/command.rb +53 -0
  16. data/lib/cyclone_lariat/generators/event.rb +52 -0
  17. data/lib/cyclone_lariat/generators/queue.rb +30 -0
  18. data/lib/cyclone_lariat/generators/topic.rb +29 -0
  19. data/lib/cyclone_lariat/messages/v1/abstract.rb +139 -0
  20. data/lib/cyclone_lariat/messages/v1/command.rb +20 -0
  21. data/lib/cyclone_lariat/messages/v1/event.rb +20 -0
  22. data/lib/cyclone_lariat/messages/v1/validator.rb +31 -0
  23. data/lib/cyclone_lariat/messages/v2/abstract.rb +149 -0
  24. data/lib/cyclone_lariat/messages/v2/command.rb +20 -0
  25. data/lib/cyclone_lariat/messages/v2/event.rb +20 -0
  26. data/lib/cyclone_lariat/messages/v2/validator.rb +39 -0
  27. data/lib/cyclone_lariat/middleware.rb +9 -6
  28. data/lib/cyclone_lariat/migration.rb +54 -117
  29. data/lib/cyclone_lariat/options.rb +52 -0
  30. data/lib/cyclone_lariat/presenters/graph.rb +54 -0
  31. data/lib/cyclone_lariat/presenters/queues.rb +41 -0
  32. data/lib/cyclone_lariat/presenters/subscriptions.rb +34 -0
  33. data/lib/cyclone_lariat/presenters/topics.rb +40 -0
  34. data/lib/cyclone_lariat/publisher.rb +25 -0
  35. data/lib/cyclone_lariat/repo/active_record/messages.rb +92 -0
  36. data/lib/cyclone_lariat/repo/active_record/versions.rb +28 -0
  37. data/lib/cyclone_lariat/repo/messages.rb +43 -0
  38. data/lib/cyclone_lariat/repo/messages_mapper.rb +49 -0
  39. data/lib/cyclone_lariat/repo/sequel/messages.rb +73 -0
  40. data/lib/cyclone_lariat/repo/sequel/versions.rb +28 -0
  41. data/lib/cyclone_lariat/repo/versions.rb +42 -0
  42. data/lib/cyclone_lariat/resources/queue.rb +167 -0
  43. data/lib/cyclone_lariat/resources/topic.rb +132 -0
  44. data/lib/cyclone_lariat/services/migrate.rb +51 -0
  45. data/lib/cyclone_lariat/services/rollback.rb +51 -0
  46. data/lib/cyclone_lariat/version.rb +1 -1
  47. data/lib/cyclone_lariat.rb +4 -11
  48. data/lib/tasks/console.rake +1 -1
  49. data/lib/tasks/cyclone_lariat.rake +10 -12
  50. data/lib/tasks/db.rake +0 -15
  51. metadata +127 -27
  52. data/config/db.example.rb +0 -9
  53. data/config/initializers/sequel.rb +0 -7
  54. data/db/migrate/01_add_uuid_extensions.rb +0 -15
  55. data/db/migrate/02_add_events.rb +0 -19
  56. data/db/migrate/03_add_versions.rb +0 -9
  57. data/docs/_imgs/graphviz_01.png +0 -0
  58. data/docs/_imgs/graphviz_02.png +0 -0
  59. data/docs/_imgs/graphviz_03.png +0 -0
  60. data/docs/_imgs/lariat.jpg +0 -0
  61. data/docs/_imgs/logic.png +0 -0
  62. data/docs/_imgs/sqs_sns_diagram.png +0 -0
  63. data/lib/cyclone_lariat/abstract/client.rb +0 -112
  64. data/lib/cyclone_lariat/abstract/message.rb +0 -98
  65. data/lib/cyclone_lariat/command.rb +0 -13
  66. data/lib/cyclone_lariat/configure.rb +0 -15
  67. data/lib/cyclone_lariat/event.rb +0 -13
  68. data/lib/cyclone_lariat/messages_mapper.rb +0 -46
  69. data/lib/cyclone_lariat/messages_repo.rb +0 -60
  70. data/lib/cyclone_lariat/queue.rb +0 -147
  71. data/lib/cyclone_lariat/sns_client.rb +0 -149
  72. data/lib/cyclone_lariat/sqs_client.rb +0 -93
  73. data/lib/cyclone_lariat/topic.rb +0 -113
metadata CHANGED
@@ -1,15 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyclone_lariat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kudrin
8
8
  - Philip Sorokin
9
+ - Kirill Drozdov
10
+ - Vitaly Perminov
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
- date: 2022-11-15 00:00:00.000000000 Z
14
+ date: 2023-01-30 00:00:00.000000000 Z
13
15
  dependencies:
14
16
  - !ruby/object:Gem::Dependency
15
17
  name: aws-sdk-sns
@@ -53,6 +55,20 @@ dependencies:
53
55
  - - "~>"
54
56
  - !ruby/object:Gem::Version
55
57
  version: '0.6'
58
+ - !ruby/object:Gem::Dependency
59
+ name: dry-validation
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '1.5'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.5'
56
72
  - !ruby/object:Gem::Dependency
57
73
  name: luna_park
58
74
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +125,20 @@ dependencies:
109
125
  - - "~>"
110
126
  - !ruby/object:Gem::Version
111
127
  version: '11.1'
128
+ - !ruby/object:Gem::Dependency
129
+ name: database_cleaner-active_record
130
+ requirement: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
112
142
  - !ruby/object:Gem::Dependency
113
143
  name: database_cleaner-sequel
114
144
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +153,62 @@ dependencies:
123
153
  - - "~>"
124
154
  - !ruby/object:Gem::Version
125
155
  version: '2.0'
156
+ - !ruby/object:Gem::Dependency
157
+ name: guard
158
+ requirement: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ type: :development
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ - !ruby/object:Gem::Dependency
171
+ name: guard-bundler
172
+ requirement: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ type: :development
178
+ prerelease: false
179
+ version_requirements: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ - !ruby/object:Gem::Dependency
185
+ name: guard-rspec
186
+ requirement: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ type: :development
192
+ prerelease: false
193
+ version_requirements: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ - !ruby/object:Gem::Dependency
199
+ name: guard-rubocop
200
+ requirement: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ type: :development
206
+ prerelease: false
207
+ version_requirements: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
126
212
  - !ruby/object:Gem::Dependency
127
213
  name: pg
128
214
  requirement: !ruby/object:Gem::Requirement
@@ -281,6 +367,7 @@ files:
281
367
  - Dockerfile
282
368
  - Gemfile
283
369
  - Gemfile.lock
370
+ - Guardfile
284
371
  - README.md
285
372
  - Rakefile
286
373
  - bin/cyclone_lariat
@@ -288,34 +375,46 @@ files:
288
375
  - bin/docker/init
289
376
  - bin/docker/privatekey
290
377
  - bin/docker/rm
291
- - config/db.example.rb
292
- - config/initializers/sequel.rb
293
378
  - cyclone_lariat.gemspec
294
- - db/migrate/01_add_uuid_extensions.rb
295
- - db/migrate/02_add_events.rb
296
- - db/migrate/03_add_versions.rb
297
379
  - docker-compose.yml
298
- - docs/_imgs/graphviz_01.png
299
- - docs/_imgs/graphviz_02.png
300
- - docs/_imgs/graphviz_03.png
301
- - docs/_imgs/lariat.jpg
302
- - docs/_imgs/logic.png
303
- - docs/_imgs/sqs_sns_diagram.png
304
380
  - lib/cyclone_lariat.rb
305
- - lib/cyclone_lariat/abstract/client.rb
306
- - lib/cyclone_lariat/abstract/message.rb
307
- - lib/cyclone_lariat/command.rb
308
- - lib/cyclone_lariat/configure.rb
381
+ - lib/cyclone_lariat/clients/abstract.rb
382
+ - lib/cyclone_lariat/clients/sns.rb
383
+ - lib/cyclone_lariat/clients/sqs.rb
384
+ - lib/cyclone_lariat/core.rb
309
385
  - lib/cyclone_lariat/errors.rb
310
- - lib/cyclone_lariat/event.rb
311
- - lib/cyclone_lariat/messages_mapper.rb
312
- - lib/cyclone_lariat/messages_repo.rb
386
+ - lib/cyclone_lariat/fake.rb
387
+ - lib/cyclone_lariat/generators/command.rb
388
+ - lib/cyclone_lariat/generators/event.rb
389
+ - lib/cyclone_lariat/generators/queue.rb
390
+ - lib/cyclone_lariat/generators/topic.rb
391
+ - lib/cyclone_lariat/messages/v1/abstract.rb
392
+ - lib/cyclone_lariat/messages/v1/command.rb
393
+ - lib/cyclone_lariat/messages/v1/event.rb
394
+ - lib/cyclone_lariat/messages/v1/validator.rb
395
+ - lib/cyclone_lariat/messages/v2/abstract.rb
396
+ - lib/cyclone_lariat/messages/v2/command.rb
397
+ - lib/cyclone_lariat/messages/v2/event.rb
398
+ - lib/cyclone_lariat/messages/v2/validator.rb
313
399
  - lib/cyclone_lariat/middleware.rb
314
400
  - lib/cyclone_lariat/migration.rb
315
- - lib/cyclone_lariat/queue.rb
316
- - lib/cyclone_lariat/sns_client.rb
317
- - lib/cyclone_lariat/sqs_client.rb
318
- - lib/cyclone_lariat/topic.rb
401
+ - lib/cyclone_lariat/options.rb
402
+ - lib/cyclone_lariat/presenters/graph.rb
403
+ - lib/cyclone_lariat/presenters/queues.rb
404
+ - lib/cyclone_lariat/presenters/subscriptions.rb
405
+ - lib/cyclone_lariat/presenters/topics.rb
406
+ - lib/cyclone_lariat/publisher.rb
407
+ - lib/cyclone_lariat/repo/active_record/messages.rb
408
+ - lib/cyclone_lariat/repo/active_record/versions.rb
409
+ - lib/cyclone_lariat/repo/messages.rb
410
+ - lib/cyclone_lariat/repo/messages_mapper.rb
411
+ - lib/cyclone_lariat/repo/sequel/messages.rb
412
+ - lib/cyclone_lariat/repo/sequel/versions.rb
413
+ - lib/cyclone_lariat/repo/versions.rb
414
+ - lib/cyclone_lariat/resources/queue.rb
415
+ - lib/cyclone_lariat/resources/topic.rb
416
+ - lib/cyclone_lariat/services/migrate.rb
417
+ - lib/cyclone_lariat/services/rollback.rb
319
418
  - lib/cyclone_lariat/version.rb
320
419
  - lib/tasks/console.rake
321
420
  - lib/tasks/cyclone_lariat.rake
@@ -336,11 +435,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
336
435
  version: 2.4.0
337
436
  required_rubygems_version: !ruby/object:Gem::Requirement
338
437
  requirements:
339
- - - ">="
438
+ - - ">"
340
439
  - !ruby/object:Gem::Version
341
- version: '0'
440
+ version: 1.3.1
342
441
  requirements: []
343
- rubygems_version: 3.0.3.1
442
+ rubyforge_project:
443
+ rubygems_version: 2.6.14.4
344
444
  signing_key:
345
445
  specification_version: 4
346
446
  summary: Shoryuken middleware for LunaPark based application.
data/config/db.example.rb DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- DB_CONF = {
4
- adapter: 'postgresql',
5
- host: ENV.fetch('DB_HOST', 'host'),
6
- username: ENV.fetch('DB_USER', 'postgres'),
7
- password: ENV.fetch('DB_PASSWORD', 'password'),
8
- database: ENV.fetch('DB_NAME', 'cyclone-lariat-test')
9
- }.freeze
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../db'
4
-
5
- require 'sequel'
6
-
7
- DB = Sequel.connect(DB_CONF)
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Sequel.migration do
4
- up do
5
- run <<-SQL
6
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
7
- SQL
8
- end
9
-
10
- down do
11
- run <<-SQL
12
- DROP EXTENSION IF EXISTS "uuid-ossp";
13
- SQL
14
- end
15
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Sequel.migration do
4
- change do
5
- create_table :async_messages do
6
- column :uuid, :uuid, primary_key: true
7
- String :kind, null: false
8
- String :type, null: false
9
- Integer :version, null: false
10
- String :publisher, null: false
11
- column :data, :json, null: false
12
- String :client_error_message, null: true, default: nil
13
- column :client_error_details, :json, null: true, default: nil
14
- DateTime :sent_at, null: true, default: nil
15
- DateTime :received_at, null: false, default: Sequel::CURRENT_TIMESTAMP
16
- DateTime :processed_at, null: true, default: nil
17
- end
18
- end
19
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Sequel.migration do
4
- change do
5
- create_table :lariat_versions do
6
- Integer :version, null: false, unique: true
7
- end
8
- end
9
- end
Binary file
Binary file
Binary file
Binary file
data/docs/_imgs/logic.png DELETED
Binary file
Binary file
@@ -1,112 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # require 'aws-sdk-sns'
4
- require 'luna_park/extensions/injector'
5
- require_relative '../event'
6
- require_relative '../command'
7
- require_relative '../errors'
8
-
9
- module CycloneLariat
10
- module Abstract
11
- class Client
12
- include LunaPark::Extensions::Injector
13
-
14
- dependency(:aws_client_class) { raise ArgumentError, 'Client class should be defined' }
15
- dependency(:aws_credentials_class) { Aws::Credentials }
16
-
17
- def initialize(key: nil, secret_key: nil, region: nil, version: nil, publisher: nil, instance: nil, account_id: nil)
18
- @key = key
19
- @secret_key = secret_key
20
- @region = region
21
- @version = version
22
- @publisher = publisher
23
- @instance = instance
24
- @account_id = account_id
25
- end
26
-
27
- def event(type, data: {}, version: self.version, request_id: nil, uuid: SecureRandom.uuid)
28
- params = {
29
- uuid: uuid,
30
- type: type,
31
- sent_at: Time.now.iso8601(3),
32
- version: version,
33
- publisher: publisher,
34
- data: data,
35
- request_id: request_id
36
- }
37
-
38
- Event.wrap(params.compact)
39
- end
40
-
41
- def command(type, data: {}, version: self.version, request_id: nil, uuid: SecureRandom.uuid)
42
- params = {
43
- uuid: uuid,
44
- type: type,
45
- sent_at: Time.now.iso8601(3),
46
- version: version,
47
- publisher: publisher,
48
- data: data,
49
- request_id: request_id
50
- }
51
-
52
- Command.wrap(params.compact)
53
- end
54
-
55
- def publish
56
- raise LunaPark::Errors::AbstractMethod, 'Publish method should be defined'
57
- end
58
-
59
- class << self
60
- def version(version = nil)
61
- version.nil? ? @version || CycloneLariat.default_version : @version = version
62
- end
63
-
64
- def instance(instance = nil)
65
- instance.nil? ? @instance || CycloneLariat.default_instance || (raise 'You should define instance') : @instance = instance
66
- end
67
-
68
- def publisher(publisher = nil)
69
- publisher.nil? ? @publisher || CycloneLariat.publisher || (raise 'You should define publisher') : @publisher = publisher
70
- end
71
- end
72
-
73
- def version
74
- @version ||= self.class.version
75
- end
76
-
77
- def publisher
78
- @publisher ||= self.class.publisher
79
- end
80
-
81
- def instance
82
- @instance ||= self.class.instance
83
- end
84
-
85
- def key
86
- @key ||= CycloneLariat.aws_key
87
- end
88
-
89
- def secret_key
90
- @secret_key ||= CycloneLariat.aws_secret_key
91
- end
92
-
93
- def region
94
- @region ||= CycloneLariat.aws_default_region
95
- end
96
-
97
- def account_id
98
- @account_id ||= CycloneLariat.aws_account_id
99
- end
100
-
101
- private
102
-
103
- def aws_client
104
- @aws_client ||= aws_client_class.new(credentials: aws_credentials, region: region)
105
- end
106
-
107
- def aws_credentials
108
- @aws_credentials ||= aws_credentials_class.new(key, secret_key)
109
- end
110
- end
111
- end
112
- end
@@ -1,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'luna_park/entities/attributable'
4
- require_relative '../errors'
5
-
6
- module CycloneLariat
7
- module Abstract
8
- class Message < LunaPark::Entities::Attributable
9
- attr :uuid, String, :new
10
- attr :publisher, String, :new
11
- attr :type, String, :new
12
- attrs :client_error, :version, :data, :request_id,
13
- :sent_at, :processed_at, :received_at
14
-
15
- def kind
16
- raise LunaPark::Errors::AbstractMethod
17
- end
18
-
19
- def version=(value)
20
- @version = Integer(value)
21
- end
22
-
23
- def sent_at=(value)
24
- @sent_at = wrap_time(value)
25
- end
26
-
27
- def received_at=(value)
28
- @received_at = wrap_time(value)
29
- end
30
-
31
- def processed_at=(value)
32
- @processed_at = wrap_time(value)
33
- end
34
-
35
- def request_at=(value)
36
- @request_id = wrap_string(value)
37
- end
38
-
39
- def processed?
40
- !@processed_at.nil?
41
- end
42
-
43
- def client_error_message=(txt)
44
- return unless txt
45
-
46
- @client_error ||= Errors::ClientError.new
47
- @client_error.message = txt
48
- end
49
-
50
- def client_error_details=(details)
51
- return unless details
52
-
53
- @client_error ||= Errors::ClientError.new
54
- @client_error.details = details
55
- end
56
-
57
- def ==(other)
58
- kind == other.kind &&
59
- uuid == other.uuid &&
60
- publisher == other.publisher &&
61
- type == other.type &&
62
- client_error&.message == other.client_error&.message &&
63
- version == other.version &&
64
- sent_at.to_i == other.sent_at.to_i &&
65
- received_at.to_i == other.received_at.to_i &&
66
- processed_at.to_i == other.processed_at.to_i
67
- end
68
-
69
- def to_json(*args)
70
- hash = serialize
71
- hash[:type] = [kind, hash[:type]].join '_'
72
- hash[:sent_at] = hash[:sent_at].iso8601(3) if hash[:sent_at]
73
- hash[:received_at] = hash[:received_at].iso8601(3) if hash[:received_at]
74
- hash[:processed_at] = hash[:processed_at].iso8601(3) if hash[:processed_at]
75
- hash.to_json(*args)
76
- end
77
-
78
- private
79
-
80
- def wrap_time(value)
81
- case value
82
- when String then Time.parse(value)
83
- when Time then value
84
- when NilClass then nil
85
- else raise ArgumentError, "Unknown type `#{value.class}`"
86
- end
87
- end
88
-
89
- def wrap_string(value)
90
- case value
91
- when String then String(value)
92
- when NilClass then nil
93
- else raise ArgumentError, "Unknown type `#{value.class}`"
94
- end
95
- end
96
- end
97
- end
98
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'abstract/message'
4
-
5
- module CycloneLariat
6
- class Command < Abstract::Message
7
- KIND = 'command'
8
-
9
- def kind
10
- KIND
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CycloneLariat
4
- class << self
5
- DEFAULT_VERSION = 1
6
-
7
- attr_accessor :aws_key, :aws_secret_key, :publisher, :aws_default_region, :default_instance,
8
- :aws_account_id, :events_dataset, :versions_dataset
9
- attr_writer :default_version
10
-
11
- def default_version
12
- @default_version ||= DEFAULT_VERSION
13
- end
14
- end
15
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'abstract/message'
4
-
5
- module CycloneLariat
6
- class Event < Abstract::Message
7
- KIND = 'event'
8
-
9
- def kind
10
- KIND
11
- end
12
- end
13
- end
@@ -1,46 +0,0 @@
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
- {
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
- end
27
-
28
- private
29
-
30
- def hash_from_json_column(data)
31
- return JSON.parse(data) if data.is_a?(String)
32
-
33
- if pg_json_extension_enabled?
34
- return data.to_h if data.is_a?(Sequel::Postgres::JSONHash)
35
- return JSON.parse(data.to_s) if data.is_a?(Sequel::Postgres::JSONString)
36
- end
37
-
38
- raise ArgumentError, "Unknown type of `#{data}`"
39
- end
40
-
41
- def pg_json_extension_enabled?
42
- Object.const_defined?('Sequel::Postgres::JSONHash')
43
- end
44
- end
45
- end
46
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'event'
4
- require_relative 'messages_mapper'
5
-
6
- module CycloneLariat
7
- class MessagesRepo
8
- attr_reader :dataset
9
-
10
- def initialize(dataset)
11
- @dataset = dataset
12
- end
13
-
14
- def create(msg)
15
- dataset.insert MessagesMapper.to_row(msg)
16
- end
17
-
18
- def exists?(uuid:)
19
- dataset.where(uuid: uuid).limit(1).any?
20
- end
21
-
22
- def processed!(uuid:, error: nil)
23
- data = { processed_at: Sequel.function(:NOW) }
24
- data.merge!(client_error_message: error.message, client_error_details: JSON.generate(error.details)) if error
25
-
26
- !dataset.where(uuid: uuid).update(data).zero?
27
- end
28
-
29
- def find(uuid:)
30
- row = dataset.where(uuid: uuid).first
31
- return if row.nil?
32
-
33
- build MessagesMapper.from_row(row)
34
- end
35
-
36
- def each_unprocessed
37
- dataset.where(processed_at: nil).each do |row|
38
- msg = build MessagesMapper.from_row(row)
39
- yield(msg)
40
- end
41
- end
42
-
43
- def each_with_client_errors
44
- dataset.where { (processed_at !~ nil) & (client_error_message !~ nil) }.each do |row|
45
- msg = build MessagesMapper.from_row(row)
46
- yield(msg)
47
- end
48
- end
49
-
50
- private
51
-
52
- def build(raw)
53
- case kind = raw.delete(:kind)
54
- when 'event' then Event.wrap raw
55
- when 'command' then Command.wrap raw
56
- else raise ArgumentError, "Unknown kind `#{kind}` of message"
57
- end
58
- end
59
- end
60
- end