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): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 6 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 7 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | class SyncStep: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 9 | """ A PlanetStack Sync step. |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 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() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 23 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 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 | """ |
| 30 | dependencies = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 31 | self.driver = args.get('driver') |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 32 | try: |
| 33 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 34 | except: |
| 35 | self.soft_deadline = 5 # 5 seconds |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 36 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 37 | return |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 38 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 39 | def fetch_pending(self): |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 40 | #return Sliver.objects.filter(ip=None) |
| 41 | return [] |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 42 | |
| 43 | def check_dependencies(self, obj): |
| 44 | for dep in self.dependencies: |
| 45 | peer_object = getattr(obj, dep.name.lowercase()) |
| 46 | if (peer_object.pk==dep.pk): |
| 47 | raise DependencyFailed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 48 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 49 | def call(self, failed=[]): |
| 50 | pending = self.fetch_pending() |
| 51 | for o in pending: |
| 52 | if (not self.depends_on(o, failed)): |
| 53 | try: |
| 54 | check_dependencies(o) # Raises exception if failed |
| 55 | self.sync_record(o) |
| 56 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 57 | o.save(update_fields=['enacted']) |
| 58 | except: |
| 59 | failed.append(o) |
| 60 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 61 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 62 | def __call__(self, **args): |
| 63 | return self.call(**args) |