class Puppet::Util::Lockfile
This class provides a simple API for managing a lock file whose contents are an (optional) String. In addition to querying the basic state (locked?) of the lock, managing the lock (lock, unlock), the contents can be retrieved at any time while the lock is held (lock_data). This can be used to store pids, messages, etc.
Attributes
file_path[R]
Public Class Methods
new(file_path)
click to toggle source
# File lib/puppet/util/lockfile.rb 13 def initialize(file_path) 14 @file_path = file_path 15 end
Public Instance Methods
lock(lock_data = nil)
click to toggle source
@return [boolean] true if lock is successfully acquired, false otherwise.
# File lib/puppet/util/lockfile.rb 26 def lock(lock_data = nil) 27 begin 28 Puppet::FileSystem.exclusive_create(@file_path, nil) do |fd| 29 fd.print(lock_data) 30 end 31 true 32 rescue Errno::EEXIST 33 false 34 end 35 end
lock_data()
click to toggle source
Retrieve the (optional) lock data that was specified at the time the file
was locked.
@return [String] the data object.
# File lib/puppet/util/lockfile.rb 54 def lock_data 55 return File.read(@file_path) if file_locked? 56 end
locked?()
click to toggle source
# File lib/puppet/util/lockfile.rb 46 def locked? 47 # delegate logic to a more explicit private method 48 file_locked? 49 end
unlock()
click to toggle source
# File lib/puppet/util/lockfile.rb 37 def unlock 38 if locked? 39 Puppet::FileSystem.unlink(@file_path) 40 true 41 else 42 false 43 end 44 end
Private Instance Methods
file_locked?()
click to toggle source
Private, internal utility method for encapsulating the logic about
whether or not the file is locked. This method can be called by other methods in this class without as much risk of accidentally being overridden by child classes.
@return [boolean] true if the file is locked, false if it is not.
# File lib/puppet/util/lockfile.rb 63 def file_locked? 64 Puppet::FileSystem.exist? @file_path 65 end