Package pulp :: Package client :: Package agent :: Module action
[hide private]
[frames] | no frames]

Source Code for Module pulp.client.agent.action

 1  # 
 2  # Copyright (c) 2010 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15   
16  """ 
17  Action slass for pulp agent. 
18  """ 
19   
20  from datetime import datetime as dt 
21  from datetime import timedelta 
22  from logging import getLogger 
23   
24  log = getLogger(__name__) 
25   
26   
27   
28 -def action(**interval):
29 def decorator(cls): 30 Action.actions.append((cls, interval)) 31 return cls
32 return decorator 33 34
35 -class Action:
36 """ 37 Abstract recurring action (base). 38 @keyword interval: The run interval. 39 One of: 40 - days 41 - seconds 42 - minutes 43 - hours 44 - weeks 45 @ivar last: The last run timestamp. 46 @type last: datetime 47 """ 48 49 actions = [] 50
51 - def __init__(self, **interval):
52 """ 53 @param interval: The run interval (minutes). 54 @type interval: timedelta 55 """ 56 for k,v in interval.items(): 57 interval[k] = int(v) 58 self.interval = timedelta(**interval) 59 self.last = dt(1900, 1, 1)
60
61 - def perform(self):
62 """ 63 Perform action. 64 This MUST be overridden by subclasses. 65 """ 66 pass # override
67
68 - def name(self):
69 """ 70 Get action name. Default to class name. 71 @return: The action name. 72 @rtype: str 73 """ 74 return self.__class__.__name__
75
76 - def __call__(self):
77 try: 78 next = self.last+self.interval 79 now = dt.utcnow() 80 if next < now: 81 self.last = now 82 log.info('perform "%s"', self.name()) 83 self.perform() 84 except Exception, e: 85 log.exception(e)
86