Class: GameMachine::CloudUpdater

Inherits:
Actor::Base
  • Object
show all
Defined in:
server/lib/game_machine/cloud_updater.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Actor::Base

aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, local_path, logger, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #schedule_message_once, #sender, set_player_controller

Instance Attribute Details

#node_statusObject (readonly)

Returns the value of attribute node_status


4
5
6
# File 'server/lib/game_machine/cloud_updater.rb', line 4

def node_status
  @node_status
end

#statsObject (readonly)

Returns the value of attribute stats


4
5
6
# File 'server/lib/game_machine/cloud_updater.rb', line 4

def stats
  @stats
end

#windowsObject (readonly)

Returns the value of attribute windows


4
5
6
# File 'server/lib/game_machine/cloud_updater.rb', line 4

def windows
  @windows
end

Instance Method Details

#on_receive(message) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'server/lib/game_machine/cloud_updater.rb', line 32

def on_receive(message)
  if message.is_a?(JavaLib::ClusterEvent::ClusterMetricsChanged)
    message.get_node_metrics.each do |node_metric|
      heap = JavaLib::StandardMetrics.extract_heap_memory(node_metric)
      stats[:heap] = heap.used / 1024 / 1024
      unless windows
        cpu = JavaLib::StandardMetrics.extract_cpu(node_metric)
        if cpu.respond_to?(:systemLoadAverage)
          stats[:load_average] = cpu.systemLoadAverage.get
          stats[:processor_count] = cpu.processors
        end
      end
    end
  elsif message.is_a?(MessageLib::Statistics)
    @statistics = message
  elsif message.is_a?(String)
    if message == 'update'
      if @statistics
        node_status.set_statistics(@statistics)
      end
      
      node_status.set_last_updated(Time.now.to_i)
      node_status.set_client_count(ClientManager.local_players.size)
      node_status.set_heap_used(stats[:heap])
      if windows
        node_status.set_load_average(0)
      else
        node_status.set_load_average(stats[:load_average])
      end

      cloud_response = JavaLib::CloudClient.get_instance.update_node_status(node_status)
      if cloud_response.status != 200
        self.class.logger.warn "Update node status returned #{cloud_response.status}"
      end
      schedule_message_once('update',5,:seconds)
    end
  end
end

#post_init(*args) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'server/lib/game_machine/cloud_updater.rb', line 5

def post_init(*args)
  @statistics = nil
  @stats = {}
  @windows = Config::CONFIG['target_os'].match(/mswin/)

  if ENV['CONTAINER_ID'] && ENV['CLUSTER_NAME']
    @node_status = MessageLib::NodeStatus.new.
      set_container_id(ENV['CONTAINER_ID'].to_i).
      set_hostname(AppConfig.instance.config.http.host).
      set_port(AppConfig.instance.config.http.port).
      set_cluster_name(ENV['CLUSTER_NAME']).
      set_client_count(ClientManager.local_players.size)
  elsif
    @node_status = MessageLib::NodeStatus.new.
      set_container_id(0).
      set_cluster_name("none").
      set_hostname(AppConfig.instance.config.http.host).
      set_port(AppConfig.instance.config.http.port).
      set_client_count(ClientManager.local_players.size)
  end

  schedule_message_once('update',5,:seconds)
  cluster = JavaLib::Cluster.get(getContext.system)
  cluster.subscribe(getSelf, JavaLib::ClusterEvent::ClusterMetricsChanged.java_class)
  
end