Class: LockFile

Inherits:
Object
  • Object
show all
Defined in:
lib/vcseif/utils/lock_file.rb

Class Method Summary collapse

Class Method Details

.create_lock(filename) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vcseif/utils/lock_file.rb', line 63

def self.create_lock(filename)
  # remove any existing lock
  self.destroy_lock(filename)

  # and write the lock file with the PID of this process and a current timestamp
  begin
    info = "%d %s\n" % [$$, Time.now.utc.strftime("%Y-%m-%d %H:%M:%S Z")]
    lf = File.new(filename, "w")
    lf.write("%s\n" % info)
    lf.close
  rescue => ex
    lockex = VCSEIF_Exceptions::UnrecoverableException.new("#{ex.message}")
    raise lockex, ex.message.to_s
  end
end

.current_lock_holder(filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vcseif/utils/lock_file.rb', line 37

def self.current_lock_holder(filename)
  holder = ""
  if !File.exists?(filename)
      raise VCSEIF_Exceptions::UnrecoverableException.new("lock file %s does not exists" % filename)
  end
  begin
    lf = File.open(filename, "r")
    entry = lf.read()
    if !entry.nil? and entry.length > 0 and entry.strip().length > 0
      entry = entry.strip()
      chunks = entry.split(' ')
      if (not chunks.empty?) and (chunks.length == 4)
         holder = chunks.first
      end
    end
    lf.close
  rescue => ex
    lockex = VCSEIF_Exceptions::UnrecoverableException.new("#{ex.message}")
    raise lockex, ex.message.to_s
  end
  if holder.empty?
      raise VCSEIF_Exceptions::UnrecoverableException.new("lock file is empty or contains non-standard content")
  end
  return holder
end

.destroy_lock(filename) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/vcseif/utils/lock_file.rb', line 80

def self.destroy_lock(filename)
  # remove the existing lock file 
  begin
    File.delete(filename) if File.exists?(filename)
  rescue => ex
    lockex = VCSEIF_Exceptions::UnrecoverableException.new("#{ex.message}")
    raise lockex, ex.message.to_s
  end
end

.exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vcseif/utils/lock_file.rb', line 10

def self.exists?(filename)
  begin
    if File.exists?(filename) and File.readable?(filename)
      return true
    else
      return false
    end
  rescue => ex
    lockex = VCSEIF_Exceptions::UnrecoverableException.new("#{ex.message}")
    raise lockex, ex.message.to_s
  end
end

.locker_is_running?(filename, locker) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vcseif/utils/lock_file.rb', line 23

def self.locker_is_running?(filename, locker)
  # return false if no lock file exists OR the PID that wrote the lock file is no longer running
  return false if !File.exists?(filename)
  holder = self.current_lock_holder(filename)
  return false if holder == "" or holder.nil?
  locker_pid = 0
  started    = ""
  holder.scan(/^(\d+)/) {|p| locker_pid = p[0]}
  running = ProcTable.target_process(locker_pid)

  return false if running.nil?
  return true
end