carioca 1.4 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +18 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/Gemfile +6 -1
  6. data/Gemfile.lock +49 -0
  7. data/README.md +12 -220
  8. data/Rakefile +5 -66
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/carioca.gemspec +32 -23
  12. data/config/locales/en.yml +15 -0
  13. data/config/locales/fr.yml +19 -0
  14. data/lib/carioca/configuration.rb +47 -0
  15. data/lib/carioca/constants.rb +45 -0
  16. data/lib/carioca/container.rb +16 -0
  17. data/lib/carioca/dependencies.rb +19 -0
  18. data/lib/carioca/helpers.rb +44 -31
  19. data/lib/carioca/mixin.rb +32 -0
  20. data/lib/carioca/{tasks/rake.rb → rake/manage.rb} +2 -4
  21. data/lib/carioca/rake/tasks/registry.tasks +57 -0
  22. data/lib/carioca/registry.rb +93 -0
  23. data/lib/carioca/registry_file.rb +62 -0
  24. data/lib/carioca/services/config.rb +140 -0
  25. data/lib/carioca/services/i18n.rb +20 -0
  26. data/lib/carioca/services/init.rb +2 -0
  27. data/lib/carioca/validator.rb +49 -0
  28. data/lib/carioca.rb +2 -319
  29. data/samples/Rakefile +2 -0
  30. data/samples/config/carioca.registry +22 -0
  31. data/samples/config/locales/en.yml +2 -0
  32. data/samples/config/locales/es.yml +2 -0
  33. data/samples/config/locales/fr.yml +2 -0
  34. data/samples/config/settings.yml +24 -0
  35. data/samples/test.rb +71 -0
  36. metadata +46 -187
  37. data/AUTHORS +0 -8
  38. data/COPYRIGHT +0 -24
  39. data/ChangeLog +0 -10
  40. data/INSTALL +0 -7
  41. data/doc/manual.rdoc +0 -225
  42. data/lib/carioca/exceptions.rb +0 -9
  43. data/lib/carioca/private.rb +0 -168
  44. data/lib/carioca/services/configuration.rb +0 -203
  45. data/lib/carioca/services/debug.rb +0 -71
  46. data/lib/carioca/services/logger.rb +0 -58
  47. data/lib/carioca/services.rb +0 -143
  48. data/lib/carioca/tasks/registry_init.rake +0 -12
  49. data/spec/carioca_spec.rb +0 -468
  50. data/spec/config/.config +0 -14
  51. data/spec/config/services.registry +0 -55
  52. data/spec/init_spec.rb +0 -2
  53. data/spec/samples/dummy.rb +0 -11
  54. data/spec/samples/otherdummy.rb +0 -11
  55. data/spec/samples/requireddummy.rb +0 -11
  56. data/spec/spec_helper.rb +0 -18
  57. data/ultragreen_roodi_coding_convention.yml +0 -25
data/lib/carioca.rb CHANGED
@@ -1,324 +1,7 @@
1
+ # frozen_string_literal: false
1
2
 
2
- #!/usr/bin/env ruby
3
- # coding: utf-8
4
- #---
5
- # Author : Romain GEORGES
6
- # type : gem component library
7
- # obj : Carioca Module
8
- #---
3
+ require_relative "carioca/dependencies"
9
4
 
10
- require 'yaml'
11
- require 'rubygems'
12
- require 'methodic'
13
- require 'carioca/services'
14
- require 'carioca/private'
15
-
16
-
17
-
18
- # module Carioca
19
- # @author Romain GEORGES <romain@ultragreen.net>
20
- # @see http://www.ultragreen.net/projects/carioca
21
- # @version 1.0
22
- # @note this module is a namespace Carioca
23
5
  module Carioca
24
6
 
