Class: OsCtld::AutoStart::Plan

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Plan

Returns a new instance of Plan.



9
10
11
12
# File 'lib/osctld/auto_start/plan.rb', line 9

def initialize(pool)
  @pool = pool
  @plan = ContinuousExecutor.new(pool.parallel_start)
end

Instance Attribute Details

#planObject (readonly, protected)

Returns the value of attribute plan.



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

def plan
  @plan
end

#poolObject (readonly)

Returns the value of attribute pool.



7
8
9
# File 'lib/osctld/auto_start/plan.rb', line 7

def pool
  @pool
end

Instance Method Details

#clearObject



68
69
70
# File 'lib/osctld/auto_start/plan.rb', line 68

def clear
  plan.clear
end

#do_try_start_ct(ct, attempts: 5, cooldown: 5, start_opts: {}) ⇒ Object (protected)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctld/auto_start/plan.rb', line 87

def do_try_start_ct(ct, attempts: 5, cooldown: 5, start_opts: {})
  attempts.times do |i|
    ret = Commands::Container::Start.run(start_opts.merge(
      pool: ct.pool.name,
      id: ct.id,
    ))

    if ret[:status]
      sleep(ct.autostart.delay)
      return
    end

    if i+1 == attempts
      log(:warn, ct, 'All attempts to start the container have failed')
      return
    end

    pause = cooldown + i * cooldown
    log(:warn, ct, "Unable to start the container, retrying in #{pause} seconds")
    sleep(pause)
  end
end

#enqueue(ct, priority: 10, start_opts: {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctld/auto_start/plan.rb', line 36

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?

      log(:info, ct, 'Starting enqueued container')
      do_try_start_ct(cur_ct, start_opts: start_opts.merge(queue: false))
    end
  )
end

#queueObject



80
81
82
# File 'lib/osctld/auto_start/plan.rb', line 80

def queue
  plan.queue
end

#resize(new_size) ⇒ Object



72
73
74
# File 'lib/osctld/auto_start/plan.rb', line 72

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

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctld/auto_start/plan.rb', line 14

def start
  log(
    :info, pool,
    "Auto-starting containers, #{pool.parallel_start} containers at a time"
  )

  cts = DB::Containers.get.select { |ct| ct.pool == pool && ct.autostart }

  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)
      next if cur_ct.nil? || cur_ct.running?

      log(:info, ct, 'Auto-starting container')
      do_try_start_ct(cur_ct)
    end
  end)
end

#start_ct(ct, priority: 10, start_opts: {}, client_handler: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osctld/auto_start/plan.rb', line 48

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?

      log(:info, ct, 'Starting enqueued container')
      Commands::Container::Start.run(
        start_opts.merge(pool: cur_ct.pool.name, id: cur_ct.id, queue: false),
        {handler: client_handler},
      )
    end,
    timeout: start_opts ? (start_opts[:wait] || 60) : nil,
  )
end

#stopObject



76
77
78
# File 'lib/osctld/auto_start/plan.rb', line 76

def stop
  plan.stop
end

#stop_ct(ct) ⇒ Object



64
65
66
# File 'lib/osctld/auto_start/plan.rb', line 64

def stop_ct(ct)
  plan.remove(ct.id)
end