Sha256: f43374c411abb3428735fa8a298c3f4c1433bcd139ebb8905188172937e06af9

Contents?: true

Size: 1.97 KB

Versions: 20

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

# 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.
#
# @see Puppet::Util::JsonLockfile
class Puppet::Util::Lockfile
  attr_reader :file_path

  def initialize(file_path)
    @file_path = file_path
  end

  # 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 [String] lock_data an optional String data object 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.

  # @return [boolean] true if lock is successfully acquired, false otherwise.
  def lock(lock_data = nil)
    Puppet::FileSystem.exclusive_create(@file_path, nil) do |fd|
      fd.print(lock_data)
    end
    true
  rescue Errno::EEXIST
    false
  end

  def unlock
    if locked?
      Puppet::FileSystem.unlink(@file_path)
      true
    else
      false
    end
  end

  def locked?
    # delegate logic to a more explicit private method
    file_locked?
  end

  # Retrieve the (optional) lock data that was specified at the time the file
  #  was locked.
  # @return [String] the data object.
  def lock_data
    File.read(@file_path) if file_locked?
  end

  # 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.
  def file_locked?
    Puppet::FileSystem.exist? @file_path
  end
  private :file_locked?
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/util/lockfile.rb
puppet-8.10.0-x86-mingw32 lib/puppet/util/lockfile.rb
puppet-8.10.0-x64-mingw32 lib/puppet/util/lockfile.rb
puppet-8.10.0-universal-darwin lib/puppet/util/lockfile.rb
puppet-8.9.0 lib/puppet/util/lockfile.rb
puppet-8.9.0-x86-mingw32 lib/puppet/util/lockfile.rb
puppet-8.9.0-x64-mingw32 lib/puppet/util/lockfile.rb
puppet-8.9.0-universal-darwin lib/puppet/util/lockfile.rb
puppet-8.8.1 lib/puppet/util/lockfile.rb
puppet-8.8.1-x86-mingw32 lib/puppet/util/lockfile.rb
puppet-8.8.1-x64-mingw32 lib/puppet/util/lockfile.rb
puppet-8.8.1-universal-darwin lib/puppet/util/lockfile.rb
puppet-8.7.0 lib/puppet/util/lockfile.rb
puppet-8.7.0-x86-mingw32 lib/puppet/util/lockfile.rb
puppet-8.7.0-x64-mingw32 lib/puppet/util/lockfile.rb
puppet-8.7.0-universal-darwin lib/puppet/util/lockfile.rb
puppet-8.6.0 lib/puppet/util/lockfile.rb
puppet-8.6.0-x86-mingw32 lib/puppet/util/lockfile.rb
puppet-8.6.0-x64-mingw32 lib/puppet/util/lockfile.rb
puppet-8.6.0-universal-darwin lib/puppet/util/lockfile.rb