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 12 def initialize(file_path) 13 @file_path = file_path 14 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 25 def lock(lock_data = nil) 26 begin 27 Puppet::FileSystem.exclusive_create(@file_path, nil) do |fd| 28 fd.print(lock_data) 29 end 30 true 31 rescue Errno::EEXIST 32 false 33 end 34 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 53 def lock_data 54 return File.read(@file_path) if file_locked? 55 end
locked?()
click to toggle source
# File lib/puppet/util/lockfile.rb 45 def locked? 46 # delegate logic to a more explicit private method 47 file_locked? 48 end
unlock()
click to toggle source
# File lib/puppet/util/lockfile.rb 36 def unlock 37 if locked? 38 Puppet::FileSystem.unlink(@file_path) 39 true 40 else 41 false 42 end 43 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 62 def file_locked? 63 Puppet::FileSystem.exist? @file_path 64 end