|
# This file is part of Buildbot. Buildbot is free software: you can # redistribute it and/or modify it under the terms of the GNU General Public # License as published by the Free Software Foundation, version 2. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Copyright Buildbot Team Members
One-at-a-time notification-triggered Deferred event loop. Each such loop has a 'doorbell' named trigger() and a set of processing functions. The processing functions are expected to be callables like Scheduler methods, which examine a database for work to do. The doorbell will be rung by other code that writes into the database (possibly in a separate process).
At some point after the doorbell is rung, each function will be run in turn, one at a time. Each function can return a Deferred, and the next function will not be run until the previous one's Deferred has fired. That is, at all times, at most one processing function will be active.
If the doorbell is rung during a run, the loop will be run again later. Multiple rings may be handled by a single run, but the class guarantees that there will be at least one full run that begins after the last ring. The relative order of processing functions within a run is not preserved. If a processing function is added to the loop more than once, it will still only be called once per run.
If the Deferred returned by the processing function fires with a number, the event loop will call that function again at or after the given time (expressed as seconds since epoch). This can be used by processing functions when they want to 'sleep' until some amount of time has passed, such as for a Scheduler that is waiting for a tree-stable-timer to expire, or a Periodic scheduler that wants to fire once every six hours. This delayed call will obey the same one-at-a-time behavior as the run-everything trigger.
Each function's return-value-timer value will replace the previous timer. Any outstanding timer will be cancelled just before invoking a processing function. As a result, these functions should basically be idempotent: if the database says that the Scheduler needs to wake up at 5pm, it should keep returning '5pm' until it gets called after 5pm, at which point it should start returning None.
The functions should also add an epsilon (perhaps one second) to their desired wakeup time, so that rounding errors or low-resolution system timers don't cause 'OCD Alarm Clock Syndrome' (in which they get woken up a moment too early and then try to sleep repeatedly for zero seconds). The event loop will silently impose a 5-second minimum delay time to avoid this.
Any errors in the processing functions are written to log.err and then ignored. """
self._start_timer.cancel() self._wakeup_timer.cancel()
# if we're triggered while not running, ignore it. We'll automatically # trigger when the service starts log.msg("loop triggered while service disabled; ignoring trigger") return
# timers are now redundant, so cancel any existing ones
raise Exception('subclasses must implement get_processors()')
def _loop_start(self): else: # don't run a processor that was removed while it still # had a timer running # consider sorting by 'when'
def _loop_next(self):
def _loop_done(self): # we're really idle, so notify waiters (used by unit tests)
# this can be overridden by subclasses to do more work when we've # finished a pass through the loop and don't need to immediately # start a new one pass
# don't wake up right away. By doing this here instead of in # _set_wakeup_timer, we avoid penalizing unrelated jobs which # want to wake up a few seconds apart
self._wakeup_timer.cancel() self._wakeup_timer = None # to avoid waking too frequently, this could be: # delay=max(when-now,OCD_MINIMUM_DELAY) # but that delays unrelated jobs that want to wake few seconds apart self._wakeup_timer.reset(delay) else:
self.processors.remove(processor)
"""I am a Loop which gets my processors from my service children. When I run, I iterate over each of them, invoking their 'run' method."""
|