Sha256: 2709516d24c547077689f2da5dbb76c807867d5ff5a07188c1e3b91d66656200

Contents?: true

Size: 1.09 KB

Versions: 21

Compression:

Stored size: 1.09 KB

Contents

require 'fileutils'

class Puppet::Util::Pidlock
  attr_reader :lockfile

  def initialize(lockfile)
    @lockfile = lockfile
  end

  def locked?
    clear_if_stale
    File.exists? @lockfile
  end

  def mine?
    Process.pid == lock_pid
  end

  def anonymous?
    return false unless File.exists?(@lockfile)
    File.read(@lockfile) == ""
  end

  def lock(opts = {})
    opts = {:anonymous => false}.merge(opts)

    if locked?
      mine?
    else
      if opts[:anonymous]
        File.open(@lockfile, 'w') { |fd| true }
      else
        File.open(@lockfile, "w") { |fd| fd.write(Process.pid) }
      end
      true
    end
  end

  def unlock(opts = {})
    opts = {:anonymous => false}.merge(opts)

    if mine? or (opts[:anonymous] and anonymous?)
      File.unlink(@lockfile)
      true
    else
      false
    end
  end

  private
  def lock_pid
    if File.exists? @lockfile
      File.read(@lockfile).to_i
    else
      nil
    end
  end

  def clear_if_stale
    return if lock_pid.nil?

    begin
      Process.kill(0, lock_pid)
    rescue Errno::ESRCH
      File.unlink(@lockfile)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
puppet-2.6.18 lib/puppet/util/pidlock.rb
puppet-2.6.17 lib/puppet/util/pidlock.rb
puppet-2.6.16 lib/puppet/util/pidlock.rb
puppet-2.6.15 lib/puppet/util/pidlock.rb
puppet-2.6.14 lib/puppet/util/pidlock.rb
puppet-2.6.13 lib/puppet/util/pidlock.rb
puppet-2.6.12 lib/puppet/util/pidlock.rb
puppet-2.6.11 lib/puppet/util/pidlock.rb
puppet-2.6.10 lib/puppet/util/pidlock.rb
puppet-2.7.3 lib/puppet/util/pidlock.rb
puppet-2.7.1 lib/puppet/util/pidlock.rb
puppet-2.6.9 lib/puppet/util/pidlock.rb
puppet-2.6.8 lib/puppet/util/pidlock.rb
puppet-2.6.7 lib/puppet/util/pidlock.rb
puppet-2.6.6 lib/puppet/util/pidlock.rb
puppet-2.6.5 lib/puppet/util/pidlock.rb
puppet-2.6.4 lib/puppet/util/pidlock.rb
puppet-2.6.3 lib/puppet/util/pidlock.rb
puppet-2.6.2 lib/puppet/util/pidlock.rb
puppet-2.6.1 lib/puppet/util/pidlock.rb