Class: AcpcTableManager::Maintainer

Inherits:
Object
  • Object
show all
Includes:
SimpleLogging
Defined in:
lib/acpc_table_manager/maintainer.rb

Instance Method Summary collapse

Methods included from SimpleLogging

#log, #log_with, #logger

Constructor Details

#initialize(logger_ = AcpcTableManager.new_log('table_manager.log')) ⇒ Maintainer

Returns a new instance of Maintainer.



11
12
13
14
15
16
17
18
# File 'lib/acpc_table_manager/maintainer.rb', line 11

def initialize(logger_ = AcpcTableManager.new_log('table_manager.log'))
  @logger = logger_

  @table_queues = {}
  enqueue_waiting_matches

  log(__method__)
end

Instance Method Details

#check_match(match_id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/acpc_table_manager/maintainer.rb', line 75

def check_match(match_id)
  log(__method__, { match_id: match_id })
  begin
    match = ::AcpcTableManager::Match.find match_id
  rescue Mongoid::Errors::DocumentNotFound
    log(
      __method__,
      {
        msg: "Match \"#{match_id}\" doesn't exist! Killing match.",
        match_id: match_id
      },
      Logger::Severity::ERROR
    )
    return kill_match!(match_id)
  end
  unless @table_queues[match.game_definition_key.to_s].running_matches[match_id]
    log(
      __method__,
      {
        msg: "Match \"#{match_id}\" in seat #{match.seat} doesn't have a proxy! Killing match.",
        match_id: match_id,
        match_name: match.name,
        last_updated_at: match.updated_at,
        running?: match.running?,
        last_slice_viewed: match.last_slice_viewed,
        last_slice_present: match.slices.length - 1
      },
      Logger::Severity::ERROR
    )
    return kill_match!(match_id)
  end
  proxy_pid = @table_queues[match.game_definition_key.to_s].running_matches[match_id][:proxy]

  log __method__, {
    match_id: match_id,
    running?: proxy_pid && AcpcDealer::process_exists?(proxy_pid)
  }

  unless proxy_pid && AcpcDealer::process_exists?(proxy_pid)
    log(
      __method__,
      {
        msg: "The proxy for match \"#{match_id}\" in seat #{match.seat} isn't running! Killing match.",
        match_id: match_id,
        match_name: match.name,
        last_updated_at: match.updated_at,
        running?: match.running?,
        last_slice_viewed: match.last_slice_viewed,
        last_slice_present: match.slices.length - 1
      },
      Logger::Severity::ERROR
    )
    kill_match!(match_id)
  end
end

#clean_up_matches!Object



51
52
53
# File 'lib/acpc_table_manager/maintainer.rb', line 51

def clean_up_matches!
  ::AcpcTableManager::Match.delete_matches_older_than! 1.day
end

#enqueue_match!(match_id, options) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/acpc_table_manager/maintainer.rb', line 55

def enqueue_match!(match_id, options)
  begin
    m = ::AcpcTableManager::Match.find match_id
  rescue Mongoid::Errors::DocumentNotFound
    return kill_match!(match_id)
  else
    @table_queues[m.game_definition_key.to_s].enqueue! match_id, options
  end
end

#enqueue_waiting_matches(game_definition_key = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acpc_table_manager/maintainer.rb', line 20

def enqueue_waiting_matches(game_definition_key=nil)
  if game_definition_key
    @table_queues[game_definition_key] ||= ::AcpcTableManager::TableQueue.new(game_definition_key)
    @table_queues[game_definition_key].my_matches.not_running.and.not_started.each do |m|
      @table_queues[game_definition_key].enqueue! m.id.to_s, m.dealer_options
    end
  else
    ::AcpcTableManager.exhibition_config.games.keys.each do |game_definition_key|
      enqueue_waiting_matches game_definition_key
    end
  end
end

#kill_match!(match_id) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/acpc_table_manager/maintainer.rb', line 43

def kill_match!(match_id)
  log(__method__, match_id: match_id)

  @table_queues.each do |key, queue|
    queue.kill_match!(match_id)
  end
end

#maintain!Object



33
34
35
36
37
38
39
40
41
# File 'lib/acpc_table_manager/maintainer.rb', line 33

def maintain!
  log __method__, msg: "Starting maintenance"

  enqueue_waiting_matches
  @table_queues.each { |key, queue| queue.check_queue! }
  clean_up_matches!

  log __method__, msg: "Finished maintenance"
end

#start_proxy!(match_id) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/acpc_table_manager/maintainer.rb', line 65

def start_proxy!(match_id)
  begin
    match = ::AcpcTableManager::Match.find match_id
  rescue Mongoid::Errors::DocumentNotFound
    return kill_match!(match_id)
  else
    @table_queues[match.game_definition_key.to_s].start_proxy match
  end
end