Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Remove some logged exceptions:
Browse files Browse the repository at this point in the history
 - Avoid dividing by zero should discovery not find any nodes
 - Avoid trying to sleep for 0 or less seconds
 - Improve the sleep logic
  • Loading branch information
Paul Lathrop authored and ripienaar committed Oct 4, 2010
1 parent c74e27a commit 7c0ce81
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions agent/puppetd/commander/puppetcommanderd
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# puppet daemons run at a set interval and it will control the concurrency
# across the cluster as a whole.
#
# The object is to make best use of the puppetmaster resources and to provide
# The object is to make best use of the puppetmaster resources and to provide
# a nice predictable utilization graphs of a master to assist in capacity planning.
#
# We also give priority to the Systems Administrator, if she runs an interactive
Expand Down Expand Up @@ -46,7 +46,7 @@ if File.exist?("/etc/puppetcommander.cfg")
if config.keys == @config.keys
@config = YAML::load_file('/etc/puppetcommander.cfg')
else
raise("Could not parse config, not all options given")
raise("Could not parse config, not all options given")
end
rescue Exception => e
puts "Failed to load config file /etc/puppetcommander.cfg: #{e}"
Expand Down Expand Up @@ -107,7 +107,7 @@ rescue Exception => e
puts "Could not log '#{msg}': #{e}"
end

# Does a SimpleRPC call and calculates the total amount of
# Does a SimpleRPC call and calculates the total amount of
# puppet daemons that are currently running a catalog
def concurrent_count
debug("Getting puppet status")
Expand All @@ -122,15 +122,15 @@ def run_client(client)
@puppet.custom_request("runonce", {:forcerun => true}, client, {"identity" => client})
end

# This is the beef of things, we take a desired interval and
# This is the beef of things, we take a desired interval and
# maximum allowed concurrency and basically performs the following
# pseudo actions:
#
# - count the clients that match the supplied filter
# - figure out a desired sleep interval to run the number of clients
# - figure out a desired sleep interval to run the number of clients
# in the supplied maximum interval
# - optionally shuffle the list of nodes based on the :randomize config option
# - traverse the list of discovered clients alphabetically and does a
# - traverse the list of discovered clients alphabetically and does a
# run of each one as long as the concurrency is below the limit.
# - after running the node we sleep for the remaining time of the sleep interval
# - once we've run all nodes, we rediscover and run again - this will pick
Expand All @@ -142,7 +142,7 @@ def run(interval, concurrency)

clients = @puppet.discover :verbose => false

unless clients == nil
unless (clients == nil or clients.size < 1)
begin
sleeptime = interval * 60 / clients.size

Expand All @@ -156,7 +156,7 @@ def run(interval, concurrency)

clients.each do |client|
starttime = Time.now.to_i

cur_concurrency = concurrent_count
log("Current puppetd's running: #{cur_concurrency}")

Expand All @@ -173,7 +173,12 @@ def run(interval, concurrency)
sleeptime = (interval * 60 / clients.size) - (Time.now.to_i - starttime)
log("Sleeping for #{sleeptime} seconds")

sleep sleeptime - (Time.now.to_i - starttime)
sleepfor = sleeptime - (Time.now.to_i - starttime)
if sleepfor > 0
sleep sleepfor
else
sleep 1
end
end
rescue Exception => e
log(e)
Expand All @@ -196,10 +201,10 @@ end
# starts the worker loop in the background
def background
pid = fork do
File.open("/var/run/puppetcommander.pid", "w") do |pidfile|
pidfile.puts $$
File.open("/var/run/puppetcommander.pid", "w") do |pidfile|
pidfile.puts $$
end

loop do
begin
run(@config[:interval], @config[:concurrency])
Expand All @@ -209,8 +214,8 @@ def background
retry
end
end
end
end

Process.detach(pid)
end

Expand All @@ -227,4 +232,4 @@ if @config[:daemonize]
background
else
foreground
end
end

0 comments on commit 7c0ce81

Please sign in to comment.