Class: Haconiwa::SmallCgroup

Inherits:
Object
  • Object
show all
Defined in:
lib/haconiwa/small_cgroup.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "haconiwa-#{$$}", pid: $$) ⇒ SmallCgroup

Returns a new instance of SmallCgroup.



24
25
26
27
28
# File 'lib/haconiwa/small_cgroup.rb', line 24

def initialize(name: "haconiwa-#{$$}", pid: $$)
  @name = name
  @pid  = pid
  @active_dirs = []
end

Class Attribute Details

.fs_rootObject

Returns the value of attribute fs_root.



7
8
9
# File 'lib/haconiwa/small_cgroup.rb', line 7

def fs_root
  @fs_root
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/haconiwa/small_cgroup.rb', line 29

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



29
30
31
# File 'lib/haconiwa/small_cgroup.rb', line 29

def pid
  @pid
end

Class Method Details

.register_at_exit(pid: nil, name: nil, dirs: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/haconiwa/small_cgroup.rb', line 9

def register_at_exit(pid: nil, name: nil, dirs: nil)
  at_exit do
    dirs.each do |dir|
      begin
        cleanup = fs_root.join(dir, name)
        FileUtils.rmdir(cleanup)
      rescue
        STDERR.puts "Failed to remove: #{cleanup}"
      end
    end
  end
end

Instance Method Details

#activate(dir) ⇒ Object



31
32
33
34
35
36
# File 'lib/haconiwa/small_cgroup.rb', line 31

def activate(dir)
  dirroot = root_of(dir)
  FileUtils.mkdir_p dirroot
  append_write dirroot.join("tasks"), self.pid
  @active_dirs << dir
end

#activated?(dir) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/haconiwa/small_cgroup.rb', line 38

def activated?(dir)
  @active_dirs.include? dir
end

#register(key, value) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/haconiwa/small_cgroup.rb', line 42

def register(key, value)
  dir = key.split('.').first
  unless activated?(dir)
    activate(dir)
  end

  overwrite root_of(dir).join(key), value
end