Class: OsCtld::HintUpdater

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Exception, OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/hint_updater.rb

Overview

Update container hints at regular intervals

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ HintUpdater

Returns a new instance of HintUpdater.

Parameters:



10
11
12
13
# File 'lib/osctld/hint_updater.rb', line 10

def initialize(pool)
  @pool = pool
  @ct_queue = OsCtl::Lib::Queue.new
end

Instance Method Details

#log_typeObject



32
33
34
# File 'lib/osctld/hint_updater.rb', line 32

def log_type
  @pool.name
end

#run_ct_updatesObject (protected)



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
# File 'lib/osctld/hint_updater.rb', line 38

def run_ct_updates
  loop do
    v = @ct_queue.pop(timeout: 8 * 60 * 60)
    return if v == :stop

    t1 = Time.now
    log(:info, 'Updating container hints')

    DB::Containers.get.each do |ct|
      break if @stop

      next if ct.pool != @pool || !ct.running?

      begin
        ct.update_hints
      rescue StandardError => e
        log(:warn, ct, "Unable to update hints: #{e.message} (#{e.class})")
        log(:warn, ct, denixstorify(e.backtrace))
      end

      sleep(0.2)
    end

    break if @stop

    log(:info, "Finished updating container hints in #{(Time.now - t1).round(2)}s")
  end
end

#startObject



15
16
17
18
19
20
# File 'lib/osctld/hint_updater.rb', line 15

def start
  raise 'already started' if @ct_thread

  @stop = false
  @ct_thread = Thread.new { run_ct_updates }
end

#stopObject



22
23
24
25
26
27
28
29
30
# File 'lib/osctld/hint_updater.rb', line 22

def stop
  return unless @ct_thread

  @stop = true
  @ct_queue.clear
  @ct_queue << :stop
  @ct_thread.join
  @ct_thread = nil
end