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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 183

def initialize(options = nil)
  init_logger

  @aws_uploader = AWSUploader.new

  begin
    init_config
    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

#aws_uploaderObject (readonly)

Returns the value of attribute aws_uploader.



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

def aws_uploader
  @aws_uploader
end

#boot_timeObject (readonly)

Returns the value of attribute boot_time.



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

def boot_time
  @boot_time
end

#configObject (readonly)

Returns the value of attribute config.



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

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.



23
24
25
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 23

def state
  @state
end

Instance Method Details

#call_in_all_managers(method_name, delay_between_calls = 0.0) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 248

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



272
273
274
275
276
277
278
279
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
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 272

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



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 396

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(errors)
  end

  result
end

#hostObject



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

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

#invalidate_cacheObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 368

def invalidate_cache
  result = nil

  errors = call_in_all_managers_with_benchmark('[INVALIDATE_CACHE]', 'invalidate_cache', false)

  errors.map!(&:to_s)

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

  result
end

#is_server_on_maintenance?Boolean

Returns:



94
95
96
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 94

def is_server_on_maintenance?
  @state == EAppState::EAS_MAINTENANCE
end

#on_config_updatedObject



117
118
119
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 117

def on_config_updated
  init_config
end

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 123

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?

  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}"
  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



103
104
105
106
107
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 103

def on_server_shutdown
  self.state = EAppState::EAS_SHUTDOWN_STARTED

  nil
end

#post_initObject



329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 329

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



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

def server_name
  result = 'unknown'

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

  result
end