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.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 223

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



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 277

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



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 301

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



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 408

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 39

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'
  end

  result
end

#is_server_on_maintenance?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 69

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

#on_config_updatedObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 147

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



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 161

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.first(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:\n#{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



133
134
135
136
137
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 133

def on_server_shutdown
  self.state = EAppState::EAS_SHUTDOWN_STARTED

  nil
end

#post_initObject



358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 358

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



58
59
60
61
62
63
64
65
66
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 58

def server_name
  result = 'unknown'

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

  result
end