Sha256: 1637d77aef7aaf6b63a1fa63b80326b581cdb2396ee5d49a5d34382de65171df

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Polyphony
  # Process extensions
  module Process
    class << self

      # Watches a forked or spawned process, waiting for it to terminate. If
      # `cmd` is given it is spawned, otherwise the process is forked with the
      # given block.
      # 
      # If the operation is interrupted for any reason, the spawned or forked
      # process is killed.
      #
      # @param cmd [String, nil] command to spawn
      # @yield [] block to fork
      # @return [void]
      def watch(cmd = nil, &block)
        terminated = nil
        pid = cmd ? Kernel.spawn(cmd) : Polyphony.fork(&block)
        Polyphony.backend_waitpid(pid)
        terminated = true
      ensure
        kill_process(pid) unless terminated || pid.nil?
      end

      # Kills the given pid, waiting for it to terminate, with a timeout of 5
      # seconds.
      #
      # @param pid [Integer] pid
      # @return [void]
      def kill_process(pid)
        cancel_after(5) do
          kill_and_await('TERM', pid)
        end
      rescue Polyphony::Cancel
        kill_and_await(-9, pid)
      end

      private

      # Kills the given process with given signal, waiting for it to terminate.
      #
      # @param sig [String, Symbol, Integer] signal to use
      # @param pid [Integer] pid
      # @return [void]
      def kill_and_await(sig, pid)
        ::Process.kill(sig, pid)
        Polyphony.backend_waitpid(pid)
      rescue Errno::ESRCH
        # process doesn't exist
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polyphony-0.99.4 lib/polyphony/adapters/process.rb
polyphony-0.99.3 lib/polyphony/adapters/process.rb
polyphony-0.99.2 lib/polyphony/adapters/process.rb
polyphony-0.99.1 lib/polyphony/adapters/process.rb