Class: Carioca::Services::Registry
- Inherits:
-
Object
- Object
- Carioca::Services::Registry
- Includes:
- PrivateMethodsCariocaServicesRegistry
- Defined in:
- lib/carioca.rb
Overview
this class is a Singleton Class to instanciate do not use new (initializer), but : Carioca::Services::Registry.init options
class Registry
This class provide the Registry manager for Carioca
Constant Summary collapse
- @@inst =
nil
Instance Attribute Summary collapse
- #debug ⇒ Object
-
#list ⇒ Object
readonly
(come from file and explicitly registered services).
- #loaded_services ⇒ Object readonly
- #name ⇒ Object
-
#registry_filename ⇒ Object
(come from file and explicitly registered services).
Class Method Summary collapse
-
.init(_options = {}) ⇒ Carioca::Services::Registry
Singleton constructor for Registry.
Instance Method Summary collapse
-
#close ⇒ TrueClass, FalseClass
close the registry (finalizer) * stop all the services * kill logger * call private kill_service for each.
-
#discover_builtins ⇒ Object
overload @list (self.list) by adding/reloading the builtins services definition scanned from Carioca gem alterate @list.
-
#load ⇒ Hash
(also: #reload)
load the registry file from self.registry_filename.
-
#register_service(_options) ⇒ TrueClass, FalseClass
register a new service in registry added to @list.
-
#restart_service(_options) ⇒ Object
start or restart (stop=>start) previously started service in @list.
-
#save! ⇒ TruaClass, FalseClass
save the registry file in self.registry_filename.
-
#start_service(_options) ⇒ Object
(also: #get_service)
start or get e previously started service in @list.
-
#stop_service(_options) ⇒ TruaClass, FalseClass
stop a service, if loaded and different to logger.
-
#unregister_service(_options = {}) ⇒ TrueClass, FalseClass
unregister a service in registry removed from @list.
Methods included from PrivateMethodsCariocaServicesRegistry
#get_ring, #initialize, #instanciate_service, #kill_distributed_service, #kill_service, #require_service, #scan_instance_suffix, #shutdown_ring_if_empty, #verify_requires_dependancies
Instance Attribute Details
#debug ⇒ Object
103 104 105 |
# File 'lib/carioca.rb', line 103 def debug @debug end |
#list ⇒ Object (readonly)
(come from file and explicitly registered services)
91 92 93 |
# File 'lib/carioca.rb', line 91 def list @list end |
#loaded_services ⇒ Object (readonly)
97 98 99 |
# File 'lib/carioca.rb', line 97 def loaded_services @loaded_services end |
#name ⇒ Object
default value is ‘Carioca’
111 112 113 |
# File 'lib/carioca.rb', line 111 def name @name end |
#registry_filename ⇒ Object
(come from file and explicitly registered services)
84 85 86 |
# File 'lib/carioca.rb', line 84 def registry_filename @registry_filename end |
Class Method Details
.init(_options = {}) ⇒ Carioca::Services::Registry
Singleton constructor for Registry
68 69 70 71 72 73 74 |
# File 'lib/carioca.rb', line 68 def Registry.init( = {}) = Methodic::() .specify_defaults_values :file => './services.registry', :debug => false, :name => 'Carioca' .merge @@inst = new() if @@inst.nil? return @@inst end |
Instance Method Details
#close ⇒ TrueClass, FalseClass
the garbage method hook is call if defined, for each service
close the registry (finalizer)
* stop all the services
* kill logger
* call private kill_service for each
280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/carioca.rb', line 280 def close @log.debug('Carioca') { "closing Registry ..." } = Hash::new @loaded_services.keys.each do |service| [:name] = service = scan_instance_suffix() next if [:name] == 'logger' kill_distributed_service :name => [:name], :preserve => true if @list[[:shortname]][:distributed] kill_service unless @list[[:shortname]][:distributed] end @log.debug('Carioca') { "Registry services closed, logger will be closed asynchronously" } kill_service :name => 'logger' return true end |
#discover_builtins ⇒ Object
overload @list (self.list) by adding/reloading the builtins services definition scanned from Carioca gem
alterate @list
203 204 205 |
# File 'lib/carioca.rb', line 203 def discover_builtins @list.merge! Carioca::Services::discover_builtins end |
#load ⇒ Hash Also known as: reload
load the registry file from self.registry_filename
302 303 304 |
# File 'lib/carioca.rb', line 302 def load @list = YAML.load_file(@registry_filename) end |
#register_service(_options) ⇒ TrueClass, FalseClass
register a new service in registry added to @list
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/carioca.rb', line 168 def register_service() = Methodic.() .specify_classes_of({:name => String, :resource => String, :description => String, :type => Symbol, :service => String }) .specify_presences_of([:name,:type,:resource,:service]) cond = Proc::new{|option| if [:gem,:gem_file,:file,:builtin].include? option then true else false end } .specify_condition_for :type => cond .validate! _name = .delete(:name) @list[_name] = return true end |
#restart_service(_options) ⇒ Object
Registry and services are Singleton
start or restart (stop=>start) previously started service in @list
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/carioca.rb', line 255 def restart_service() = Methodic.() .specify_classes_of :name => String .specify_presences_of [:name] .validate! = scan_instance_suffix() dist = (@list[[:shortname]][:distributed].nil?)? false : @list[[:shortname]][:distributed] if dist and @loaded_services.include? [:name] then @log.debug('Carioca') { "Restarting distributed service #{[:name]}"} if @log kill_distributed_service elsif @loaded_services.include? [:name] then @log.debug('Carioca') { "Restarting service #{[:name]}"} if @log kill_service end verify_requires_dependancies() require_service() return instanciate_service() end |
#save! ⇒ TruaClass, FalseClass
save the registry file in self.registry_filename
214 215 216 217 218 219 220 |
# File 'lib/carioca.rb', line 214 def save! res = false File.open(@registry_filename, "w") do |f| res = true if f.write(@list.to_yaml) end return res end |
#start_service(_options) ⇒ Object Also known as: get_service
start or get e previously started service in @list
232 233 234 235 236 237 238 239 240 |
# File 'lib/carioca.rb', line 232 def start_service() = Methodic.() .specify_classes_of :name => String .specify_presences_of([:name]) .validate! @log.debug('Carioca') { "getting service #{[:name]}"} if @log self.restart_service() unless @loaded_services.include? [:name] return @loaded_services[[:name]] end |
#stop_service(_options) ⇒ TruaClass, FalseClass
log if debug mode
stop a service, if loaded and different to logger
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/carioca.rb', line 138 def stop_service() = Methodic.() .specify_class_of :name => String .specify_presence_of([:name]) .validate! @log.debug('Carioca') { "Service logger can't be unloaded" } if @log and [:name] == 'logger' return false if [:name] == 'logger' if @loaded_services.include?([:name]) then = scan_instance_suffix() return kill_distributed_service if @list[[:shortname]][:distributed] return kill_service else @log.debug('Carioca') { "Service #{[:name]} not loaded" } if @log return false end end |
#unregister_service(_options = {}) ⇒ TrueClass, FalseClass
unregister a service in registry removed from @list
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/carioca.rb', line 186 def unregister_service( = {}) = Methodic.() .specify_class_of :name => String .specify_presence_of :name .validate! raise RegistryError::new("FONDATION : Can't unregistered the logger service" ) if [:name] == 'logger' raise RegistryError::new("Can't unregistered a loaded service" ) if @loaded_services.include?([:name]) @list.delete([:name]) return true end |