blob: c6ed066b703007bbb84b16cbb04dbcaf287c11a0 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
2import base64
3from datetime import datetime
Scott Baker86e132c2015-02-11 21:38:09 -08004from xos.config import Config
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04005from 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):
Sapan Bhatia3e328352014-07-23 10:03:50 -040047 # This is the most common implementation of fetch_pending
48 # Steps should override it if they have their own logic
49 # for figuring out what objects are outstanding.
50 main_obj = self.provides[0]
51 if (not deleted):
52 objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
53 else:
54 objs = main_obj.deleted_objects.all()
55
56 return objs
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040057 #return Sliver.objects.filter(ip=None)
58
59 def check_dependencies(self, obj, failed):
60 for dep in self.dependencies:
Sapan Bhatia3e328352014-07-23 10:03:50 -040061 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
62 peer_object = getattr(obj, peer_name)
Sapan Bhatia95470622014-08-04 10:48:28 -040063
64 # peer_object can be None, and if so there
65 # is no object-level dependency
66 if (peer_object and peer_object.pk==failed.pk):
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040067 raise FailedDependency
68
69 def call(self, failed=[], deletion=False):
70 pending = self.fetch_pending(deletion)
71 for o in pending:
72 try:
73 for f in failed:
74 self.check_dependencies(o,f) # Raises exception if failed
75 if (deletion):
76 self.delete_record(o)
77 o.delete(purge=True)
78 else:
79 self.sync_record(o)
80 o.enacted = datetime.now() # Is this the same timezone? XXX
81 o.backend_status = "OK"
82 o.save(update_fields=['enacted'])
83 except Exception,e:
84 try:
85 o.backend_status = self.error_map.map(str(e))
86 except:
87 o.backend_status = str(e)
88
Sapan Bhatiaf1d3d272014-08-18 02:24:22 -040089 if (o.pk):
90 o.save(update_fields=['backend_status'])
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040091
92 logger.log_exc("sync step failed!")
93 failed.append(o)
94
95 return failed
96
97 def __call__(self, **args):
98 return self.call(**args)