Package buildbot :: Package changes :: Module base
[frames] | no frames]

Source Code for Module buildbot.changes.base

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  from zope.interface import implements 
17  from twisted.application import service 
18  from twisted.internet import defer, task, reactor 
19  from twisted.python import log 
20   
21  from buildbot.interfaces import IChangeSource 
22  from buildbot import util 
23   
24 -class ChangeSource(service.Service, util.ComparableMixin):
25 implements(IChangeSource) 26 27 master = None 28 "if C{self.running} is true, then C{cs.master} points to the buildmaster." 29
30 - def describe(self):
31 pass
32
33 -class PollingChangeSource(ChangeSource):
34 """ 35 Utility subclass for ChangeSources that use some kind of periodic polling 36 operation. Subclasses should define C{poll} and set C{self.pollInterval}. 37 The rest is taken care of. 38 39 Any subclass will be available via the "poller" webhook. 40 """ 41 42 pollInterval = 60 43 "time (in seconds) between calls to C{poll}" 44 45 _loop = None 46
47 - def __init__(self, name=None, pollInterval=60*10):
48 if name: 49 self.setName(name) 50 self.pollInterval = pollInterval 51 52 self.doPoll = util.misc.SerializedInvocation(self.doPoll)
53
54 - def doPoll(self):
55 """ 56 This is the method that is called by LoopingCall to actually poll. 57 It may also be called by change hooks to request a poll. 58 It is serialiazed - if you call it while a poll is in progress 59 then the 2nd invocation won't start until the 1st has finished. 60 """ 61 d = defer.maybeDeferred(self.poll) 62 d.addErrback(log.err, 'while polling for changes') 63 return d
64
65 - def poll(self):
66 """ 67 Perform the polling operation, and return a deferred that will fire 68 when the operation is complete. Failures will be logged, but the 69 method will be called again after C{pollInterval} seconds. 70 """
71
72 - def startLoop(self):
73 self._loop = task.LoopingCall(self.doPoll) 74 self._loop.start(self.pollInterval, now=False)
75
76 - def stopLoop(self):
77 if self._loop and self._loop.running: 78 self._loop.stop() 79 self._loop = None
80
81 - def startService(self):
82 ChangeSource.startService(self) 83 84 # delay starting doing anything until the reactor is running - if 85 # services are still starting up, they may miss an initial flood of 86 # changes 87 if self.pollInterval: 88 reactor.callWhenRunning(self.startLoop) 89 else: 90 reactor.callWhenRunning(self.doPoll)
91
92 - def stopService(self):
93 self.stopLoop() 94 return ChangeSource.stopService(self)
95