Sha256: 6c158350d5317f2b83742b1332dbfb27960753417468c303db9793f1f0d78a5a

Contents?: true

Size: 583 Bytes

Versions: 2

Compression:

Stored size: 583 Bytes

Contents

# frozen_string_literal: true

export :watch

def watch(cmd = nil, &block)
  terminated = nil
  pid = cmd ? Kernel.spawn(cmd) : Polyphony.fork(&block)
  watcher = Gyro::Child.new(pid)
  watcher.await
  terminated = true
ensure
  kill_process(pid) unless terminated || pid.nil?
end

def kill_process(pid)
  cancel_after(5) do
    kill_and_await('TERM', pid)
  end
rescue Polyphony::Cancel
  kill_and_await(-9, pid)
end

def kill_and_await(sig, pid)
  Process.kill(sig, pid)
  Gyro::Child.new(pid).await
rescue SystemCallError
  # ignore
  puts 'SystemCallError in kill_and_await'
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polyphony-0.36 lib/polyphony/adapters/process.rb
polyphony-0.34 lib/polyphony/adapters/process.rb