Class: OsCtld::AutoStop::Plan

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/auto_stop/plan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Plan

Returns a new instance of Plan.



10
11
12
13
14
15
# File 'lib/osctld/auto_stop/plan.rb', line 10

def initialize(pool)
  @pool = pool
  @plan = ContinuousExecutor.new(pool.parallel_stop)
  @stop = false
  @nproc = Etc.nprocessors
end

Instance Attribute Details

#planObject (readonly, protected)

Returns the value of attribute plan.



156
157
158
# File 'lib/osctld/auto_stop/plan.rb', line 156

def plan
  @plan
end

#poolObject (readonly)

Returns the value of attribute pool.



8
9
10
# File 'lib/osctld/auto_stop/plan.rb', line 8

def pool
  @pool
end

Instance Method Details

#clearObject



130
131
132
# File 'lib/osctld/auto_stop/plan.rb', line 130

def clear
  plan.clear
end

#do_stop_ct(ct, message: nil) ⇒ Object (protected)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/osctld/auto_stop/plan.rb', line 158

def do_stop_ct(ct, message: nil)
  if ct.ephemeral?
    Commands::Container::Delete.run(
      pool: pool.name,
      id: ct.id,
      force: true,
      progress: false,
      manipulation_lock: 'ignore',
      message: message,
    )
  else
    Commands::Container::Stop.run(
      pool: pool.name,
      id: ct.id,
      progress: false,
      manipulation_lock: 'ignore',
      message: message,
    )

    pool.autostart_plan.clear_ct(ct)
  end
end

#log_typeObject



151
152
153
# File 'lib/osctld/auto_stop/plan.rb', line 151

def log_type
  "#{pool.name}:auto-stop"
end

#queueObject



147
148
149
# File 'lib/osctld/auto_stop/plan.rb', line 147

def queue
  plan.queue
end

#resize(new_size) ⇒ Object



134
135
136
# File 'lib/osctld/auto_stop/plan.rb', line 134

def resize(new_size)
  plan.resize(new_size)
end

#start(message: nil, client_handler: nil) ⇒ Object

Stop all containers on pool

It is assumed that manipulation lock is held on all containers on the pool.

Parameters:



23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
# File 'lib/osctld/auto_stop/plan.rb', line 23

def start(message: nil, client_handler: nil)
  @stop = false

  log(
    :info,
    "Auto-stopping containers, #{pool.parallel_stop} containers at a time"
  )

  # Sort containers by reversed autostart priority -- containers with
  # the lowest priority are stopped first
  cts = DB::Containers.get.select { |ct| ct.pool == pool }

  log(:info, "#{cts.size} containers to stop")

  if CpuScheduler.use_sequential_start_stop?
    log(:info, 'Using sequential auto-stop')

    cts.sort! do |a, b|
      a_conf = a.run_conf
      b_conf = b.run_conf

      # Stop running containers first
      if a_conf && !b_conf
        -1
      elsif !a_conf && b_conf
        1
      elsif !a_conf && !b_conf
        0

      # Stop containers with a CPU package first
      elsif a_conf.cpu_package && !b_conf.cpu_package
        -1
      elsif !a_conf.cpu_package && b_conf.cpu_package
        1
      elsif !a_conf.cpu_package && !b_conf.cpu_package
        0

      # Same CPU package, sort by autostart priority
      elsif a_conf.cpu_package == b_conf.cpu_package
        if a.autostart && b.autostart
          b.autostart <=> a.autostart
        elsif a.autostart
          1
        elsif b.autostart
          -1
        else
          0
        end

      # Sort by CPU package, lower package first
      else
        a_conf.cpu_package <=> b_conf.cpu_package
      end
    end
  else
    log(:info, 'Using priority auto-stop')

    cts.sort! do |a, b|
      if a.autostart && b.autostart
        b.autostart <=> a.autostart
      elsif a.autostart
        1
      elsif b.autostart
        -1
      else
        0
      end
    end
  end

  # Progress counters
  total = cts.count
  done = 0
  mutex = Mutex.new
  debug = Daemon.get.config.debug?

  # Stop the containers
  cmds = cts.each_with_index.map do |ct, i|
    if debug
      run_conf = ct.run_conf

      log(
        :debug,
        "[#{(i+1).to_s.rjust(4)}/#{total}] #{ct.id} priority=#{ct.autostart ? ct.autostart.priority : '-'} cpu-package=#{run_conf ? run_conf.cpu_package : '-'}"
      )
    end

    ContinuousExecutor::Command.new(id: ct.id, priority: i) do |cmd|
      if client_handler
        mutex.synchronize do
          done += 1
          client_handler.send_update(
            "[#{done}/#{total}] "+
            (ct.ephemeral? ? 'Deleting ephemeral container' : 'Stopping container')+
            " #{ct.ident}"
          )
        end
      end

      log(:info, ct, 'Auto-stopping container')
      do_stop_ct(ct, message: message)
    end
  end

  plan << cmds
end

#stopObject



142
143
144
145
# File 'lib/osctld/auto_stop/plan.rb', line 142

def stop
  @stop = true
  plan.stop
end

#stop?Boolean (protected)

Returns:

  • (Boolean)


181
182
183
# File 'lib/osctld/auto_stop/plan.rb', line 181

def stop?
  @stop
end

#waitObject



138
139
140
# File 'lib/osctld/auto_stop/plan.rb', line 138

def wait
  plan.wait_until_empty
end