Class: Puppet::Util::Pidlock

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/util/pidlock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile) ⇒ Pidlock

Returns a new instance of Pidlock.



6
7
8
# File 'lib/vendor/puppet/util/pidlock.rb', line 6

def initialize(lockfile)
  @lockfile = lockfile
end

Instance Attribute Details

#lockfileObject (readonly)

Returns the value of attribute lockfile.



4
5
6
# File 'lib/vendor/puppet/util/pidlock.rb', line 4

def lockfile
  @lockfile
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/vendor/puppet/util/pidlock.rb', line 25

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

#lock(opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vendor/puppet/util/pidlock.rb', line 30

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

#locked?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/vendor/puppet/util/pidlock.rb', line 10

def locked?
  clear_if_stale
  return true if File.exists? @lockfile

  # HACK!  There was a temporary change to the lockfile behavior introduced in 2.7.10 and 2.7.11, and reverted
  # in 2.7.12.  We need to pull some chicanery to be backwards-compatible with those versions.  For more info,
  # see the comments on the method... and this hack should be removed for the 3.x series.
  handle_2_7_10_disabled_lockfile
  File.exists? @lockfile
end

#mine?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/vendor/puppet/util/pidlock.rb', line 21

def mine?
  Process.pid == lock_pid
end

#unlock(opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vendor/puppet/util/pidlock.rb', line 45

def unlock(opts = {})
  return false unless locked?

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

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