Sha256: e0a3bfe7ba103aac079d1b6a224d7f2c42eb60a14c1d7358e2a292d1dddcfaf7

Contents?: true

Size: 1.65 KB

Versions: 42

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

42 entries across 30 versions & 7 rubygems

Version Path
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/childprocess-4.1.0/lib/childprocess/unix/process.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
childprocess-4.1.0 lib/childprocess/unix/process.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/childprocess-3.0.0/lib/childprocess/unix/process.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/childprocess-4.0.0/lib/childprocess/unix/process.rb