Sha256: ec8da3ecbd071afdfc2d9ddb6f7efc0014919a350ee413bc20b73634977c0af0

Contents?: true

Size: 1.39 KB

Versions: 112

Compression:

Stored size: 1.39 KB

Contents

class BackgroundJob
  attr_reader :stdin, :stdout, :stderr, :pid
  def initialize(pid, stdin, stdout, stderr)
    @pid, @stdin, @stdout, @stderr = pid, stdin, stdout, stderr
    ObjectSpace.define_finalizer(self) { kill }
  end

  def self.run(command)
    command = sanitize_params(command) if command.is_a?(Array)
    child_stdin, parent_stdin = IO::pipe
    parent_stdout, child_stdout = IO::pipe
    parent_stderr, child_stderr = IO::pipe

    pid = Kernel.fork do
      [parent_stdin, parent_stdout, parent_stderr].each { |io| io.close }

      STDIN.reopen(child_stdin)
      STDOUT.reopen(child_stdout)
      STDERR.reopen(child_stderr)

      [child_stdin, child_stdout, child_stderr].each { |io| io.close }

      exec command
    end

    [child_stdin, child_stdout, child_stderr].each { |io| io.close }
    parent_stdin.sync = true

    new(pid, parent_stdin, parent_stdout, parent_stderr)
  end

  def self.sanitize_params(params)
    params.map { |p| p.gsub(' ', '\ ') }.join(" ")
  end

  def kill(signal = 'TERM')
    if running?
      Process.kill(Signal.list[signal], @pid)
      true
    end
  end

  def interrupt
    kill('INT')
  end

  def running?
    return false unless @pid
    Process.getpgid(@pid)
    true
  rescue Errno::ESRCH
    false
  end

  def wait(timeout = 1000)
    Timeout.timeout(timeout) do
      Process.wait(@pid)
    end
    true
  rescue Timeout::Error
    false
  end
end

Version data entries

112 entries across 112 versions & 9 rubygems

Version Path
classiccms-0.6.1 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.0 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.17 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.16 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.15 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.14 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.13 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.12 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.11 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.10 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.9 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.8 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.7 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.6 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.5 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.2 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.1 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.5.0 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.4.2 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.4.1 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb