summaryrefslogtreecommitdiffstats
path: root/lib/puppet/agent/locker.rb
blob: 98f5b38d93e0e0992c691fd34e6bb26dc7bdfb84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'puppet/util/pidlock'

# Break out the code related to locking the agent.  This module is just
# included into the agent, but having it here makes it easier to test.
module Puppet::Agent::Locker
  # Let the daemon run again, freely in the filesystem.
  def enable
    lockfile.unlock(:anonymous => true)
  end

  # Stop the daemon from making any catalog runs.
  def disable
    lockfile.lock(:anonymous => true)
  end

  # Yield if we get a lock, else do nothing.  Return
  # true/false depending on whether we get the lock.
  def lock
    if lockfile.lock
      begin
        yield
      ensure
        lockfile.unlock
      end
      return true
    else
      return false
    end
  end

  def lockfile
    @lockfile ||= Puppet::Util::Pidlock.new(lockfile_path)

    @lockfile
  end

  def running?
    lockfile.locked?
  end
end