Sha256: 6da5471040f8bcc4a5a43566decb037fe44fcb28e3abb2430c51cd7a23ca5cc2

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module ChildProcess
  class AbstractProcess
    attr_reader :exit_code

    def initialize(args)
      @args      = args
      @started   = false
      @exit_code = nil
    end

    #
    # Launch the child process
    #
    # @return [AbstractProcess] self
    #

    def start
      launch_process
      @started = true

      self
    end

    #
    # Forcibly terminate the process, using increasingly harsher methods if possible.
    #
    # @param [Fixnum] timeout (3) Seconds to wait before trying the next method.
    #

    def stop(timeout = 3)
      raise SubclassResponsibility, "stop"
    end

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

    def exited?
      raise SubclassResponsibility, "exited?"
    end

    #
    # Is this process running?
    #

    def alive?
      started? && !exited?
    end
    
    def crashed?
      exited? && @exit_code != 0
    end

    def poll_for_exit(timeout)
      log "polling #{timeout} seconds for exit"

      end_time = Time.now + timeout
      until (ok = exited?) || Time.now > end_time
        sleep POLL_INTERVAL
      end

      unless ok
        raise TimeoutError, "process still alive after #{timeout} seconds"
      end
    end

    private

    def launch_process
      raise SubclassResponsibility, "launch_process"
    end

    POLL_INTERVAL = 0.1

    def started?
      @started
    end

    def log(*args)
      $stderr.puts "#{self.inspect} : #{args.inspect}" if $DEBUG
    end

    def assert_started
      raise Error, "process not started" unless started?
    end

  end # AbstractProcess
end # ChildProcess

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
childprocess-0.0.7 lib/childprocess/abstract_process.rb
childprocess-0.0.6 lib/childprocess/abstract_process.rb
childprocess-0.0.5 lib/childprocess/abstract_process.rb
childprocess-0.0.4 lib/childprocess/abstract_process.rb
childprocess-0.0.3 lib/childprocess/abstract_process.rb