Sha256: eaa67caf0b53c30bed0a661fd9c85c33b2cccb1b6377cbf39ae8de033e47da2e

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class ProcessManager
  @@pidfile = '/tmp/crazy_ivan.pid'
  
  def self.pidfile=(file)
    @@pidfile = file
  end
  
  def self.acquire_lock
    lock_exclusively!
    yield
    unlock
  end
  
  def self.unlock
    File.new(@@pidfile).flock(File::LOCK_UN)
  end

  def self.ci_already_running?
    File.exists?(@@pidfile) && !File.new(@@pidfile).flock(File::LOCK_EX | File::LOCK_NB)
  end
  
  def self.lock_exclusively!(options = {})
    pid = Integer(File.read(@@pidfile)) if File.exists?(@@pidfile)
    
    Syslog.debug "Acquiring lock"
    
    if options[:interrupt_existing_process]
      File.open(@@pidfile, "w+") { |fp| fp << Process.pid }

      if ci_already_running?
        Process.kill("INT", pid)
        Syslog.debug("Detected another running CI process #{pid}; interrupting it and starting myself")
        File.new(@@pidfile).flock(File::LOCK_EX)
      end
    else
      if ci_already_running?
        msg = "Detected another running CI process #{pid} - interrupting and terminating myself"
        Syslog.warning msg
        puts msg
        Process.kill("INT", 0)
      else
        Syslog.debug("Locked CI process pid file")
        Syslog.debug("Writing to pid file with #{Process.pid}")
        File.open(@@pidfile, "w+") { |fp| fp << Process.pid }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crazy_ivan-1.2.3 lib/crazy_ivan/process_manager.rb