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.



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
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 187

def initialize(options = nil)
  init_logger

  @aws_uploader = AWSUploader.new

  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

#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



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

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



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
305
306
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 274

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 381

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



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

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)


98
99
100
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 98

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

#on_config_updatedObject



121
122
123
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 121

def on_config_updated
  init_config
end

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



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
177
178
179
180
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 127

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



107
108
109
110
111
# File 'lib/mrpin/core/app_info/app_info_base.rb', line 107

def on_server_shutdown
  self.state = EAppState::EAS_SHUTDOWN_STARTED

  nil
end

#post_initObject



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

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



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

def server_name
  result = 'unknown'

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

  result
end