Feed errors that occur in the back end in the backend_status field
[plstackapi.git] / planetstack / observer / syncstep.py
1 import os
2 import base64
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
8
9 logger = Logger(level=logging.INFO)
10
11 class FailedDependency(Exception):
12     pass
13
14 class SyncStep:
15     """ A PlanetStack Sync step. 
16
17     Attributes:
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
20     """ 
21     slow=False
22     def get_prop(prop):
23         try:
24             sync_config_dir = Config().sync_config_dir
25         except:
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()
29
30     def __init__(self, **args):
31         """Initialize a sync step
32            Keyword arguments:
33                    name -- Name of the step
34                 provides -- PlanetStack models sync'd by this step
35         """
36         dependencies = []
37         self.driver = args.get('driver')
38         self.error_map = args.get('error_map')
39
40         try:
41             self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
42         except:
43             self.soft_deadline = 5 # 5 seconds
44
45         return
46
47     def fetch_pending(self):
48         return []
49         #return Sliver.objects.filter(ip=None)
50     
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
56
57     def call(self, failed=[]):
58         pending = self.fetch_pending()
59         for o in pending:
60             try:
61                 for f in failed:
62                     self.check_dependencies(o,f) # Raises exception if failed
63                 self.sync_record(o)
64                 o.enacted = datetime.now() # Is this the same timezone? XXX
65                 o.save(update_fields=['enacted'])
66             except Exception,e:
67                 try:
68                     o.backend_status = self.error_map.map(str(e))
69                 except:
70                     o.backend_status = str(e)
71
72                 o.save(update_fields=['backend_status'])
73
74                 logger.log_exc("sync step failed!")
75                 failed.append(o)
76
77         return failed
78
79     def __call__(self, **args):
80         return self.call(**args)