Sha256: 2922864b356fec2ba71e3a5ec8e0ac1e00a409d7d1b9a44a54f2e35d592b99da

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 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?
      @exit_code && @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

1 entries across 1 versions & 1 rubygems

Version Path
childprocess-0.0.2 lib/childprocess/abstract_process.rb