Sha256: 5270eceb094485069373789af5c6a73c83f6dc559d74a9b8414d7e3433d69dff

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module DeepTest
  class Warlock
    def initialize
      @demons = []
    end
    
    def start(name, &block)
      begin
        pid = Process.fork do
          Signal.trap("HUP") { exit 0 }
          yield
          exit
        end
        raise "fatal: fork returned nil" if pid.nil?
        @demons << [name, pid]
        DeepTest.logger.debug "Started #{name} (#{pid})"
      rescue => e
        puts "exception starting #{name}: #{e}"
        puts "\t" + e.backtrace.join("\n\t")
      end
    end

    def stop_all
      @demons.reverse.each do |demon|
        name, pid = demon
        if running?(pid)
          Process.kill("HUP", pid)
        end
      end
      @demons.each do |demon|
        name, pid = demon
        begin
          Process.wait(pid)
        rescue Errno::ECHILD => e
          puts e
        end
        DeepTest.logger.debug "Stopped #{name} (#{pid})"
      end
    end

    #stolen from daemons
    def running?(pid)
      # Check if process is in existence
      # The simplest way to do this is to send signal '0'
      # (which is a single system call) that doesn't actually
      # send a signal
      begin
        Process.kill(0, pid)
        return true
      rescue Errno::ESRCH
        return false
      rescue ::Exception   # for example on EPERM (process exists but does not belong to us)
        return true
      #rescue Errno::EPERM
      #  return false
      end
    end    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deep_test-1.1.3 lib/deep_test/warlock.rb
deep_test-1.1.4 lib/deep_test/warlock.rb