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

: () -> String



41
42
43
# File 'lib/fusuma/custom_process.rb', line 41

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

Instance Method Details

#child_pidsObject

: () -> Array



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

def child_pids
  @child_pids ||= []
end

#forkObject

: () { () -> void } -> nil



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

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

#set_trapObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/fusuma/custom_process.rb', line 45

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

#shutdownObject



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

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

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