realm-core 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realm
4
+ class MultiWorker
5
+ def initialize(workers = [])
6
+ @workers = workers
7
+ end
8
+
9
+ def start(*args)
10
+ @workers.each { |w| w.start(*args) }
11
+ self
12
+ end
13
+
14
+ def stop(timeout: 30)
15
+ @workers.each { |w| w.stop(timeout: timeout) }
16
+ end
17
+
18
+ def join
19
+ @workers.each(&:join)
20
+ end
21
+
22
+ def run
23
+ %w[INT TERM].each do |signal|
24
+ Signal.trap(signal) { stop }
25
+ end
26
+ start
27
+ join
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string'
4
+ require 'realm/error'
5
+
6
+ module Realm
7
+ class Persistence
8
+ class InvalidPersistanceType < Realm::Error; end
9
+ class Conflict < Realm::Error; end
10
+
11
+ class RelationIsReadOnly < Realm::Error
12
+ def initialize(relation, msg: "Cannot write using read-only relation #{relation.class}")
13
+ super(msg)
14
+ end
15
+ end
16
+
17
+ class RepositoryIsReadOnly < Realm::Error
18
+ def initialize(repo, msg: "Cannot write using read-only repository #{repo.class}")
19
+ super(msg)
20
+ end
21
+ end
22
+
23
+ def self.setup(...)
24
+ new(...).setup
25
+ end
26
+
27
+ def initialize(container, repositories)
28
+ @container = container
29
+ @repositories = repositories
30
+ end
31
+
32
+ def setup
33
+ register_repos
34
+ end
35
+
36
+ private
37
+
38
+ def gateway
39
+ @gateway ||= @container.resolve('persistence.gateway')
40
+ end
41
+
42
+ def register_repos
43
+ @repositories.each do |repo_class|
44
+ @container.register_factory(repo_class, gateway, as: "#{repo_class.name.demodulize.underscore}_repo")
45
+ end
46
+ end
47
+
48
+ def constantize(*parts)
49
+ return parts[0] unless parts[0].is_a?(String)
50
+
51
+ parts.join('::').safe_constantize
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'realm/persistence'
4
+ require 'realm/error'
5
+
6
+ module Realm
7
+ class Persistence
8
+ class QueryCannotModifyState < Realm::Error; end
9
+
10
+ class RepositoryQueryHandlerAdapter
11
+ def initialize(repo)
12
+ @repo = repo.respond_to?(:readonly) ? repo.readonly : repo
13
+ end
14
+
15
+ def call(action:, params: {}, **)
16
+ raise CannotHandleAction.new(self, action) unless @repo.respond_to?(action)
17
+
18
+ @repo.send(action, **params)
19
+ rescue RepositoryIsReadOnly
20
+ raise QueryCannotModifyState
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/class'
4
+ require 'active_support/core_ext/string'
5
+
6
+ module Realm
7
+ class Plugin
8
+ class << self
9
+ def plugin_name(value = :not_provided)
10
+ @plugin_name = value.to_sym unless value == :not_provided
11
+ @plugin_name = name.split('::')[-2].underscore.to_sym unless defined?(@plugin_name)
12
+ @plugin_name
13
+ end
14
+
15
+ def setup(_config, _container)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'realm/action_handler'
4
+
5
+ module Realm
6
+ class QueryHandler < Realm::ActionHandler
7
+ end
8
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/delegation'
4
+ require 'active_support/core_ext/object/try'
5
+ require 'realm/context'
6
+ require 'realm/container'
7
+ require 'realm/dispatcher'
8
+ require 'realm/event_router'
9
+ require 'realm/multi_worker'
10
+ require 'realm/health_status'
11
+ require 'realm/runtime/session'
12
+
13
+ module Realm
14
+ class Runtime
15
+ delegate :query, :run, :run_as_job, :wait_for_jobs, to: :dispatcher
16
+ delegate :trigger, :add_listener, to: :event_router
17
+ delegate :[], to: :context
18
+ attr_reader :container
19
+
20
+ def initialize(container = Container.new)
21
+ @container = Container[container]
22
+ end
23
+
24
+ def context
25
+ @context ||= Context.new(@container)
26
+ end
27
+
28
+ def session(context = {})
29
+ context.blank? ? self : Session.new(self, context)
30
+ end
31
+
32
+ def worker(*args)
33
+ MultiWorker.new(event_router.try(:workers, *args) || [])
34
+ end
35
+
36
+ def health
37
+ component_statuses = container.each_with_object({}) do |(name, component), map|
38
+ map[name] = component.health if component.respond_to?(:health) && !component.is_a?(Runtime)
39
+ end
40
+ HealthStatus.combine(component_statuses)
41
+ end
42
+
43
+ # Get all active messaging queues. For maintenance purpose only.
44
+ # TODO: Introduce component container and allow to call those method directly on components instead of
45
+ # polluting runtime
46
+ # Example: engine.realm.components.find(type: Realm::EventRouter::SNSGateway).try(:active_queues)
47
+ def active_queues
48
+ event_router.try(:active_queues) || []
49
+ end
50
+
51
+ private
52
+
53
+ def dispatcher
54
+ @dispatcher ||= container.create(Dispatcher, self)
55
+ end
56
+
57
+ def event_router
58
+ @container[EventRouter]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'realm/dispatcher'
4
+
5
+ module Realm
6
+ class Runtime
7
+ class Session
8
+ delegate :query, :run, :run_as_job, :wait_for_jobs, to: :dispatcher
9
+ delegate :add_listener, :trigger, :worker, to: :@runtime
10
+ delegate :[], to: :context
11
+ attr_reader :context
12
+
13
+ def initialize(runtime, context)
14
+ @runtime = runtime
15
+ @context = runtime.context.merge(context)
16
+ end
17
+
18
+ def session(context = {})
19
+ context.blank? ? self : self.class.new(self, context)
20
+ end
21
+
22
+ def container
23
+ @runtime.container
24
+ end
25
+
26
+ private
27
+
28
+ def dispatcher
29
+ @dispatcher ||= container.create(Dispatcher, self)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-types'
4
+
5
+ module Realm
6
+ module Types
7
+ include Dry.Types()
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: realm-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - developers@reevoo.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-container
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-initializer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dry-struct
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dry-types
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dry-validation
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.5'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.5'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description:
154
+ email:
155
+ executables: []
156
+ extensions: []
157
+ extra_rdoc_files: []
158
+ files:
159
+ - lib/realm-core.rb
160
+ - lib/realm.rb
161
+ - lib/realm/action_handler.rb
162
+ - lib/realm/action_handler/result.rb
163
+ - lib/realm/builder.rb
164
+ - lib/realm/command_handler.rb
165
+ - lib/realm/config.rb
166
+ - lib/realm/container.rb
167
+ - lib/realm/context.rb
168
+ - lib/realm/dependency.rb
169
+ - lib/realm/dispatcher.rb
170
+ - lib/realm/domain_resolver.rb
171
+ - lib/realm/error.rb
172
+ - lib/realm/event.rb
173
+ - lib/realm/event_factory.rb
174
+ - lib/realm/event_handler.rb
175
+ - lib/realm/event_router.rb
176
+ - lib/realm/event_router/gateway.rb
177
+ - lib/realm/event_router/internal_loop_gateway.rb
178
+ - lib/realm/health_status.rb
179
+ - lib/realm/mixins/aggregate_member.rb
180
+ - lib/realm/mixins/context_injection.rb
181
+ - lib/realm/mixins/controller.rb
182
+ - lib/realm/mixins/decorator.rb
183
+ - lib/realm/mixins/dependency_injection.rb
184
+ - lib/realm/mixins/reactive.rb
185
+ - lib/realm/mixins/repository_helper.rb
186
+ - lib/realm/multi_worker.rb
187
+ - lib/realm/persistence.rb
188
+ - lib/realm/persistence/repository_query_handler_adapter.rb
189
+ - lib/realm/plugin.rb
190
+ - lib/realm/query_handler.rb
191
+ - lib/realm/runtime.rb
192
+ - lib/realm/runtime/session.rb
193
+ - lib/realm/types.rb
194
+ homepage:
195
+ licenses:
196
+ - MIT
197
+ metadata: {}
198
+ post_install_message:
199
+ rdoc_options: []
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: 2.7.0
207
+ required_rubygems_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ requirements: []
213
+ rubygems_version: 3.1.6
214
+ signing_key:
215
+ specification_version: 4
216
+ summary: Domain layer framework following Domain-driven/CQRS design principles
217
+ test_files: []