Class: Citrus::Application
- Inherits:
-
Object
- Object
- Citrus::Application
- Includes:
- Utils::AppUtil, Utils::EventEmitter
- Defined in:
- lib/citrus/application.rb
Overview
Application
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#cur_server ⇒ Object
readonly
Returns the value of attribute cur_server.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#master ⇒ Object
readonly
Returns the value of attribute master.
-
#modules_registered ⇒ Object
readonly
Returns the value of attribute modules_registered.
-
#server_id ⇒ Object
readonly
Returns the value of attribute server_id.
-
#server_type ⇒ Object
readonly
Returns the value of attribute server_type.
-
#servers_map ⇒ Object
readonly
Returns the value of attribute servers_map.
-
#start_id ⇒ Object
readonly
Returns the value of attribute start_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#add_servers(sinfos) ⇒ Object
Add new servers at runtime.
-
#after_start(&block) ⇒ Object
Lifecycle callback for after start.
-
#backend?(server = nil) ⇒ Boolean
Check whether a server is a backend.
-
#configure(*args, &block) ⇒ Object
Configure callback for the specified env and server type.
-
#frontend?(server = nil) ⇒ Boolean
Check whether a server is a frontend.
-
#get(setting) ⇒ Object
Get property from setting.
-
#get_servers_by_type(type) ⇒ Object
Get server infos by server type.
-
#initialize(args = {}) ⇒ Application
constructor
Initialize the application.
-
#master? ⇒ Boolean
Check whether a server is a master.
-
#remove_server(server_id) ⇒ Object
Remove server at runtime.
-
#remove_servers(server_ids) ⇒ Object
Remove servers at runtime.
-
#replace_server(slist, sinfo) ⇒ Object
Replace server at runtime.
-
#replace_servers(sinfos) ⇒ Object
Replace servers at runtime.
-
#set(setting, value) ⇒ Object
Assign ‘setting` to `value`.
-
#start(&block) ⇒ Object
Start the application.
-
#start_up(&block) ⇒ Object
Start up.
-
#stop(force = false) ⇒ Object
Stop components.
Methods included from Utils::EventEmitter
Methods included from Utils::AppUtil
#config_logger, #contains, #default_configuration, #load, #load_config_file, #load_default_components, #load_lifecycle, #load_master, #load_servers, #opt_component, #opt_components, #parse_args, #process_args, #register, #setup_env, #stop_components
Constructor Details
#initialize(args = {}) ⇒ Application
Initialize the application
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/citrus/application.rb', line 23 def initialize args={} @env = nil @type = nil @start_id = nil @base = args[:base] || Dir.pwd @settings = {} # current server info @cur_server = nil @server_id = nil @server_type = nil @start_time = nil # global server info @master = nil @servers = {} @servers_map = {} @server_types = [] @server_type_maps = {} # lifecycle callbacks @lifecycle_cbs = {} @components = {} @modules_registered = {} default_configuration @state = :state_inited # singleton pattern eval %Q{ class ::Citrus::Application private_class_method :new end } end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
15 16 17 |
# File 'lib/citrus/application.rb', line 15 def base @base end |
#components ⇒ Object (readonly)
Returns the value of attribute components.
18 19 20 |
# File 'lib/citrus/application.rb', line 18 def components @components end |
#cur_server ⇒ Object (readonly)
Returns the value of attribute cur_server.
16 17 18 |
# File 'lib/citrus/application.rb', line 16 def cur_server @cur_server end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
15 16 17 |
# File 'lib/citrus/application.rb', line 15 def env @env end |
#master ⇒ Object (readonly)
Returns the value of attribute master.
17 18 19 |
# File 'lib/citrus/application.rb', line 17 def master @master end |
#modules_registered ⇒ Object (readonly)
Returns the value of attribute modules_registered.
18 19 20 |
# File 'lib/citrus/application.rb', line 18 def modules_registered @modules_registered end |
#server_id ⇒ Object (readonly)
Returns the value of attribute server_id.
16 17 18 |
# File 'lib/citrus/application.rb', line 16 def server_id @server_id end |
#server_type ⇒ Object (readonly)
Returns the value of attribute server_type.
16 17 18 |
# File 'lib/citrus/application.rb', line 16 def server_type @server_type end |
#servers_map ⇒ Object (readonly)
Returns the value of attribute servers_map.
17 18 19 |
# File 'lib/citrus/application.rb', line 17 def servers_map @servers_map end |
#start_id ⇒ Object (readonly)
Returns the value of attribute start_id.
15 16 17 |
# File 'lib/citrus/application.rb', line 15 def start_id @start_id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/citrus/application.rb', line 15 def type @type end |
Instance Method Details
#add_servers(sinfos) ⇒ Object
Add new servers at runtime
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/citrus/application.rb', line 187 def add_servers sinfos return unless sinfos && !sinfos.empty? sinfos.each { |sinfo| # update global server map @servers[sinfo[:server_id]] = sinfo # update global server type map slist = @server_type_maps[sinfo[:server_type]] ||= [] replace_server slist, sinfo # update global server type list if !@server_types.member? sinfo[:server_type] @server_types << sinfo[:server_type] end } emit 'add_servers', sinfos end |
#after_start(&block) ⇒ Object
Lifecycle callback for after start
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/citrus/application.rb', line 106 def after_start &block if @state != :state_started block_given? and yield RuntimeError.new 'application is not running now' return end opt_components(@components.values, :after_start) { |err| if after_cb = @lifecycle_cbs[:after_start] after_cb.call self, &proc{ block_given? and yield err } else block_given? and yield err end } puts used_time = Time.now.to_f - @start_time end |
#backend?(server = nil) ⇒ Boolean
Check whether a server is a backend
175 176 177 |
# File 'lib/citrus/application.rb', line 175 def backend? server=nil not frontend? server end |
#configure(*args, &block) ⇒ Object
Configure callback for the specified env and server type
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/citrus/application.rb', line 147 def configure *args, &block args.length > 0 ? env = args[0].to_s : env = 'all' args.length > 1 ? server_type = args[1].to_s : server_type = 'all' if env == 'all' || contains(@env.to_s, env) if server_type == 'all' || contains(@server_type, server_type) instance_eval &block if block end end return self end |
#frontend?(server = nil) ⇒ Boolean
Check whether a server is a frontend
168 169 170 |
# File 'lib/citrus/application.rb', line 168 def frontend? server=nil server ? server[:frontend] == true : @cur_server[:frontend] == true end |
#get(setting) ⇒ Object
Get property from setting
142 143 144 |
# File 'lib/citrus/application.rb', line 142 def get setting @settings[setting] end |
#get_servers_by_type(type) ⇒ Object
Get server infos by server type
161 162 163 |
# File 'lib/citrus/application.rb', line 161 def get_servers_by_type type @server_type_maps[type] end |
#master? ⇒ Boolean
Check whether a server is a master
180 181 182 |
# File 'lib/citrus/application.rb', line 180 def master? @server_type == 'master' end |
#remove_server(server_id) ⇒ Object
Remove server at runtime
220 221 |
# File 'lib/citrus/application.rb', line 220 def remove_server server_id end |
#remove_servers(server_ids) ⇒ Object
Remove servers at runtime
208 209 |
# File 'lib/citrus/application.rb', line 208 def remove_servers server_ids end |
#replace_server(slist, sinfo) ⇒ Object
Replace server at runtime
227 228 229 230 231 232 233 234 235 |
# File 'lib/citrus/application.rb', line 227 def replace_server slist, sinfo slist.each_with_index { |s, index| if s[:server_id] == sinfo[:server_id] slist[index] = sinfo return end } slist << sinfo end |
#replace_servers(sinfos) ⇒ Object
Replace servers at runtime
214 215 |
# File 'lib/citrus/application.rb', line 214 def replace_servers sinfos end |
#set(setting, value) ⇒ Object
Assign ‘setting` to `value`
134 135 136 137 |
# File 'lib/citrus/application.rb', line 134 def set setting, value @settings[setting] = value return self end |
#start(&block) ⇒ Object
Start the application
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/citrus/application.rb', line 63 def start &block @start_time = Time.now.to_f if @state != :state_inited block_given? and yield Exception.new 'application double start' return end if @start_id if @start_id != 'master' Starter.run_servers self return end else if @type && @type != :all && @type != :master Starter.run_servers self return end end load_default_components if before_cb = @lifecycle_cbs[:before_startup] before_cb.call self, &proc{ start_up &block } else start_up &block end end |
#start_up(&block) ⇒ Object
Start up
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/citrus/application.rb', line 94 def start_up &block opt_components(@components.values, :start) { |err| @state = :state_started if err block_given? and yield err else after_start &block end } end |
#stop(force = false) ⇒ Object
Stop components
127 128 |
# File 'lib/citrus/application.rb', line 127 def stop force=false end |