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
13
14
# File 'lib/osctld/auto_start/plan.rb', line 9

def initialize(pool)
  @pool = pool
  @plan = ContinuousExecutor.new(pool.parallel_start)
  @state = AutoStart::State.load(pool)
  @stop = false
end

Instance Attribute Details

#planObject (readonly, protected)

Returns the value of attribute plan.



105
106
107
# File 'lib/osctld/auto_start/plan.rb', line 105

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

#stateObject (readonly, protected)

Returns the value of attribute state.



105
106
107
# File 'lib/osctld/auto_start/plan.rb', line 105

def state
  @state
end

Instance Method Details

#assets(add) ⇒ Object



16
17
18
# File 'lib/osctld/auto_start/plan.rb', line 16

def assets(add)
  state.assets(add)
end

#clearObject



87
88
89
# File 'lib/osctld/auto_start/plan.rb', line 87

def clear
  plan.clear
end

#clear_ct(ct) ⇒ Object



83
84
85
# File 'lib/osctld/auto_start/plan.rb', line 83

def clear_ct(ct)
  state.clear(ct)
end

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/osctld/auto_start/plan.rb', line 107

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,
    ))

    if ret[:status]
      state.set_started(ct)
      return if stop?
      sleep(ct.autostart.delay)
      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



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

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



100
101
102
# File 'lib/osctld/auto_start/plan.rb', line 100

def queue
  plan.queue
end

#resize(new_size) ⇒ Object



91
92
93
# File 'lib/osctld/auto_start/plan.rb', line 91

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

#start(force: false) ⇒ Object



20
21
22
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
# File 'lib/osctld/auto_start/plan.rb', line 20

def start(force: false)
  @stop = false

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

  cts = DB::Containers.get.select do |ct|
    ct.pool == pool && ct.autostart && (force || !state.is_started?(ct))
  end

  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?
        next
      elsif cur_ct.running?
        state.set_started(cur_ct)
        next
      end

      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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osctld/auto_start/plan.rb', line 63

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



95
96
97
98
# File 'lib/osctld/auto_start/plan.rb', line 95

def stop
  @stop = true
  plan.stop
end

#stop?Boolean (protected)

Returns:

  • (Boolean)


139
140
141
# File 'lib/osctld/auto_start/plan.rb', line 139

def stop?
  @stop
end

#stop_ct(ct) ⇒ Object



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

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