Package Products :: Package ZenHub :: Package services :: Module Procrastinator
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenHub.services.Procrastinator

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 or (at your 
 8  # option) any later version as published by the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13   
14  from sets import Set 
15  from twisted.internet import reactor 
16   
17 -class Procrastinate(object):
18 "A class to delay executing a change to a device" 19 20 _DO_LATER_DELAY = 5 21 _DO_NOW_DELAY = 0.05 22
23 - def __init__(self, cback):
24 self.cback = cback 25 self.devices = Set() 26 self.timer = None
27
28 - def clear(self):
29 self.devices = Set()
30
31 - def doLater(self, device = None):
32 if self.timer and not self.timer.called: 33 self.timer.cancel() 34 self.devices.add(device) 35 self.timer = reactor.callLater(Procrastinate._DO_LATER_DELAY, self._doNow)
36 37
38 - def _doNow(self, *unused):
39 if self.devices: 40 device = self.devices.pop() 41 self.cback(device) 42 if self.devices: 43 reactor.callLater(Procrastinate._DO_NOW_DELAY, self._doNow)
44