Skip to content

Commit 75476e1

Browse files
committed
Merge remote-tracking branch 'daniel-pittman/refactor/2.7.x/4862-remove-the-event-loop-library' into 2.7.x
* daniel-pittman/refactor/2.7.x/4862-remove-the-event-loop-library: (#4862) Finally remove the event-loop library. (#4862) define_method is not a public method in Ruby. (#4862) `returning` is not a standard Ruby method. (#4862) Stop using EventLoop in the Puppet daemon.
2 parents 3df8eef + dba27c9 commit 75476e1

File tree

13 files changed

+77
-1078
lines changed

13 files changed

+77
-1078
lines changed

lib/puppet/agent.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'sync'
2-
require 'puppet/external/event-loop'
32
require 'puppet/application'
43

54
# A general class for triggering a run of another
@@ -75,18 +74,6 @@ def splay
7574
@splayed = true
7675
end
7776

78-
# Start listening for events. We're pretty much just listening for
79-
# timer events here.
80-
def start
81-
# Create our timer. Puppet will handle observing it and such.
82-
timer = EventLoop::Timer.new(:interval => Puppet[:runinterval], :tolerance => 1, :start? => true) do
83-
run
84-
end
85-
86-
# Run once before we start following the timer
87-
timer.sound_alarm
88-
end
89-
9077
def sync
9178
@sync ||= Sync.new
9279
end

lib/puppet/daemon.rb

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'puppet'
22
require 'puppet/util/pidlock'
3-
require 'puppet/external/event-loop'
43
require 'puppet/application'
54

65
# A module that handles operations common to all daemons. This is included
@@ -120,10 +119,77 @@ def start
120119
create_pidfile
121120

122121
raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server
122+
123+
# Start the listening server, if required.
123124
server.start if server
124-
agent.start if agent
125125

126-
EventLoop.current.run
126+
# Finally, loop forever running events - or, at least, until we exit.
127+
run_event_loop
128+
end
129+
130+
def run_event_loop
131+
# Now, we loop waiting for either the configuration file to change, or the
132+
# next agent run to be due. Fun times.
133+
#
134+
# We want to trigger the reparse if 15 seconds passed since the previous
135+
# wakeup, and the agent run if Puppet[:runinterval] seconds have passed
136+
# since the previous wakeup.
137+
#
138+
# We always want to run the agent on startup, so it was always before now.
139+
# Because 0 means "continuously run", `to_i` does the right thing when the
140+
# input is strange or badly formed by returning 0. Integer will raise,
141+
# which we don't want, and we want to protect against -1 or below.
142+
next_agent_run = 0
143+
agent_run_interval = [Puppet[:runinterval].to_i, 0].max
144+
145+
# We may not want to reparse; that can be disable. Fun times.
146+
next_reparse = 0
147+
reparse_interval = Puppet[:filetimeout].to_i
148+
149+
loop do
150+
now = Time.now.to_i
151+
152+
# Handle reparsing of configuration files, if desired and required.
153+
# `reparse` will just check if the action is required, and would be
154+
# better named `reparse_if_changed` instead.
155+
if reparse_interval > 0 and now >= next_reparse
156+
Puppet.settings.reparse
157+
158+
# The time to the next reparse might have changed, so recalculate
159+
# now. That way we react dynamically to reconfiguration.
160+
reparse_interval = Puppet[:filetimeout].to_i
161+
next_reparse = now + reparse_interval
162+
163+
# We should also recalculate the agent run interval, and adjust the
164+
# next time it is scheduled to run, just in case. In the event that
165+
# we made no change the result will be a zero second adjustment.
166+
new_run_interval = [Puppet[:runinterval].to_i, 0].max
167+
next_agent_run += agent_run_interval - new_run_interval
168+
agent_run_interval = new_run_interval
169+
end
170+
171+
# Handle triggering another agent run. This will block the next check
172+
# for configuration reparsing, which is a desired and deliberate
173+
# behaviour. You should not change that. --daniel 2012-02-21
174+
if agent and now >= next_agent_run
175+
agent.run
176+
next_agent_run = now + agent_run_interval
177+
end
178+
179+
# Finally, an interruptable able sleep until the next scheduled event.
180+
# We also set a default wakeup of "one hour from now", which will
181+
# recheck everything at a minimum every hour. Just in case something in
182+
# the math messes up or something; it should be inexpensive enough to
183+
# wake once an hour, then go back to sleep after doing nothing, if
184+
# someone only wants listen mode.
185+
next_event = now + 60 * 60
186+
next_event > next_reparse and next_event = next_reparse
187+
next_event > next_agent_run and next_event = next_agent_run
188+
189+
how_long = next_event - now
190+
191+
how_long > 0 and select([], [], [], how_long)
192+
end
127193
end
128194
end
129195

lib/puppet/external/event-loop.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)