Sha256: 6f293970b7b180b6c220a06f7352743788972367a60672241c1343f3fc28ba26

Contents?: true

Size: 1.96 KB

Versions: 89

Compression:

Stored size: 1.96 KB

Contents

module Consul
  module Async
    class ProcessDoesNotExist < StandardError
    end
    class ProcessHandler
      attr_reader :command, :sig_reload, :sig_term, :pid, :exit_status
      def initialize(command, sig_reload: 'HUP', sig_term: 'TERM')
        raise 'empty sig_term is not supported' unless sig_term
        @command = command
        @sig_reload = sig_reload
        @sig_term = sig_term
        @pid = nil
        @exit_status = nil
      end

      def start
        return pid unless pid.nil?
        @pid = Process.spawn(command)
      end

      def reload
        return if sig_reload.nil?
        STDERR.puts "Sending SIG #{sig_reload} to #{pid}..."
        begin
          Process.kill(sig_reload, pid)
        rescue Errno::ESRCH => e
          STDERR.puts "*** Process #{pid} has already been killed: #{e.inspect}"
          raise e
        end
      end

      def kill
        return exit_status if pid.nil?
        the_pid = pid
        @pid = nil
        STDERR.puts "[KILL] Sending SIG #{sig_term} to #{the_pid}..."
        begin
          STDERR.puts "[KILL] waiting for #{the_pid}..."
          Process.kill(sig_term, the_pid)
        rescue Errno::ESRCH
          STDERR.puts "[KILL] *** Process #{the_pid} has already been killed"
        end
        begin
          _pid, @exit_status = Process.waitpid2 the_pid
        rescue SystemCallError
          STDERR.puts "[KILL] *** UNEXPECTED ERROR *** Failed to get return code for #{the_pid}"
        end
        exit_status
      end

      def process_status
        raise ProcessDoesNotExist, 'No child process' if pid.nil?
        begin
          cpid, result = Process.waitpid2(pid, Process::WNOHANG)
          raise ProcessDoesNotExist, "Unexpected PID: #{cpid}, was expecting #{pid}" unless cpid.nil? || cpid == pid
          result
        rescue Errno::ECHILD => e
          e2 = ProcessDoesNotExist.new e
          raise e2, "ChildProcess has been killed: #{e.message}", e.backtrace
        end
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
consul-templaterb-1.23.0 lib/consul/async/process_handler.rb
consul-templaterb-1.22.0 lib/consul/async/process_handler.rb
consul-templaterb-1.21.8 lib/consul/async/process_handler.rb
consul-templaterb-1.21.7 lib/consul/async/process_handler.rb
consul-templaterb-1.21.6 lib/consul/async/process_handler.rb
consul-templaterb-1.21.5 lib/consul/async/process_handler.rb
consul-templaterb-1.21.4 lib/consul/async/process_handler.rb
consul-templaterb-1.21.3 lib/consul/async/process_handler.rb
consul-templaterb-1.21.2 lib/consul/async/process_handler.rb
consul-templaterb-1.21.1 lib/consul/async/process_handler.rb
consul-templaterb-1.21.0 lib/consul/async/process_handler.rb
consul-templaterb-1.20.0 lib/consul/async/process_handler.rb
consul-templaterb-1.19.0 lib/consul/async/process_handler.rb
consul-templaterb-1.18.5 lib/consul/async/process_handler.rb
consul-templaterb-1.18.4 lib/consul/async/process_handler.rb
consul-templaterb-1.18.3 lib/consul/async/process_handler.rb
consul-templaterb-1.18.2 lib/consul/async/process_handler.rb
consul-templaterb-1.18.1 lib/consul/async/process_handler.rb
consul-templaterb-1.18.0 lib/consul/async/process_handler.rb
consul-templaterb-1.17.4 lib/consul/async/process_handler.rb