|
1 | 1 | require 'puppet' |
2 | 2 | require 'puppet/util/pidlock' |
3 | | -require 'puppet/external/event-loop' |
4 | 3 | require 'puppet/application' |
5 | 4 |
|
6 | 5 | # A module that handles operations common to all daemons. This is included |
@@ -120,10 +119,77 @@ def start |
120 | 119 | create_pidfile |
121 | 120 |
|
122 | 121 | raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server |
| 122 | + |
| 123 | + # Start the listening server, if required. |
123 | 124 | server.start if server |
124 | | - agent.start if agent |
125 | 125 |
|
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 |
127 | 193 | end |
128 | 194 | end |
129 | 195 |
|
0 commit comments