Sha256: 5289eafc9fc28e9974a0080f97fa3f12c3d6cf8b7f39fe80a50dd1729a5c7cef

Contents?: true

Size: 910 Bytes

Versions: 7

Compression:

Stored size: 910 Bytes

Contents

# frozen_string_literal: true

require_relative "string_support"

module Fusuma
  # Rename process
  module CustomProcess
    attr_writer :proctitle

    def child_pids
      @child_pids ||= []
    end

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

    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

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fusuma-3.7.0 lib/fusuma/custom_process.rb
fusuma-3.6.2 lib/fusuma/custom_process.rb
fusuma-3.6.1 lib/fusuma/custom_process.rb
fusuma-3.6.0 lib/fusuma/custom_process.rb
fusuma-3.5.0 lib/fusuma/custom_process.rb
fusuma-3.4.0 lib/fusuma/custom_process.rb
fusuma-3.3.1 lib/fusuma/custom_process.rb