3 from datetime import datetime
4 from planetstack.config import Config
5 from util.logger import Logger, logging
6 from observer.steps import *
7 from observer.error_mapper import error_mapper
9 logger = Logger(level=logging.INFO)
11 class FailedDependency(Exception):
15 """ A PlanetStack Sync step.
18 psmodel Model name the step synchronizes
19 dependencies list of names of models that must be synchronized first if the current model depends on them
24 sync_config_dir = Config().sync_config_dir
26 sync_config_dir = '/etc/planetstack/sync'
27 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
28 return open(prop_config_path).read().rstrip()
30 def __init__(self, **args):
31 """Initialize a sync step
33 name -- Name of the step
34 provides -- PlanetStack models sync'd by this step
37 self.driver = args.get('driver')
38 self.error_map = args.get('error_map')
41 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
43 self.soft_deadline = 5 # 5 seconds
47 def fetch_pending(self):
49 #return Sliver.objects.filter(ip=None)
51 def check_dependencies(self, obj, failed):
52 for dep in self.dependencies:
53 peer_object = getattr(obj, dep.lower())
54 if (peer_object.pk==failed.pk):
55 raise FailedDependency
57 def call(self, failed=[]):
58 pending = self.fetch_pending()
62 self.check_dependencies(o,f) # Raises exception if failed
64 o.enacted = datetime.now() # Is this the same timezone? XXX
65 o.save(update_fields=['enacted'])
68 o.backend_status = self.error_map.map(str(e))
70 o.backend_status = str(e)
72 o.save(update_fields=['backend_status'])
74 logger.log_exc("sync step failed!")
79 def __call__(self, **args):
80 return self.call(**args)