Sha256: a92f256baf3e0e137d0da250ec2df62b0f502b74d17c1f32b5023897f5e99e13

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

module ChildProcess
  module Unix
    class Process < AbstractProcess
      #
      # @return [Fixnum] the pid of the process after it has started
      #
      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

      #
      # Did the process exit?
      #
      # @return [Boolean]
      #

      def exited?
        return true if @exit_code

        assert_started
        pid, status = ::Process.waitpid2(@pid, ::Process::WNOHANG)

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

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

        !!pid
      end

      private

      def wait
        @exit_code = ::Process.waitpid @pid
      end

      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 launch_process
        if @io
          stdout = @io.stdout
          stderr = @io.stderr
        end

        if duplex?
          reader, writer = ::IO.pipe
        end

        @pid = fork {
          STDOUT.reopen(stdout || "/dev/null")
          STDERR.reopen(stderr || "/dev/null")

          if duplex?
            STDIN.reopen(reader)
            writer.close
          end

          exec(*@args)
        }

        if duplex?
          io._stdin = writer
          reader.close
        end

        ::Process.detach(@pid) if detach?
      end

    end # Process
  end # Unix
end # ChildProcess

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/childprocess-0.1.7/lib/childprocess/unix/process.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/childprocess-0.1.7/lib/childprocess/unix/process.rb
childprocess-0.2.2 lib/childprocess/unix/process.rb
childprocess-0.2.1 lib/childprocess/unix/process.rb
childprocess-0.2.0 lib/childprocess/unix/process.rb
childprocess-0.1.9 lib/childprocess/unix/process.rb
childprocess-0.1.8 lib/childprocess/unix/process.rb
childprocess-0.1.8.pre lib/childprocess/unix/process.rb
childprocess-0.1.7 lib/childprocess/unix/process.rb