Class: Puppet::Util::Pidlock

Inherits:
Object
  • Object
show all
Defined in:
lib/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/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/puppet/util/pidlock.rb', line 4

def lockfile
  @lockfile
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/puppet/util/pidlock.rb', line 19

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

#lock(opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet/util/pidlock.rb', line 24

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
# File 'lib/puppet/util/pidlock.rb', line 10

def locked?
  clear_if_stale
  File.exists? @lockfile
end

#mine?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/puppet/util/pidlock.rb', line 15

def mine?
  Process.pid == lock_pid
end

#unlock(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/util/pidlock.rb', line 39

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

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