Module: Fusuma::CustomProcess

Defined in:
lib/fusuma/custom_process.rb

Overview

Rename process

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#proctitleObject



38
39
40
# File 'lib/fusuma/custom_process.rb', line 38

def proctitle
  @proctitle ||= self.class.name.underscore
end

Instance Method Details

#child_pidsObject



10
11
12
# File 'lib/fusuma/custom_process.rb', line 10

def child_pids
  @child_pids ||= []
end

#forkObject



14
15
16
17
18
19
20
21
22
# File 'lib/fusuma/custom_process.rb', line 14

def fork
  pid = Process.fork do
    Process.setproctitle(proctitle)
    set_trap # for child process
    yield
  end
  child_pids << pid
  pid
end

#set_trapObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/fusuma/custom_process.rb', line 42

def set_trap
  Signal.trap("INT") {
    shutdown
    exit
  } # Trap ^C
  Signal.trap("TERM") {
    shutdown
    exit
  } # Trap `Kill `
end

#shutdownObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fusuma/custom_process.rb', line 24

def shutdown
  child_pids.each do |pid|
    Process.kill("TERM", pid)
  rescue Errno::ESRCH
    # ignore
  end

  child_pids.each do |pid|
    Process.wait(pid)
  rescue Errno::ECHILD
    # ignore
  end
end