class Puppet::Util::JsonLockfile
This class provides a simple API for managing a lock file whose contents are a serialized JSON object. 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 structured data (state messages, etc.) about the lock.
Public Instance Methods
Lock the lockfile. You may optionally pass a data object, which will be retrievable for the duration of time during which the file is locked.
@param [Hash] lock_data
an optional Hash of data to associate with the lock.
This may be used to store pids, descriptive messages, etc. The data may be retrieved at any time while the lock is held by calling the #lock_data method. <b>NOTE</b> that the JSON serialization does NOT support Symbol objects--if you pass them in, they will be serialized as Strings, so you should plan accordingly.
@return [boolean] true if lock is successfully acquired, false otherwise.
Puppet::Util::Lockfile#lock
# File lib/puppet/util/json_lockfile.rb 23 def lock(lock_data = nil) 24 return false if locked? 25 26 super(Puppet::Util::Json.dump(lock_data)) 27 end
Retrieve the (optional) lock data that was specified at the time the file
was locked.
@return [Object] the data object. Remember that the serialization does not
support Symbol objects, so if your data Object originally contained symbols, they will be converted to Strings.
Puppet::Util::Lockfile#lock_data
# File lib/puppet/util/json_lockfile.rb 34 def lock_data 35 return nil unless file_locked? 36 file_contents = super 37 return nil if file_contents.nil? or file_contents.empty? 38 Puppet::Util::Json.load(file_contents) 39 rescue Puppet::Util::Json::ParseError 40 Puppet.warning _("Unable to read lockfile data from %{path}: not in JSON") % { path: @file_path } 41 nil 42 end