Class: Ocak::ProcessRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ocak/process_registry.rb

Constant Summary collapse

KILL_WAIT =
2

Instance Method Summary collapse

Constructor Details

#initializeProcessRegistry

Returns a new instance of ProcessRegistry.



7
8
9
10
# File 'lib/ocak/process_registry.rb', line 7

def initialize
  @pids = Set.new
  @mutex = Mutex.new
end

Instance Method Details

#kill_all(signal: :TERM, wait: KILL_WAIT) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ocak/process_registry.rb', line 24

def kill_all(signal: :TERM, wait: KILL_WAIT)
  snapshot = pids
  return if snapshot.empty?

  snapshot.each do |pid|
    Process.kill(signal, pid)
  rescue Errno::ESRCH, Errno::EPERM
    nil
  end

  sleep wait

  # SIGKILL any survivors; ESRCH means already exited, which is fine
  snapshot.each do |pid|
    Process.kill(:KILL, pid)
  rescue Errno::ESRCH, Errno::EPERM
    nil
  end
end

#pidsObject



20
21
22
# File 'lib/ocak/process_registry.rb', line 20

def pids
  @mutex.synchronize { @pids.dup }
end

#register(pid) ⇒ Object



12
13
14
# File 'lib/ocak/process_registry.rb', line 12

def register(pid)
  @mutex.synchronize { @pids.add(pid) }
end

#unregister(pid) ⇒ Object



16
17
18
# File 'lib/ocak/process_registry.rb', line 16

def unregister(pid)
  @mutex.synchronize { @pids.delete(pid) }
end