Class: Puppet::Util::JsonLockfile
- Defined in:
- lib/puppet/util/json_lockfile.rb
Overview
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.
Instance Attribute Summary
Attributes inherited from Lockfile
Instance Method Summary collapse
-
#lock(lock_data = nil) ⇒ boolean
Lock the lockfile.
-
#lock_data ⇒ Object
Retrieve the (optional) lock data that was specified at the time the file was locked.
Methods inherited from Lockfile
#initialize, #locked?, #unlock
Constructor Details
This class inherits a constructor from Puppet::Util::Lockfile
Instance Method Details
#lock(lock_data = nil) ⇒ boolean
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.
23 24 25 26 27 |
# File 'lib/puppet/util/json_lockfile.rb', line 23 def lock(lock_data = nil) return false if locked? super(lock_data.to_json) end |
#lock_data ⇒ Object
Retrieve the (optional) lock data that was specified at the time the file
was locked.
34 35 36 37 38 39 40 41 42 |
# File 'lib/puppet/util/json_lockfile.rb', line 34 def lock_data return nil unless file_locked? file_contents = super return nil if file_contents.nil? or file_contents.empty? JSON.parse(file_contents) rescue JSON::ParserError => e Puppet.warning _("Unable to read lockfile data from %{path}: not in JSON") % { path: @file_path } nil end |