Class: Puppet::Agent

Inherits:
Object show all
Includes:
Locker
Defined in:
lib/vendor/puppet/agent.rb

Overview

A general class for triggering a run of another class.

Defined Under Namespace

Modules: Locker

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Locker

#disable, #disabled?, #enable, #lock, #lockfile, #running?

Constructor Details

#initialize(client_class) ⇒ Agent

Just so we can specify that we are “the” instance.



13
14
15
16
17
# File 'lib/vendor/puppet/agent.rb', line 13

def initialize(client_class)
  @splayed = false

  @client_class = client_class
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/vendor/puppet/agent.rb', line 10

def client
  @client
end

#client_classObject (readonly)

Returns the value of attribute client_class.



10
11
12
# File 'lib/vendor/puppet/agent.rb', line 10

def client_class
  @client_class
end

#splayedObject (readonly)

Returns the value of attribute splayed.



10
11
12
# File 'lib/vendor/puppet/agent.rb', line 10

def splayed
  @splayed
end

Instance Method Details

#lockfile_pathObject



19
20
21
# File 'lib/vendor/puppet/agent.rb', line 19

def lockfile_path
  client_class.lockfile_path
end

#needing_restart?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/vendor/puppet/agent.rb', line 23

def needing_restart?
  Puppet::Application.restart_requested?
end

#run(*args) ⇒ Object

Perform a run with our client.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vendor/puppet/agent.rb', line 28

def run(*args)
  if running?
    Puppet.notice "Run of #{client_class} already in progress; skipping"
    return
  end
  if disabled?
    Puppet.notice "Skipping run of #{client_class}; administratively disabled; use 'puppet agent --enable' to re-enable."
    return
  end

  result = nil
  block_run = Puppet::Application.controlled_run do
    splay
    with_client do |client|
      begin
        sync.synchronize { lock { result = client.run(*args) } }
      rescue SystemExit,NoMemoryError
        raise
      rescue Exception => detail
        puts detail.backtrace if Puppet[:trace]
        Puppet.err "Could not run #{client_class}: #{detail}"
      end
    end
    true
  end
  Puppet.notice "Shutdown/restart in progress (#{Puppet::Application.run_status.inspect}); skipping run" unless block_run
  result
end

#splayObject

Sleep when splay is enabled; else just return.



67
68
69
70
71
72
73
74
75
# File 'lib/vendor/puppet/agent.rb', line 67

def splay
  return unless Puppet[:splay]
  return if splayed?

  time = rand(Integer(Puppet[:splaylimit]) + 1)
  Puppet.info "Sleeping for #{time} seconds (splay is enabled)"
  sleep(time)
  @splayed = true
end

#splayed?Boolean

Have we splayed already?

Returns:

  • (Boolean)


62
63
64
# File 'lib/vendor/puppet/agent.rb', line 62

def splayed?
  splayed
end

#stopping?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/vendor/puppet/agent.rb', line 57

def stopping?
  Puppet::Application.stop_requested?
end

#syncObject



77
78
79
# File 'lib/vendor/puppet/agent.rb', line 77

def sync
  @sync ||= Sync.new
end