Sha256: 9423ba964f767081c2d3cfee417e3079f5c37db337d1209ab41d19fabef0647b

Contents?: true

Size: 1.65 KB

Versions: 21

Compression:

Stored size: 1.65 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 | ::Process::WUNTRACED)
        pid = nil if pid == 0 # may happen on jruby

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

        if pid
          set_exit_code(status)
        end

        !!pid
      rescue Errno::ECHILD
        # may be thrown for detached processes
        true
      end

      def wait
        assert_started

        if exited?
          exit_code
        else
          _, status = ::Process.waitpid2 _pid
          set_exit_code(status)
        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

      def set_exit_code(status)
        @exit_code = status.exitstatus || status.termsig
      end

      def _pid
        if leader?
          -@pid # negative pid == process group
        else
          @pid
        end
      end

    end # Process
  end # Unix
end # ChildProcess

Version data entries

21 entries across 20 versions & 6 rubygems

Version Path
childprocess-1.0.1 lib/childprocess/unix/process.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
tdiary-5.0.2 vendor/bundle/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
tdiary-5.0.1 vendor/bundle/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/childprocess-0.5.6/lib/childprocess/unix/process.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/childprocess-0.5.6/lib/childprocess/unix/process.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/childprocess-0.5.9/lib/childprocess/unix/process.rb
childprocess-0.5.9 lib/childprocess/unix/process.rb
childprocess-0.5.8 lib/childprocess/unix/process.rb
childprocess-0.5.7 lib/childprocess/unix/process.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/childprocess-0.5.6/lib/childprocess/unix/process.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/childprocess-0.5.6/lib/childprocess/unix/process.rb