class AppInfoBase

Attributes

boot_time[R]
config[R]
logger[R]

Properties

managers_bundles_map[R]
managers_list[R]
managers_with_json_list[R]
state[R]

Public Class Methods

new(options = nil) click to toggle source

Methods

# 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

Public Instance Methods

call_in_all_managers(method_name, delay_between_calls = 0.0) click to toggle source
# 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) click to toggle source
# 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_data() click to toggle source
# 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
host() click to toggle source
# 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?() click to toggle source
# 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_updated() click to toggle source
# 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) click to toggle source
# 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_shutdown() click to toggle source
# 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_init() click to toggle source
# 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_name() click to toggle source
# 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
state=(value) click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 24
def state=(value)
  return if @state == value

  on_change_state_before(value)

  @state = value

  @logger.info("app state is #{@state}") unless @logger.nil?

  on_change_state_after(value)

  nil
end

Protected Instance Methods

add_manager_bundles(manager) click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 336
def add_manager_bundles(manager)
  @managers_bundles_map[manager.build_type] = manager
  @managers_list << manager

  nil
end
create_manager(manager_class) click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 344
def create_manager(manager_class)
  result = manager_class.new

  @managers_list << result

  if result.is_a?(ManagerWithJsonData)
    @managers_with_json_list << result
  end

  result
end
init_config() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 256
def init_config
  @config = @config || AppConfigBase.first

  @logger.warn('config is empty') if @config.nil?

  nil
end
init_managers() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 265
def init_managers
  @managers_list           = []
  @managers_with_json_list = []
  #key - build type, value - manager_bundle
  @managers_bundles_map    = {}

  @manager_scheduler = nil

  nil
end
init_periodical_tasks() click to toggle source

todo:review

# File lib/mrpin/core/app_info/app_info_base.rb, line 380
def init_periodical_tasks
  # Tasks Every

  # @manager_scheduler.scheduler.every '1h', overlap: false, first_in: '30m' do

  # Tasks Cron

  # Minute   Hour   Day of Month       Month          Day of Week
  # (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)

  # every day
  # @manager_scheduler.scheduler.cron '00 00 * * *' do
  # every tuesday 14:00
  # @manager_scheduler.scheduler.cron '00 14 * * 2' do
end
on_change_state_after(state) click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 102
def on_change_state_after(state)
  case state
    when EAppState::EAS_INIT_CONFIG
      init_config
    when EAppState::EAS_INIT_MANAGERS
      init_managers
    when EAppState::EAS_INIT_PERIODICAL_TASKS
      init_periodical_tasks
    when EAppState::EAS_START_REMOTE_SERVERS
      start_remote_servers
    when EAppState::EAS_MAINTENANCE
      on_server_maintenance_on
    else
      #do nothing
  end

  nil
end
on_change_state_before(state) click to toggle source

Events

# File lib/mrpin/core/app_info/app_info_base.rb, line 78
def on_change_state_before(state)
  case state
    when EAppState::EAS_POST_INIT
      call_in_all_managers_with_benchmark('[POST_INIT]', 'post_init')
    when EAppState::EAS_LOAD_INIT_DATA
      call_in_all_managers_with_benchmark('[LOAD_DATA]', 'load_init_data')

      call_in_all_managers('on_data_loaded')
    when EAppState::EAS_START_MANAGER_TASKS
      call_in_all_managers('start_tasks', 0.1)
    when EAppState::EAS_LAUNCH_COMPLETE
      call_in_all_managers('on_server_started')

      @boot_time = Time.now
    when EAppState::EAS_SHUTDOWN_STARTED
      call_in_all_managers('on_server_shutdown')
    else
      #do nothing
  end

  nil
end
on_server_launch_complete() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 122
def on_server_launch_complete
  if @config.nil? || @config.server_on_maintenance
    self.state = EAppState::EAS_MAINTENANCE
  else
    self.state = EAppState::EAS_LAUNCH_COMPLETE
  end

  nil
end
on_server_maintenance_on() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 140
def on_server_maintenance_on
  call_in_all_managers('on_server_maintenance_on')

  nil
end
start_remote_servers() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 372
def start_remote_servers
  assert(false, 'please override')

  nil
end
try_create_admin_user(login, password, role) click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 397
def try_create_admin_user(login, password, role)
  result = AdminUser.where(email: login).first

  if result.nil?
    result = AdminUser.create!(email: login, password: password, password_confirmation: password, role: role)
  end

  result
end

Private Instance Methods

init_logger() click to toggle source
# File lib/mrpin/core/app_info/app_info_base.rb, line 248
def init_logger
  @logger           = Logger.new($stdout)
  @logger.formatter = LoggerSdk.new

  nil
end