Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from planetstack.config import Config |
| 4 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 5 | class FailedDependency(Exception): |
| 6 | pass |
| 7 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | class 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 Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 30 | dependencies = [] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 31 | 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 Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 40 | |
| 41 | def check_dependencies(self, obj): |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 42 | for dep in self.dependencies: |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 43 | peer_object = getattr(obj, dep.name.lowercase()) |
| 44 | if (peer_object.pk==dep.pk): |
| 45 | raise DependencyFailed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 47 | def call(self, failed=[]): |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 48 | pending = self.fetch_pending() |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 49 | for o in pending: |
| 50 | if (not self.depends_on(o, failed)): |
| 51 | try: |
| 52 | check_dependencies(o) # Raises exception if failed |
| 53 | self.sync_record(o) |
| 54 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 55 | o.save(update_fields=['enacted']) |
| 56 | except: |
| 57 | failed.append(o) |
| 58 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 59 | |
| 60 | def __call__(self): |
| 61 | return self.call() |