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

Source Code for Module gofer.agent.action

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 14  # 
 15   
 16  """ 
 17  Action slass for gofer agent. 
 18  """ 
 19   
 20  import inspect 
 21  from gofer.collator import Collator 
 22  from datetime import datetime as dt 
 23  from datetime import timedelta 
 24  from logging import getLogger 
 25   
 26  log = getLogger(__name__) 
27 28 29 -class Actions:
30 """ 31 @cvar functions: The list of decorated functions. 32 """ 33 functions = {} 34 35 @classmethod
36 - def add(cls, fn, interval):
37 cls.functions[fn] = interval
38 39 @classmethod
40 - def collated(cls):
41 collated = [] 42 c = Collator() 43 classes, functions = c.collate(cls.functions) 44 for c,m in classes.items(): 45 inst = c() 46 for m,d in m: 47 m = getattr(inst, m.__name__) 48 action = Action(m, **d) 49 collated.append(action) 50 for m,f in functions.items(): 51 for f,d in f: 52 action = Action(f, **d) 53 collated.append(action) 54 return collated
55 56 @classmethod
57 - def clear(cls):
58 """ 59 Clear the list of actions. 60 """ 61 cls.functions = {}
62
63 64 -def action(**interval):
65 """ 66 Action decorator. 67 """ 68 def decorator(fn): 69 Actions.add(fn, interval) 70 return fn
71 return decorator 72
73 74 -class Action:
75 """ 76 Abstract recurring action (base). 77 @ivar target: The action target. 78 @type target: (method|function) 79 @keyword interval: The run interval. 80 One of: 81 - days 82 - seconds 83 - minutes 84 - hours 85 - weeks 86 @ivar last: The last run timestamp. 87 @type last: datetime 88 """ 89
90 - def __init__(self, target, **interval):
91 """ 92 @param target: The action target. 93 @type target: (method|function) 94 @param interval: The run interval (minutes). 95 @type interval: timedelta 96 """ 97 self.target = target 98 for k,v in interval.items(): 99 interval[k] = int(v) 100 self.interval = timedelta(**interval) 101 self.last = dt(1900, 1, 1)
102
103 - def name(self):
104 """ 105 Get action name. Default to class name. 106 @return: The action name. 107 @rtype: str 108 """ 109 t = self.target 110 if inspect.ismethod(t): 111 cls = t.im_class 112 else: 113 cls = t.__module__ 114 method = t.__name__ 115 return '%s.%s()' % (cls, method)
116
117 - def __str__(self):
118 return self.name()
119
120 - def __call__(self):
121 try: 122 next = self.last+self.interval 123 now = dt.utcnow() 124 if next < now: 125 self.last = now 126 log.debug('perform "%s"', self.name()) 127 self.target() 128 except Exception, e: 129 log.exception(e)
130