Class: AppInfoBase

Inherits:
Object show all
Includes:
Benchmark, Singleton
Defined in:
lib/mrpin/core/app_info/app_info_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ AppInfoBase

Returns a new instance of AppInfoBase.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 202

def initialize(options = nil)
  init_logger

  begin

    self.state = EAppState::EAS_INIT_CONFIG

    self.state = EAppState::EAS_INIT_MANAGERS
  rescue Exception => e
    on_server_error(e.to_s, e)

    @logger.error "can't start server with error #{e}."

    sleep 60

    exit
  end

  at_exit do
    on_server_shutdown
  end

end

Instance Attribute Details

#boot_timeObject (readonly)

Returns the value of attribute boot_time.



17
18
19
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 17

def boot_time
  @boot_time
end

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 19

def config
  @config
end

#loggerObject (readonly)

Properties



10
11
12
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 10

def logger
  @logger
end

#managers_bundles_mapObject (readonly)

Returns the value of attribute managers_bundles_map.



15
16
17
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 15

def managers_bundles_map
  @managers_bundles_map
end

#managers_listObject (readonly)

Returns the value of attribute managers_list.



12
13
14
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 12

def managers_list
  @managers_list
end

#managers_with_json_listObject (readonly)

Returns the value of attribute managers_with_json_list.



13
14
15
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 13

def managers_with_json_list
  @managers_with_json_list
end

#stateObject

Returns the value of attribute state.



21
22
23
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 21

def state
  @state
end

Instance Method Details

#call_in_all_managers(method_name, delay_between_calls = 0.0) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 256

def call_in_all_managers(method_name, delay_between_calls = 0.0)
  result = []

  @managers_list.each do |manager|
    next unless manager.respond_to?(method_name)

    begin
      manager.send(method_name)

      if delay_between_calls > 0
        sleep(delay_between_calls)
      end

    rescue Exception => e
      on_server_error("Error on #{__method__}: #{e.to_s}", e)

      result << e
    end
  end

  result
end

#call_in_all_managers_with_benchmark(benchmark_name, method_name, exit_on_error = true) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 280

def call_in_all_managers_with_benchmark(benchmark_name, method_name, exit_on_error = true)
  result = []

  white_spaces_count = 50

  spaces = ' ' * white_spaces_count

  benchmark("#{benchmark_name}\n#{spaces}#{CAPTION}", white_spaces_count, FORMAT) do |benchmark|
    @managers_list.each do |manager|
      benchmark.report("#{manager.class.name}") do
        begin
          next unless manager.respond_to?(method_name)

          manager.send(method_name)
        rescue Exception => e
          on_server_error("Error on #{__method__}: #{e.to_s}", e)

          result << e
        end
      end
    end
  end

  if !result.empty? && exit_on_error
    result.each do |e|
      on_server_error(e.to_s, e)
    end

    exit
  end

  result
end

#cleanup_dataObject



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 387

def cleanup_data
  result = nil

  errors = call_in_all_managers('cleanup_data')

  errors.map!(&:to_s)

  if errors.empty?
    result = get_response_ok
  else
    result = get_response_error(nil, errors)
  end

  result
end

#hostObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 66

def host
  result = nil

  if !@config.nil? && !@config.host.nil?
    result = @config.host
  end

  if result.blank?
    @logger.warn('your forgot setup domain name in AppConfig')
  end

  if Rails.env.development?
    result = 'localhost:3000'
  end

  result
end

#is_server_on_maintenance?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 96

def is_server_on_maintenance?
  @state != EAppState::EAS_LAUNCH_COMPLETE
end

#on_config_updatedObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 126

def on_config_updated
  init_config

  if @config.server_on_maintenance && self.state == EAppState::EAS_LAUNCH_COMPLETE
    self.state = EAppState::EAS_MAINTENANCE
  elsif !@config.server_on_maintenance && self.state == EAppState::EAS_MAINTENANCE
    self.state = EAppState::EAS_LAUNCH_COMPLETE
  end

  nil
end

#on_server_error(message, exception = nil, request = nil, player_id = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 140

def on_server_error(message, exception = nil, request = nil, player_id = nil)
  request_class = request.class.to_s
  player_id     ||= 'unknown'

  exception ||= Exception.new(message)

  stacktrace = exception.backtrace

  stacktrace = caller if stacktrace.nil?

  stacktrace = stacktrace.last(10)

  logger_message = ''

  logger_message += "\nerror message:   #{message}"

  unless request.nil?
    logger_message += "\nerror request:   #{request}"
    logger_message += "\nerror request class:   #{request.class}"
  end

  if Rails.env.development?
    logger_message += "\nerror player_id: #{player_id}" unless player_id.nil?
    logger_message += "\nerror stack trace: #{stacktrace.join("\n")}"
  end

  @logger.error(logger_message)

  if Rails.env.production?

    md5 = Digest::MD5.hexdigest(request_class.to_s + message.to_s)

    error = ErrorServerBase.where(md5: md5).first

    if error.nil?
      #create
      error                = ErrorServerBase.new
      error.first_error_at = Time.now.to_i
      error.message        = UtilsString.to_utf8(message)
      error.request_class  = request_class
      error.md5            = md5
    end

    player_errors_count = error.players_ids_map[player_id] || 0

    error.last_error_at              = Time.now.to_i
    error.players_ids_map[player_id] = player_errors_count + 1
    error.throws_count               += 1
    error.players_count              = error.players_ids_map.size
    error.stack_trace                = stacktrace

    error.save!
  end


end

#on_server_shutdownObject



112
113
114
115
116
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 112

def on_server_shutdown
  self.state = EAppState::EAS_SHUTDOWN_STARTED

  nil
end

#post_initObject



337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 337

def post_init

  self.state = EAppState::EAS_POST_INIT

  self.state = EAppState::EAS_LOAD_INIT_DATA

  self.state = EAppState::EAS_INIT_PERIODICAL_TASKS

  self.state = EAppState::EAS_START_MANAGER_TASKS

  self.state = EAppState::EAS_START_REMOTE_SERVERS
end

#server_nameObject



85
86
87
88
89
90
91
92
93
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 85

def server_name
  result = 'unknown'

  unless @config.nil?
    result = @config.server_name
  end

  result
end