blob: f3eb4baefc3430df91548b339b866a1b7baa6633 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
3from planetstack.config import Config
4
Sapan Bhatia13c7f112013-09-02 14:19:35 -04005class FailedDependency(Exception):
6 pass
7
Sapan Bhatia24836f12013-08-27 10:16:05 -04008class SyncStep:
9 """ A PlanetStack Sync step.
10
11 Attributes:
12 psmodel Model name the step synchronizes
13 dependencies list of names of models that must be synchronized first if the current model depends on them
14 """
15 slow=False
16 def get_prop(prop):
17 try:
18 sync_config_dir = Config().sync_config_dir
19 except:
20 sync_config_dir = '/etc/planetstack/sync'
21 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
22 return open(prop_config_path).read().rstrip()
23
24 def __init__(self, **args):
25 """Initialize a sync step
26 Keyword arguments:
27 name -- Name of the step
28 provides -- PlanetStack models sync'd by this step
29 """
Sapan Bhatia13c7f112013-09-02 14:19:35 -040030 dependencies = []
Sapan Bhatia24836f12013-08-27 10:16:05 -040031 try:
32 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
33 except:
34 self.soft_deadline = 5 # 5 seconds
35
36 return
37
38 def fetch_pending(self):
39 return Sliver.objects.filter(ip=None)
Sapan Bhatia13c7f112013-09-02 14:19:35 -040040
41 def check_dependencies(self, obj):
42 for dep in dependencies:
43 peer_object = getattr(obj, dep.name.lowercase())
44 if (peer_object.pk==dep.pk):
45 raise DependencyFailed
Sapan Bhatia24836f12013-08-27 10:16:05 -040046
Sapan Bhatia13c7f112013-09-02 14:19:35 -040047 def call(self, failed=failed_objects):
48 pending = self.fetch_pending()
49 failed = []
50 for o in pending:
51 if (not self.depends_on(o, failed)):
52 try:
53 check_dependencies(o) # Raises exception if failed
54 self.sync_record(o)
55 o.enacted = datetime.now() # Is this the same timezone? XXX
56 o.save(update_fields=['enacted'])
57 except:
58 failed.append(o)
59 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040060
61 def __call__(self):
62 return self.call()