Sha256: c163defcb7c522f291f2ead39f2cb0d9bce0c49cab9d0a7b54070820915d0bae

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module ChildProcess
  module Unix
    class Process < AbstractProcess
      attr_reader :pid

      def io
        @io ||= Unix::IO.new
      end

      def stop(timeout = 3)
        assert_started
        send_term

        begin
          return poll_for_exit(timeout)
        rescue TimeoutError
          # try next
        end

        send_kill
        wait
      rescue Errno::ECHILD, Errno::ESRCH
        # handle race condition where process dies between timeout
        # and send_kill
        true
      end

      def exited?
        return true if @exit_code

        assert_started
        pid, status = ::Process.waitpid2(@pid, ::Process::WNOHANG)
        pid = nil if pid == 0 # may happen on jruby

        log(:pid => pid, :status => status)

        if pid
          @exit_code = status.exitstatus || status.termsig
        end

        !!pid
      end

      def wait
        assert_started

        if exited?
          exit_code
        else
          _, status = ::Process.waitpid2 @pid
          @exit_code = status.exitstatus || status.termsig
        end
      end

      private

      def send_term
        send_signal 'TERM'
      end

      def send_kill
        send_signal 'KILL'
      end

      def send_signal(sig)
        assert_started

        log "sending #{sig}"
        ::Process.kill sig, -@pid
      end

    end # Process
  end # Unix
end # ChildProcess

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
childprocess-0.4.1 lib/childprocess/unix/process.rb
childprocess-0.4.1.rc3 lib/childprocess/unix/process.rb
childprocess-0.4.1.rc2 lib/childprocess/unix/process.rb
childprocess-0.4.1.rc1 lib/childprocess/unix/process.rb