Class: ProcessManager

Inherits:
Object show all
Defined in:
lib/crazy_ivan/process_manager.rb

Constant Summary collapse

@@pidfile =
'/tmp/crazy_ivan.pid'

Class Method Summary collapse

Class Method Details

.acquire_lockObject



8
9
10
11
12
# File 'lib/crazy_ivan/process_manager.rb', line 8

def self.acquire_lock
  lock_exclusively!
  yield
  unlock
end

.ci_already_running?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/crazy_ivan/process_manager.rb', line 18

def self.ci_already_running?
  File.exists?(@@pidfile) && !File.new(@@pidfile).flock(File::LOCK_EX | File::LOCK_NB)
end

.lock_exclusively!(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crazy_ivan/process_manager.rb', line 22

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} - 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

.pidfile=(file) ⇒ Object



4
5
6
# File 'lib/crazy_ivan/process_manager.rb', line 4

def self.pidfile=(file)
  @@pidfile = file
end

.unlockObject



14
15
16
# File 'lib/crazy_ivan/process_manager.rb', line 14

def self.unlock
  File.new(@@pidfile).flock(File::LOCK_UN)
end