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.



99
100
101
# File 'lib/osctld/auto_stop/plan.rb', line 99

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



77
78
79
# File 'lib/osctld/auto_stop/plan.rb', line 77

def clear
  plan.clear
end

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/osctld/auto_stop/plan.rb', line 101

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

#queueObject



94
95
96
# File 'lib/osctld/auto_stop/plan.rb', line 94

def queue
  plan.queue
end

#resize(new_size) ⇒ Object



81
82
83
# File 'lib/osctld/auto_stop/plan.rb', line 81

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
# File 'lib/osctld/auto_stop/plan.rb', line 23

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

  log(
    :info, pool,
    "Auto-stopping containers, #{pool.parallel_start} 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 }
  cts.sort! do |a, b|
    if a.autostart && b.autostart
      a.autostart <=> b.autostart

    elsif a.autostart
      -1

    elsif b.autostart
      1

    else
      0
    end
  end
  cts.reverse!

  # Progress counters
  total = cts.count
  done = 0
  mutex = Mutex.new

  # Stop the containers
  cmds = cts.map do |ct|
    ContinuousExecutor::Command.new(id: ct.id, priority: 0) 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



89
90
91
92
# File 'lib/osctld/auto_stop/plan.rb', line 89

def stop
  @stop = true
  plan.stop
end

#stop?Boolean (protected)

Returns:

  • (Boolean)


124
125
126
# File 'lib/osctld/auto_stop/plan.rb', line 124

def stop?
  @stop
end

#waitObject



85
86
87
# File 'lib/osctld/auto_stop/plan.rb', line 85

def wait
  plan.wait_until_empty
end