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
panjiva-spork-0.8.6 features/support/background_job.rb
panjiva-spork-0.8.5.1 features/support/background_job.rb
panjiva-spork-0.8.5 features/support/background_job.rb
classiccms-0.7.5 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.7.4 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.7.3 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
spork-1.0.0rc4 features/support/background_job.rb
spork-1.0.0rc4-x86-mswin32 features/support/background_job.rb
spork-1.0.0rc4-x86-mingw32 features/support/background_job.rb
classiccms-0.7.2 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.7.1 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.7.0 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.9 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.8 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.7 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.6 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.5 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.4 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.3 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb
classiccms-0.6.2 vendor/bundle/gems/spork-0.9.0/features/support/background_job.rb