25
-
26
-
27
- # module Services
28
- # @note this module is a namespace Carioca::Services
29
- module Services
30
-
31
-
32
- # class Registry
33
- # This class provide the Registry manager for Carioca
34
- # @note this class is a Singleton Class to instanciate do not use new (initializer), but :
35
- # Carioca::Services::Registry.init options
36
- # @example Complete usage
37
- # require 'rubygems'
38
- # require 'carioca'
39
- # registry = Carioca::Services::Registry.init
40
- # registry = Carioca::Services::Registry.init :file => 'myservices.registry'
41
- # registry = Carioca::Services::Registry.init :file => 'myservices.registry', :debug => true
42
- class Registry
43
-
44
- include PrivateMethodsCariocaServicesRegistry
45
-
46
- # for singleton
47
- private_class_method :new
48
- private :verify_requires_dependancies
49
- private :require_service
50
- private :instanciate_service
51
- private :kill_service
52
- private :scan_instance_suffix
53
- private :kill_distributed_service
54
- private :shutdown_ring_if_empty
55
-
56
-
57
- @@inst = nil
58
-
59
- # Singleton constructor for Registry
60
- # @param [Hash] _options the options, keys are symbols
61
- # @option _options [String] :file The path of your registry YAML definition (see YAML registry format)
62
- # @option _options [TrueClass,FalseClass] :debug Boolean activation/deactiviation of the carioca debug mode (log traces)
63
- # @return [Carioca::Services::Registry] Singleton class instance of Registry
64
- # @example usage
65
- # registry = Carioca::Services::Registry.init # or
66
- # registry = Carioca::Services::Registry.init :file => 'myservices.registry' # or
67
- # registry = Carioca::Services::Registry.init :file => 'myservices.registry', :debug => true
68
- def Registry.init(_options = {})
69
- options = Methodic::get_options(_options)
70
- options.specify_defaults_values :file => './services.registry', :debug => false, :name => 'Carioca'
71
- options.merge
72
- @@inst = new(options) if @@inst.nil?
73
- return @@inst
74
- end
75
-
76
- # @example read
77
- # registry = Carioca::Services::Registry.init
78
- # p registry.registry_filename
79
- # @example write
80
- # registry = Carioca::Services::Registry.init
81
- # p registry.registry_filename = '/tmp/test.file'
82
- # @attr_reader [Hash] list a hash table of all structured registred services definition
83
- # (come from file and explicitly registered services)
84
- attr_accessor :registry_filename
85
-
86
- # @example
87
- # registry = Carioca::Services::Registry.init
88
- # p registry.list
89
- # @attr_reader [Hash] list a hash table of all structured registred services definition
90
- # (come from file and explicitly registered services)
91
- attr_reader :list
92
-
93
- # @example
94
- # registry = Carioca::Services::Registry.init
95
- # p registry.loaded_services # you should see the logger service Hash definition
96
- # @attr_reader [Hash] loaded_services a hash table of all structured loaded services
97
- attr_reader :loaded_services
98
-
99
- # @example
100
- # registry = Carioca::Services::Registry.init
101
- # p registry.debug
102
- # @attr_reader [TrueClass,FalseClass] debug a boolean of the current debug status
103
- attr_reader :debug
104
-
105
- # @example
106
- # registry = Carioca::Services::Registry.init
107
- # p registry.name
108
- # registry.name = 'Agent'
109
- # @attr_reader [String] the name of the Registry, used for distributed services
110
- # @note default value is 'Carioca'
111
- attr_accessor :name
112
-
113
- # writer accessor for debug (interaction with log service)
114
- # @example
115
- # registry = Carioca::Services::Registry.init
116
- # p registry.debug = true
117
- # @param [TrueClass,FalseClass] _value true or false to activate/deactivate debug mode
118
- # @note interaction with preloaded service logger
119
- def debug=(_value)
120
- @log.level =(_value)? Logger::DEBUG : Logger::INFO
121
- end
122
-
123
-
124
- # stop a service, if loaded and different to logger
125
- # @param [Hash] _options the options, keys are symbols
126
- # @option _options [String] :name The name of the service to stop
127
- # @return [TruaClass,FalseClass] true if service effectivly stopped, false if not, or :name == 'logger'
128
- # @example usage
129
- # registry = Carioca::Services::Registry.init
130
- # registry.start_service :name => 'configuration'
131
- # registry.stop_service :name => 'configuration'
132
- # #=> return true
133
- # registry.stop_service :name => 'configuration'
134
- # #=> return false
135
- # registry.stop_service :name => 'logger'
136
- # #=> return false
137
- # @note log if debug mode
138
- def stop_service(_options)
139
- options = Methodic.get_options(_options)
140
- options.specify_class_of :name => String
141
- options.specify_presence_of([:name])
142
- options.validate!
143
- @log.debug('Carioca') { "Service logger can't be unloaded" } if @log and options[:name] == 'logger'
144
- return false if options[:name] == 'logger'
145
- if @loaded_services.include?(options[:name]) then
146
- options = scan_instance_suffix(options)
147
- return kill_distributed_service options if @list[options[:shortname]][:distributed]
148
- return kill_service options
149
- else
150
- @log.debug('Carioca') { "Service #{options[:name]} not loaded" } if @log
151
- return false
152
- end
153
- end
154
-
155
-
156
- # register a new service in registry added to @list
157
- # @param [Hash] _options the options hash, key are :symbols
158
- # @option _options [String] :name the name of the service (required)
159
- # @option _options [String] :resource the resource, must be a gem name, a fullpath filename, a builtin service (required)
160
- # @option _options [Symbol] :type the resource type of the service, must be :gem, :builtin or :file (required)
161
- # @option _options [String] :service the realname of the service class with namespace (eg. ExternalServices::Dummy )
162
- # @option _options [String] :description the description of the service (required)
163
- # @option _options [Hash] :init_options the params of the service, keys are symbols
164
- # @option _options [Array] :requires the list of [String] services name required to load this service
165
- # @option _options [TruClass|FalseClass] :distributed if service must be load as a distributed service
166
- # @return [TrueClass,FalseClass] true if service is added
167
- # @raise ArgumentError when :type is not in [:gem,:file,:builtin]
168
- def register_service(_options)
169
- options = Methodic.get_options(_options)
170
- options.specify_classes_of({:name => String, :resource => String, :description => String, :type => Symbol, :service => String })
171
- options.specify_presences_of([:name,:type,:resource,:service])
172
- cond = Proc::new{|option| if [:gem,:gem_file,:file,:builtin].include? option then true else false end }
173
- options.specify_condition_for :type => cond
174
- options.validate!
175
- _name = _options.delete(:name)
176
- @list[_name] = _options
177
- return true
178
- end
179
-
180
- # unregister a service in registry removed from @list
181
- # @param [Hash] _options the options hash, key are :symbols
182
- # @option _options [String] :name the name of the service (required)
183
- # @raise [RegistryError] if try to unregister logger
184
- # @raise [registryerror] if try to unregister a loaded service
185
- # @return [TrueClass,FalseClass]
186
- def unregister_service(_options = {})
187
- options = Methodic.get_options(_options)
188
- options.specify_class_of :name => String
189
- options.specify_presence_of :name
190
- options.validate!
191
- raise RegistryError::new("FONDATION : Can't unregistered the logger service" ) if options[:name] == 'logger'
192
- raise RegistryError::new("Can't unregistered a loaded service" ) if @loaded_services.include?(options[:name])
193
- @list.delete(options[:name])
194
- return true
195
- end
196
-
197
- # overload @list (self.list) by adding/reloading the builtins services definition scanned from Carioca gem
198
- # alterate @list
199
- # @example usage
200
- # registry = Carioca::Services::Registry.init :name => '/tmp/empty.file'
201
- # registry.discover_builtins
202
- # registry.save!
203
- def discover_builtins
204
- @list.merge! Carioca::Services::discover_builtins
205
- end
206
-
207
- # save the registry file in self.registry_filename
208
- # @return [TruaClass,FalseClass] true if the file is saved
209
- # @example usage
210
- # registry = Carioca::Services::Registry.init :file => './empty.file'
211
- # registry.discover_builtins
212
- # registry.unregister_service :name => 'configuration'
213
- # registry.save!
214
- def save!
215
- res = false
216
- File.open(@registry_filename, "w") do |f|
217
- res = true if f.write(@list.to_yaml)
218
- end
219
- return res
220
- end
221
-
222
- # start or get e previously started service in @list
223
- # @return [Object] the loaded service class instance
224
- # @param [Hash] _options the params, key are symbols
225
- # @option _options [String] :name the name of the service
226
- # @option _options [Hash] :params the params of the service
227
- # @raise [RegistryError] Config Failed, for unconsistant service definition in @list
228
- # @example usage
229
- # registry = Carioca::Services::Registry.init
230
- # config = registry.start_service :name => 'configuration'
231
- # proxy = subject.start_service :name => 'debug' , :params => {:service => 'configuration'}
232
- def start_service(_options)
233
- options = Methodic.get_options(_options)
234
- options.specify_classes_of :name => String
235
- options.specify_presences_of([:name])
236
- options.validate!
237
- @log.debug('Carioca') { "getting service #{options[:name]}"} if @log
238
- self.restart_service(options) unless @loaded_services.include? options[:name]
239
- return @loaded_services[options[:name]]
240
- end
241
- alias :get_service :start_service
242
-
243
- # start or restart (stop=>start) previously started service in @list
244
- # @return [Object] the loaded service class instance
245
- # @param [Hash] _options the params, key are symbols
246
- # @option _options [String] :name the name of the service
247
- # @option _options [Hash] :params the params of the service
248
- # @raise [RegistryError] Config Failed, for unconsistant service definition in @list
249
- # @example usage
250
- # registry = Carioca::Services::Registry.init
251
- # config = registry.restart_service :name => 'configuration'
252
- # config = registry.restart_service :name => 'configuration' # stop and restart the previous services
253
- # proxy = subject.restart_service :name => 'debug' , :params => {:service => 'configuration'}
254
- # @note Registry and services are Singleton
255
- def restart_service(_options)
256
- options = Methodic.get_options(_options)
257
- options.specify_classes_of :name => String
258
- options.specify_presences_of [:name]
259
- options.validate!
260
- options = scan_instance_suffix(options)
261
- dist = (@list[options[:shortname]][:distributed].nil?)? false : @list[options[:shortname]][:distributed]
262
- if dist and @loaded_services.include? options[:name] then
263
- @log.debug('Carioca') { "Restarting distributed service #{options[:name]}"} if @log
264
- kill_distributed_service options
265
- elsif @loaded_services.include? options[:name] then
266
- @log.debug('Carioca') { "Restarting service #{options[:name]}"} if @log
267
- kill_service options
268
- end
269
- verify_requires_dependancies(options)
270
- require_service(options)
271
- return instanciate_service(options)
272
- end
273
-
274
- # close the registry (finalizer)
275
- # * stop all the services
276
- # * kill logger
277
- # * call private kill_service for each
278
- # @note the garbage method hook is call if defined, for each service
279
- # @return [TrueClass,FalseClass] true if registry closed successfully
280
- def close
281
- @log.debug('Carioca') { "closing Registry ..." }
282
- options = Hash::new
283
- @loaded_services.keys.each do |service|
284
- options[:name] = service
285
- options = scan_instance_suffix(options)
286
- next if options[:name] == 'logger'
287
- kill_distributed_service :name => options[:name], :preserve => true if @list[options[:shortname]][:distributed]
288
- kill_service options unless @list[options[:shortname]][:distributed]
289
- end
290
- @log.debug('Carioca') { "Registry services closed, logger will be closed asynchronously" }
291
- kill_service :name => 'logger'
292
- return true
293
- end
294
-
295
- # load the registry file from self.registry_filename
296
- # @return [Hash] @list the list Structure of the loaded registry
297
- # @example usage
298
- # registry = Carioca::Services::Registry.init
299
- # registry.registry_filename = "./an_other.file"
300
- # registry.load #or
301
- # registry.reload
302
- def load
303
- @list = YAML.load_file(@registry_filename)
304
- end
305
- alias :reload :load
306
-
307
- end # end of Carioca::Services::Registry
308
- end # end of Carioca:Services
309
- end # end of Carioca
310
-
311
- # Exception overload class for Carioca
312
- class RegistryError < Exception; end
313
-
314
-
315
- Module.class_eval do
316
- def init_registry _options={}
317
- options = Methodic.get_options(_options)
318
- options.specify_classes_of :with_file => String
319
- options.specify_default_value_of :with_file => './services.registry'
320
- options.merge
321
- options.validate!
322
- Carioca::Services::Registry.init options[:with_file]
323
- end
324
7
  end
