Class: OsCtld::AutoStart::Plan
- Inherits:
-
Object
- Object
- OsCtld::AutoStart::Plan
- Includes:
- OsCtl::Lib::Utils::Log
- Defined in:
- lib/osctld/auto_start/plan.rb
Instance Attribute Summary collapse
-
#plan ⇒ Object
readonly
protected
Returns the value of attribute plan.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#reboot ⇒ Object
readonly
protected
Returns the value of attribute reboot.
-
#state ⇒ Object
readonly
protected
Returns the value of attribute state.
Instance Method Summary collapse
- #assets(add) ⇒ Object
- #clear ⇒ Object
- #clear_ct(ct) ⇒ Object
- #delay_after_start? ⇒ Boolean protected
- #do_try_start_ct(ct, attempts: 5, cooldown: 5, start_opts: {}) ⇒ Object protected
- #enqueue(ct, priority: 10, start_opts: {}) ⇒ Object
- #fulfil_reboot(ct) ⇒ Object
-
#initialize(pool) ⇒ Plan
constructor
A new instance of Plan.
- #prestart_delay(ct) ⇒ Object protected
- #queue ⇒ Object
- #request_reboot(ct) ⇒ Object
- #resize(new_size) ⇒ Object
- #start(force: false) ⇒ Object
- #start_ct(ct, priority: 10, start_opts: {}, client_handler: nil) ⇒ Object
- #stop ⇒ Object
- #stop? ⇒ Boolean protected
- #stop_ct(ct) ⇒ Object
Constructor Details
#initialize(pool) ⇒ Plan
Returns a new instance of Plan.
10 11 12 13 14 15 16 17 |
# File 'lib/osctld/auto_start/plan.rb', line 10 def initialize(pool) @pool = pool @plan = ContinuousExecutor.new(pool.parallel_start) @state = AutoStart::State.load(pool) @reboot = AutoStart::Reboot.load(pool) @stop = false @nproc = Etc.nprocessors end |
Instance Attribute Details
#plan ⇒ Object (readonly, protected)
Returns the value of attribute plan.
146 147 148 |
# File 'lib/osctld/auto_start/plan.rb', line 146 def plan @plan end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
8 9 10 |
# File 'lib/osctld/auto_start/plan.rb', line 8 def pool @pool end |
#reboot ⇒ Object (readonly, protected)
Returns the value of attribute reboot.
146 147 148 |
# File 'lib/osctld/auto_start/plan.rb', line 146 def reboot @reboot end |
#state ⇒ Object (readonly, protected)
Returns the value of attribute state.
146 147 148 |
# File 'lib/osctld/auto_start/plan.rb', line 146 def state @state end |
Instance Method Details
#assets(add) ⇒ Object
19 20 21 22 |
# File 'lib/osctld/auto_start/plan.rb', line 19 def assets(add) state.assets(add) reboot.assets(add) end |
#clear ⇒ Object
128 129 130 |
# File 'lib/osctld/auto_start/plan.rb', line 128 def clear plan.clear end |
#clear_ct(ct) ⇒ Object
123 124 125 126 |
# File 'lib/osctld/auto_start/plan.rb', line 123 def clear_ct(ct) state.clear(ct) reboot.clear(ct) end |
#delay_after_start? ⇒ Boolean (protected)
194 195 196 197 |
# File 'lib/osctld/auto_start/plan.rb', line 194 def delay_after_start? lavg = OsCtl::Lib::LoadAvg.new lavg.avg[1] >= @nproc end |
#do_try_start_ct(ct, attempts: 5, cooldown: 5, start_opts: {}) ⇒ Object (protected)
148 149 150 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 |
# File 'lib/osctld/auto_start/plan.rb', line 148 def do_try_start_ct(ct, attempts: 5, cooldown: 5, start_opts: {}) attempts.times do |i| return if stop? ret = Commands::Container::Start.run(**start_opts.merge( pool: ct.pool.name, id: ct.id, wait: 'infinity', )) if ret[:status] state.set_started(ct) return if stop? if delay_after_start? log(:info, ct, "Autostart delay for #{ct.autostart.delay} seconds") sleep(ct.autostart.delay) else log(:info, ct, 'Skipping autostart delay thanks to low system load average') end return end if i+1 == attempts log(:warn, ct, 'All attempts to start the container have failed') return end if stop? log(:warn, ct, "Unable to start the container, giving up to stop") return else pause = cooldown + i * cooldown log(:warn, ct, "Unable to start the container, retrying in #{pause} seconds") sleep(pause) end end end |
#enqueue(ct, priority: 10, start_opts: {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/osctld/auto_start/plan.rb', line 74 def enqueue(ct, priority: 10, start_opts: {}) plan << ( ContinuousExecutor::Command.new(id: ct.id, priority: priority) do |cmd| cur_ct = DB::Containers.find(cmd.id, pool) next if cur_ct.nil? || cur_ct.running? prestart_delay(cur_ct) log(:info, ct, 'Starting enqueued container') do_try_start_ct( cur_ct, start_opts: start_opts.merge(queue: false), ) end ) end |
#fulfil_reboot(ct) ⇒ Object
115 116 117 |
# File 'lib/osctld/auto_start/plan.rb', line 115 def fulfil_reboot(ct) reboot.clear(ct) end |
#prestart_delay(ct) ⇒ Object (protected)
188 189 190 191 192 |
# File 'lib/osctld/auto_start/plan.rb', line 188 def prestart_delay(ct) delay = rand(0.0..3.0) log(:info, ct, "Delaying auto-start by #{delay.round(2)}s") sleep(delay) end |
#queue ⇒ Object
141 142 143 |
# File 'lib/osctld/auto_start/plan.rb', line 141 def queue plan.queue end |
#request_reboot(ct) ⇒ Object
111 112 113 |
# File 'lib/osctld/auto_start/plan.rb', line 111 def request_reboot(ct) reboot.add(ct) end |
#resize(new_size) ⇒ Object
132 133 134 |
# File 'lib/osctld/auto_start/plan.rb', line 132 def resize(new_size) plan.resize(new_size) end |
#start(force: false) ⇒ Object
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 |
# File 'lib/osctld/auto_start/plan.rb', line 24 def start(force: false) @stop = false log( :info, pool, "Auto-starting containers, #{pool.parallel_start} containers at a time" ) # Select containers for autostart cts = DB::Containers.get.select do |ct| next(false) if ct.pool != pool next(true) if reboot.include?(ct) && ct.can_start? next(true) if ct.autostart && ct.can_start? && (force || !state.is_started?(ct)) next(false) end # Preschedule the containers if CpuScheduler.use? cts.reject do |ct| ct.running? end.sort do |a, b| b.hints.cpu_daily.usage_us <=> a.hints.cpu_daily.usage_us end.each do |ct| CpuScheduler.preschedule_ct(ct) end end # Start the containers plan << (cts.map do |ct| ContinuousExecutor::Command.new( id: ct.id, priority: ct.autostart.priority, ) do |cmd| cur_ct = DB::Containers.find(cmd.id, pool) if cur_ct.nil? || !cur_ct.can_start? CpuScheduler.cancel_preschedule_ct(ct) next elsif cur_ct.running? CpuScheduler.cancel_preschedule_ct(ct) state.set_started(cur_ct) next end prestart_delay(cur_ct) log(:info, cur_ct, 'Auto-starting container') do_try_start_ct(cur_ct) end end) end |
#start_ct(ct, priority: 10, start_opts: {}, client_handler: nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/osctld/auto_start/plan.rb', line 90 def start_ct(ct, priority: 10, start_opts: {}, client_handler: nil) plan.execute( ContinuousExecutor::Command.new(id: ct.id, priority: priority) do |cmd| cur_ct = DB::Containers.find(cmd.id, pool) next if cur_ct.nil? || cur_ct.running? prestart_delay(cur_ct) log(:info, ct, 'Starting enqueued container') Commands::Container::Start.run( **start_opts.merge( pool: cur_ct.pool.name, id: cur_ct.id, queue: false, internal: {handler: client_handler}, ), ) end, timeout: start_opts ? (start_opts[:wait] || Container::DEFAULT_START_TIMEOUT) : nil, ) end |
#stop ⇒ Object
136 137 138 139 |
# File 'lib/osctld/auto_start/plan.rb', line 136 def stop @stop = true plan.stop end |
#stop? ⇒ Boolean (protected)
199 200 201 |
# File 'lib/osctld/auto_start/plan.rb', line 199 def stop? @stop end |
#stop_ct(ct) ⇒ Object
119 120 121 |
# File 'lib/osctld/auto_start/plan.rb', line 119 def stop_ct(ct) plan.remove(ct.id) end |