blob: 187b318c82a50ea303a065cedbe75009508840cd [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
2import base64
3from datetime import datetime
4from planetstack.config import Config
5from util.logger import Logger, logging
6from observer.steps import *
7
8logger = Logger(level=logging.INFO)
9
10class FailedDependency(Exception):
11 pass
12
13class SyncStep:
14 """ A PlanetStack Sync step.
15
16 Attributes:
17 psmodel Model name the step synchronizes
18 dependencies list of names of models that must be synchronized first if the current model depends on them
19 """
20 slow=False
21 def get_prop(prop):
22 try:
23 sync_config_dir = Config().sync_config_dir
24 except:
25 sync_config_dir = '/etc/planetstack/sync'
26 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
27 return open(prop_config_path).read().rstrip()
28
29 def __init__(self, **args):
30 """Initialize a sync step
31 Keyword arguments:
32 name -- Name of the step
33 provides -- PlanetStack models sync'd by this step
34 """
35 dependencies = []
36 self.driver = args.get('driver')
37 self.error_map = args.get('error_map')
38
39 try:
40 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
41 except:
42 self.soft_deadline = 5 # 5 seconds
43
44 return
45
46 def fetch_pending(self, deletion=False):
47 return []
48 #return Sliver.objects.filter(ip=None)
49
50 def check_dependencies(self, obj, failed):
51 for dep in self.dependencies:
52 peer_object = getattr(obj, dep.lower())
53 if (peer_object.pk==failed.pk):
54 raise FailedDependency
55
56 def call(self, failed=[], deletion=False):
57 pending = self.fetch_pending(deletion)
58 for o in pending:
59 try:
60 for f in failed:
61 self.check_dependencies(o,f) # Raises exception if failed
62 if (deletion):
63 self.delete_record(o)
64 o.delete(purge=True)
65 else:
66 self.sync_record(o)
67 o.enacted = datetime.now() # Is this the same timezone? XXX
68 o.backend_status = "OK"
69 o.save(update_fields=['enacted'])
70 except Exception,e:
71 try:
72 o.backend_status = self.error_map.map(str(e))
73 except:
74 o.backend_status = str(e)
75
76 o.save(update_fields=['backend_status'])
77
78 logger.log_exc("sync step failed!")
79 failed.append(o)
80
81 return failed
82
83 def __call__(self, **args):
84 return self.call(**args)