data/samples/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'carioca/rake/manage'
@@ -0,0 +1,22 @@
1
+ ---
2
+ :toto:
3
+ :type: :gem
4
+ :description: The toto service
5
+ :service: Toto
6
+ :resource: toto
7
+ :depends:
8
+ - :configuration
9
+ :sqdqd:
10
+ :type: :internal
11
+ :description: The sqdqd service
12
+ :service: SQP::new('titi')
13
+ :depends:
14
+ - :configuration
15
+ - :logger
16
+ - :toto
17
+ :uuid:
18
+ :type: :gem
19
+ :description: The uuid service
20
+ :service: UUID
21
+ :resource: uuid
22
+
@@ -0,0 +1,2 @@
1
+ en:
2
+ test: 'english'
@@ -0,0 +1,2 @@
1
+ es:
2
+ test: 'español'
@@ -0,0 +1,2 @@
1
+ fr:
2
+ test: 'français'
@@ -0,0 +1,24 @@
1
+ ---
2
+ :monappli:
3
+ :production: {}
4
+ :staging:
5
+ :onstaging: staging
6
+ :test: {}
7
+ :development:
8
+ :ondev: test
9
+ :treeA:
10
+ :trunk1:
11
+ :branch1: leaf1
12
+ :trunk2:
13
+ :branch3: leaf3
14
+ :branch4: leaf4
15
+ :branch5: [ "toto","titi","tata" ]
16
+ :default:
17
+ :toto: 'titi'
18
+ :treeA:
19
+ :trunk1:
20
+ :branch2: leaf2
21
+ :trunk2:
22
+ :branch5: ["tutu"]
23
+
24
+
data/samples/test.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'carioca'
3
+
4
+
5
+ Carioca::Registry.configure do |spec|
6
+ spec.filename = './config/carioca.registry'
7
+ spec.debug = true
8
+ spec.init_from_file = true
9
+ # spec.log_file = '/tmp/test.rge'
10
+ spec.config_file = './config/settings.yml'
11
+ spec.config_root = :monappli
12
+ spec.environment = :development
13
+ spec.default_locale = :fr
14
+ spec.locales_load_path << Dir[File.expand_path('./config/locales') + "/*.yml"]
15
+ end
16
+
17
+
18
+ class MyService
19
+ extend Carioca::Injector
20
+ inject service: :logger
21
+
22
+ def initialize
23
+ logger.warn(self.class.to_s) {'Init service'}
24
+ end
25
+
26
+ def hello
27
+ logger.info(self.class.to_s) {'Hello World'}
28
+ end
29
+ end
30
+
31
+
32
+ spec = {
33
+ service: 'MyService::new',
34
+ type: :internal,
35
+ }
36
+
37
+ Carioca::Registry.init.add service: :myservice, definition: spec
38
+
39
+ logger = Carioca::Registry.get.get_service name: :logger
40
+
41
+ logger.info(self.to_s) { "avaible services : #{Carioca::Registry.get.services.keys} "}
42
+ i18n = Carioca::Registry.get.get_service name: :i18n
43
+ i18n.locale = :es
44
+ p i18n.t(:test)
45
+
46
+ config = Carioca::Registry.get.get_service name: :configuration
47
+ config.settings.newkey = "value"
48
+
49
+ logger.info(self.to_s) { config.settings }
50
+
51
+ class MonAppli < Carioca::Container
52
+ def test
53
+ myservice.hello
54
+ logger.warn(self.class.to_s) {uuid.generate}
55
+ end
56
+
57
+ inject service: :uuid
58
+ inject service: :myservice
59
+ logger.info(self.to_s) { uuid.generate }
60
+
61
+ end
62
+
63
+
64
+
65
+
66
+ appli = MonAppli::new
67
+ appli.test
68
+
69
+
70
+
71
+