Class: OsCtld::Monitor::Master

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, Utils::SwitchUser
Defined in:
lib/osctld/monitor/master.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_control, #ct_exec, #ct_runscript, #ct_syscmd, #init_script, #unlink_file

Class Method Details

.instanceObject



14
15
16
17
# File 'lib/osctld/monitor/master.rb', line 14

def instance
  @@instance = new unless @@instance
  @@instance
end

Instance Method Details

#demonitor(ct) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osctld/monitor/master.rb', line 50

def demonitor(ct)
  stop_entry = sync do
    k = key(ct)

    entry = @monitors[k]
    entry.cts.delete(ct.id)

    if entry.cts.empty?
      @monitors.delete(k)
      entry
    else
      false
    end
  end

  graceful_stop(stop_entry, ct) if stop_entry
  true
end

#monitor(ct) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/osctld/monitor/master.rb', line 33

def monitor(ct)
  sync do
    k = key(ct)

    if @monitors.has_key?(k)
      @monitors[k].cts << ct.id
      update_state(ct)
      next
    end

    t = Thread.new { handle_monitor(ct) }
    @monitors[k] = Entry.new(t, nil, [])
  end

  true
end

#stopObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctld/monitor/master.rb', line 69

def stop
  tmp = nil

  sync do
    tmp = @monitors.clone
    @monitors.clear
  end

  tmp.each_value do |entry|
    Process.kill('TERM', entry.pid) if entry.pid
  end

  tmp.each_value do |entry|
    entry.thread.join
  end
end