Class: OsCtld::AutoStart::State

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, Lockable
Defined in:
lib/osctld/auto_start/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(pool) ⇒ State

Returns a new instance of State.

Parameters:



21
22
23
24
25
# File 'lib/osctld/auto_start/state.rb', line 21

def initialize(pool)
  @pool = pool
  @started_cts = []
  init_lock
end

Instance Attribute Details

#poolPool (readonly)

Returns:



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

def pool
  @pool
end

#started_ctsObject (readonly, protected)

Returns the value of attribute started_cts.



77
78
79
# File 'lib/osctld/auto_start/state.rb', line 77

def started_cts
  @started_cts
end

Class Method Details

.load(pool) ⇒ AutoStart::State

Parameters:

Returns:



14
15
16
17
18
# File 'lib/osctld/auto_start/state.rb', line 14

def self.load(pool)
  st = new(pool)
  st.load
  st
end

Instance Method Details

#assets(add) ⇒ Object

Parameters:



28
29
30
31
32
33
34
35
36
37
# File 'lib/osctld/auto_start/state.rb', line 28

def assets(add)
  add.file(
    state_path,
    desc: 'Contains a list of auto-started containers',
    user: 0,
    group: 0,
    mode: 0o600,
    optional: true
  )
end

#clear(ct) ⇒ Object

Parameters:



66
67
68
69
70
71
72
73
# File 'lib/osctld/auto_start/state.rb', line 66

def clear(ct)
  exclusively do
    next unless started_cts.include?(ct.id)

    started_cts.delete(ct.id)
    save
  end
end

#is_started?(ct) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/osctld/auto_start/state.rb', line 59

def is_started?(ct)
  inclusively do
    started_cts.include?(ct.id)
  end
end

#loadObject



39
40
41
42
43
44
45
# File 'lib/osctld/auto_start/state.rb', line 39

def load
  return unless File.exist?(state_path)

  File.open(state_path).each_line do |line|
    started_cts << line.strip
  end
end

#saveObject (protected)



79
80
81
82
83
84
85
# File 'lib/osctld/auto_start/state.rb', line 79

def save
  exclusively do
    regenerate_file(state_path, 0o600) do |new|
      started_cts.each { |id| new.puts(id) }
    end
  end
end

#set_started(ct) ⇒ Object

Parameters:



48
49
50
51
52
53
54
55
# File 'lib/osctld/auto_start/state.rb', line 48

def set_started(ct)
  exclusively do
    next if started_cts.include?(ct.id)

    started_cts << ct.id
    save
  end
end

#state_pathObject (protected)



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

def state_path
  File.join(pool.autostart_dir, 'started-cts.txt')
end