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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/osctld/auto_start/plan.rb', line 38

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')
      Commands::Container::Start.run(
        start_opts.merge(pool: cur_ct.pool.name, id: cur_ct.id, queue: false)
      )
    end
  )
end

#queueObject



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

def queue
  plan.queue
end

#queue_cmd_for(ct) ⇒ Object (protected)



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

def queue_cmd_for(ct)

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
35
36
# 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')
      Commands::Container::Start.run(pool: cur_ct.pool.name, id: cur_ct.id)

      sleep(cur_ct.autostart.delay)
    end
  end)
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/auto_start/plan.rb', line 52

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