Module: Citrus::Utils::AppUtil

Included in:
Application
Defined in:
lib/citrus/util/app_util.rb

Overview

AppUtil

Instance Method Summary collapse

Instance Method Details

#config_loggerObject

Configure logger



87
88
# File 'lib/citrus/util/app_util.rb', line 87

def config_logger
end

#contains(str, settings) ⇒ Object

Check whether a string is contained in the settings

Parameters:

  • str (String)
  • settings (String)


196
197
198
199
200
# File 'lib/citrus/util/app_util.rb', line 196

def contains str, settings
  return false unless settings.instance_of? String
  return false if settings.empty?
  settings.split('|').inject(false) { |r, t| true if str == t }
end

#default_configurationObject

Initialize application configuration



17
18
19
20
21
22
23
24
25
# File 'lib/citrus/util/app_util.rb', line 17

def default_configuration
  args = parse_args
  setup_env args
  load_master
  load_servers
  process_args args
  config_logger
  load_lifecycle
end

#load(component) ⇒ Object

Load component

Parameters:

  • component (Class)


135
136
137
138
139
140
# File 'lib/citrus/util/app_util.rb', line 135

def load component
  name = component.name
  instance_eval %Q{
    @components[name] = #{component}.new self, @settings[:#{name}_config] || {}
  }
end

#load_config_file(filename) ⇒ Object

Load config file

Parameters:

  • filename (String)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/citrus/util/app_util.rb', line 97

def load_config_file filename
  origin_path = File.join @base, filename
  present_path = File.join @base, Constants::Filepath::CONFIG_DIR, @env.to_s, File.basename(filename)
  unless File.exists?(origin_path) && file_path = origin_path
    unless File.exists? present_path && file_path = present_path
    end
  end
  config = {}
  instance_eval %Q{
    config = #{File.read file_path}
  }
  if file_path == origin_path && config[@env]
    config = config[@env]
  end
  return config
end

#load_default_componentsObject

Load default components for application



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/citrus/util/app_util.rb', line 115

def load_default_components
  if @server_type == 'master'
    load Components::Master
  else
    load Components::Proxy
    load Components::Remote if @cur_server[:port]
    load Components::Connection if frontend?
    load Components::Connector if frontend?
    load Components::Session if frontend?
    load Components::PushScheduler if frontend?
    load Components::BackendSession
    load Components::Channel
    load Components::Server
  end
  load Components::Monitor
end

#load_lifecycleObject

Load life cycle



91
92
# File 'lib/citrus/util/app_util.rb', line 91

def load_lifecycle
end

#load_masterObject

Load master info from config/master.json



55
56
57
# File 'lib/citrus/util/app_util.rb', line 55

def load_master
  @master = load_config_file Constants::Filepath::MASTER
end

#load_serversObject

Load server info from config/servers.json



60
61
62
63
64
65
66
67
68
# File 'lib/citrus/util/app_util.rb', line 60

def load_servers
  servers = load_config_file Constants::Filepath::SERVER
  servers.each { |server_type, servers|
    servers.each { |server|
      server[:server_type] = server_type.to_s
      @servers_map[server[:server_id]] = server
    }
  }
end

#opt_component(err, components, method, index, &block) ⇒ Object

Apply command to loaded component

Parameters:

  • err (Object)
  • components (Array)
  • method (Symbol)
  • index (Integer)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/citrus/util/app_util.rb', line 164

def opt_component err, components, method, index, &block
  if err || index >= components.length
    block_given? and yield err
    return
  end
  component = components[index]
  if component && component.respond_to?(method)
    component.send method, &proc{ |err|
      opt_component err, components, method, (index + 1), &block
    }
  else
    opt_component err, components, method, (index + 1), &block
  end
end

#opt_components(components, method, &block) ⇒ Object

Apply command to loaded components

Parameters:

  • components (Array)
  • method (Symbol)


154
155
156
# File 'lib/citrus/util/app_util.rb', line 154

def opt_components components, method, &block
  opt_component nil, components, method, 0, &block
end

#parse_argsObject

Parse command line arguments



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/citrus/util/app_util.rb', line 28

def parse_args
  args_map = {:main => $0}
  ARGV.each { |arg|
    sep = arg.index('=')

    key = arg[0..sep-1].to_sym
    val = arg[sep+1..-1]

    if val == 'true'
      val = true
    end
    if val == 'false'
      val = false
    end
    args_map[key] = val
  }
  return args_map
end

#process_args(args = {}) ⇒ Object

Process server start command

Parameters:

  • args (Hash) (defaults to: {})


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/citrus/util/app_util.rb', line 73

def process_args args={}
  @type = args[:type] ? args[:type].to_sym : :all
  @server_type = args[:server_type] ? args[:server_type] : 'master'
  @server_id = args[:server_id] || @master[:server_id]
  @start_id = args[:start_id]

  if @server_type == 'master'
    @cur_server = @master
  else
    @cur_server = args
  end
end

#register(module_klass, args = {}) ⇒ Object

Register console module

Parameters:

  • module_klass (Class)
  • args (Hash) (defaults to: {})


183
184
185
186
187
188
189
190
# File 'lib/citrus/util/app_util.rb', line 183

def register module_klass, args={}
  module_id = module_klass.module_id
  @modules_registered[module_id] = {
    :module_id => module_id,
    :module_klass => module_klass,
    :args => args
  }
end

#setup_env(args = {}) ⇒ Object

Setup enviroment

Parameters:

  • args (Hash) (defaults to: {})


50
51
52
# File 'lib/citrus/util/app_util.rb', line 50

def setup_env args={}
  @env = args[:env] ? args[:env].to_sym : :development
end

#stop_components(components, index, force, &block) ⇒ Object

Stop components

Parameters:

  • components (Array)
  • index (Integer)
  • force (Boolean)


147
148
# File 'lib/citrus/util/app_util.rb', line 147

def stop_components components, index, force, &block
end