Sha256: 2a32edfaa218352c7f905d60eaa6f652f9ab9b3ad8fa98b7bb23121dcc9b3d59

Contents?: true

Size: 790 Bytes

Versions: 3

Compression:

Stored size: 790 Bytes

Contents

# frozen_string_literal: true

module Polyphony
  # Process patches
  module Process
    class << self
      def watch(cmd = nil, &block)
        terminated = nil
        pid = cmd ? Kernel.spawn(cmd) : Polyphony.fork(&block)
        Thread.current.backend.waitpid(pid)
        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)
        Thread.current.backend.waitpid(pid)
      rescue SystemCallError
        # ignore
        puts 'SystemCallError in kill_and_await'
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
polyphony-0.45.2 lib/polyphony/adapters/process.rb
polyphony-0.45.1 lib/polyphony/adapters/process.rb
polyphony-0.45.0 lib/polyphony/adapters/process.rb