Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | 4fa85fb | 2013-09-25 14:39:57 -0400 | [diff] [blame] | 3 | from datetime import datetime |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 5 | from util.logger import Logger, logging |
| 6 | |
| 7 | logger = Logger(logfile='observer.log', level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 9 | class FailedDependency(Exception): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 10 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 11 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 12 | class SyncStep: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 13 | """ A PlanetStack Sync step. |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 14 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 15 | Attributes: |
| 16 | psmodel Model name the step synchronizes |
| 17 | dependencies list of names of models that must be synchronized first if the current model depends on them |
| 18 | """ |
| 19 | slow=False |
| 20 | def get_prop(prop): |
| 21 | try: |
| 22 | sync_config_dir = Config().sync_config_dir |
| 23 | except: |
| 24 | sync_config_dir = '/etc/planetstack/sync' |
| 25 | prop_config_path = '/'.join(sync_config_dir,self.name,prop) |
| 26 | return open(prop_config_path).read().rstrip() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 27 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 28 | def __init__(self, **args): |
| 29 | """Initialize a sync step |
| 30 | Keyword arguments: |
| 31 | name -- Name of the step |
| 32 | provides -- PlanetStack models sync'd by this step |
| 33 | """ |
| 34 | dependencies = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 35 | self.driver = args.get('driver') |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 36 | try: |
| 37 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 38 | except: |
| 39 | self.soft_deadline = 5 # 5 seconds |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 40 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 41 | return |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 42 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 43 | def fetch_pending(self): |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 44 | return [] |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 45 | #return Sliver.objects.filter(ip=None) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 47 | def check_dependencies(self, obj, failed): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 48 | for dep in self.dependencies: |
Tony Mack | 4fa85fb | 2013-09-25 14:39:57 -0400 | [diff] [blame] | 49 | peer_object = getattr(obj, dep.lower()) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 50 | if (peer_object.pk==failed.pk): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 51 | raise DependencyFailed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 52 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 53 | def call(self, failed=[]): |
| 54 | pending = self.fetch_pending() |
| 55 | for o in pending: |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 56 | try: |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 57 | for f in failed: |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 58 | self.check_dependencies(o,f) # Raises exception if failed |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 59 | self.sync_record(o) |
| 60 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 61 | o.save(update_fields=['enacted']) |
| 62 | except: |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 63 | logger.log_exc("sync step failed!") |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 64 | failed.append(o) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 65 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 66 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 67 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 68 | def __call__(self, **args): |
| 69 | return self.call(**args) |