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.



88
89
90
91
92
93
94
95
# File 'lib/acpc_table_manager/maintainer.rb', line 88

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



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 'lib/acpc_table_manager/maintainer.rb', line 44

def self.kill_orphan_proxies(pids, pids_file)
  new_pids = []
  pids.each do |pid_pair|
    proxy_running = AcpcDealer::process_exists?(pid_pair['proxy'])
    if proxy_running && AcpcDealer::process_exists?(pid_pair['dealer'])
      new_pids << pid_pair
    elsif proxy_running
      kill_process_if_running pid_pair['proxy'] do
        raise(
          StandardError.new(
            "Proxy process #{pid_pair['proxy']} couldn't be killed!"
          )
        )
      end
    else
      kill_process_if_running pid_pair['dealer'] do
        raise(
          StandardError.new(
            "Dealer process #{pid_pair['dealer']} couldn't be killed!"
          )
        )
      end
    end
  end
  new_pids
end

.kill_process_if_running(pid) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/acpc_table_manager/maintainer.rb', line 27

def self.kill_process_if_running(pid)
  begin
    AcpcDealer::kill_process pid
    sleep 1 # Give the process a chance to exit

    if AcpcDealer::process_exists?(pid)
      AcpcDealer::force_kill_process pid
      sleep 1 # Give the process a chance to exit

      if AcpcDealer::process_exists?(pid)
        yield if block_given?
      end
    end
  rescue Errno::ESRCH
  end
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



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

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

.update_pids(pids) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/acpc_table_manager/maintainer.rb', line 71

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/acpc_table_manager/maintainer.rb', line 191

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



142
143
144
# File 'lib/acpc_table_manager/maintainer.rb', line 142

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

#enqueue_match!(match_id, options) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/acpc_table_manager/maintainer.rb', line 146

def enqueue_match!(match_id, options)
  begin
    m = ::AcpcTableManager::Match.find match_id
  rescue Mongoid::Errors::DocumentNotFound

    log(
      __method__,
      {
        msg: "Match not found",
        match_id: match_id
      },
      Logger::Severity::ERROR
    )

    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



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

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



132
133
134
135
136
137
138
139
140
# File 'lib/acpc_table_manager/maintainer.rb', line 132

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

  @table_queues.each do |key, queue|
    log(__method__, {queue: key, match_id: match_id})

    queue.kill_match!(match_id)
  end
end

#maintain!Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/acpc_table_manager/maintainer.rb', line 115

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/acpc_table_manager/maintainer.rb', line 169

def start_proxy!(match_id)
  begin
    match = ::AcpcTableManager::Match.find match_id
  rescue Mongoid::Errors::DocumentNotFound

    log(
      __method__,
      {
        msg: "Match not found",
        match_id: match_id
      },
      Logger::Severity::ERROR
    )

    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