Class: AcpcTableManager::Maintainer

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

Class Method Summary collapse

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.



70
71
72
73
74
75
76
77
# File 'lib/acpc_table_manager/maintainer.rb', line 70

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

  maintain!

  log(__method__)
end

Class Method Details

.kill_orphan_proxies(pids, pids_file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/acpc_table_manager/maintainer.rb', line 27

def self.kill_orphan_proxies(pids, pids_file)
  new_pids = []
  pids.each do |pid_pair|
    if AcpcDealer::process_exists?(pid_pair['proxy']) && !AcpcDealer::process_exists?(pid_pair['dealer'])
      AcpcDealer::kill_process pid_pair['proxy']
      sleep 1 # Give the process a chance to exit

      if AcpcDealer::process_exists?(pid_pair['proxy'])
        AcpcDealer::force_kill_process pid_pair['proxy']
        sleep 1 # Give the process a chance to exit

        if AcpcDealer::process_exists?(pid_pair['proxy'])
          raise(
            StandardError.new(
              "Proxy process #{pid_pair['proxy']} couldn't be killed!"
            )
          )
        end
      end
    else
      new_pids << pid_pair
    end
  end
  new_pids
end

.proxy_pids(pids_file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acpc_table_manager/maintainer.rb', line 12

def self.proxy_pids(pids_file)
  if !File.exists?(pids_file)
    File.open(pids_file, 'w') do |pids_file|
      yield pids_file, {} if block_given?
    end
  else
    File.open(pids_file, 'r+') do |pids_file|
      pids = YAML.safe_load(pids_file) || {}
      pids_file.seek(0, IO::SEEK_SET)
      pids_file.truncate(0)
      yield pids_file, pids if block_given?
    end
  end
end

.proxy_pids_fileObject



68
# File 'lib/acpc_table_manager/maintainer.rb', line 68

def self.proxy_pids_file() ::AcpcTableManager.config.proxy_pids_file end

.update_pids(pids) ⇒ Object



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

def self.update_pids(pids)
  pids, pids_file = proxy_pids proxy_pids_file do |pids_file, pids|
    pids = kill_orphan_proxies pids, pids_file

    matches_started = yield if block_given?

    matches_started.each do |info|
      if info
        pids << {'dealer' => info[:dealer][:pid], 'proxy' => info[:proxy]}
      end
    end
    pids_file.write(YAML.dump(pids)) unless pids.empty?
  end
end

Instance Method Details

#check_match(match_id) ⇒ Object



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
181
182
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/acpc_table_manager/maintainer.rb', line 151

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



122
123
124
# File 'lib/acpc_table_manager/maintainer.rb', line 122

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

#enqueue_match!(match_id, options) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/acpc_table_manager/maintainer.rb', line 126

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

#enqueue_waiting_matches(game_definition_key = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/acpc_table_manager/maintainer.rb', line 79

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

#kill_match!(match_id) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/acpc_table_manager/maintainer.rb', line 114

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/acpc_table_manager/maintainer.rb', line 97

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

  self.class().update_pids self.class().proxy_pids_file do
    queues_touched = enqueue_waiting_matches
    matches_started = []
    queues_touched.each do |_, queue|
      matches_started << queue.check_queue!
    end
    matches_started
  end

  clean_up_matches!

  log __method__, msg: "Finished maintenance"
end

#start_proxy!(match_id) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/acpc_table_manager/maintainer.rb', line 139

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