Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from datetime import datetime |
Tony Mack | 4947567 | 2015-02-24 14:19:07 -0500 | [diff] [blame] | 4 | |
| 5 | from django.db.models import F, Q |
| 6 | |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 7 | from xos.config import Config |
Scott Baker | f154cc2 | 2016-01-14 16:07:32 -0800 | [diff] [blame] | 8 | from xos.logger import Logger, logging |
Sapan Bhatia | 003e84c | 2016-01-15 11:05:52 -0500 | [diff] [blame] | 9 | from synchronizers.base.steps import * |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 10 | |
| 11 | logger = Logger(level=logging.INFO) |
| 12 | |
| 13 | class FailedDependency(Exception): |
| 14 | pass |
| 15 | |
| 16 | class SyncStep: |
Scott Baker | 286a78f | 2015-02-18 16:13:48 -0800 | [diff] [blame] | 17 | """ A XOS Sync step. |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 18 | |
| 19 | Attributes: |
| 20 | psmodel Model name the step synchronizes |
| 21 | dependencies list of names of models that must be synchronized first if the current model depends on them |
| 22 | """ |
| 23 | slow=False |
Tony Mack | fbdae1b | 2015-02-24 14:16:43 -0500 | [diff] [blame] | 24 | def get_prop(self, prop): |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 25 | try: |
| 26 | sync_config_dir = Config().sync_config_dir |
| 27 | except: |
Scott Baker | b8059c9 | 2015-02-19 22:25:49 -0800 | [diff] [blame] | 28 | sync_config_dir = '/etc/xos/sync' # XXX TODO: update path |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 29 | prop_config_path = '/'.join(sync_config_dir,self.name,prop) |
| 30 | return open(prop_config_path).read().rstrip() |
| 31 | |
| 32 | def __init__(self, **args): |
| 33 | """Initialize a sync step |
| 34 | Keyword arguments: |
| 35 | name -- Name of the step |
Scott Baker | 286a78f | 2015-02-18 16:13:48 -0800 | [diff] [blame] | 36 | provides -- XOS models sync'd by this step |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 37 | """ |
| 38 | dependencies = [] |
| 39 | self.driver = args.get('driver') |
| 40 | self.error_map = args.get('error_map') |
| 41 | |
| 42 | try: |
| 43 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 44 | except: |
| 45 | self.soft_deadline = 5 # 5 seconds |
| 46 | |
| 47 | return |
| 48 | |
| 49 | def fetch_pending(self, deletion=False): |
Sapan Bhatia | 3e32835 | 2014-07-23 10:03:50 -0400 | [diff] [blame] | 50 | # This is the most common implementation of fetch_pending |
| 51 | # Steps should override it if they have their own logic |
| 52 | # for figuring out what objects are outstanding. |
| 53 | main_obj = self.provides[0] |
Tony Mack | fbdae1b | 2015-02-24 14:16:43 -0500 | [diff] [blame] | 54 | if (not deletion): |
Sapan Bhatia | 3e32835 | 2014-07-23 10:03:50 -0400 | [diff] [blame] | 55 | objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 56 | else: |
| 57 | objs = main_obj.deleted_objects.all() |
| 58 | |
| 59 | return objs |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 60 | #return Instance.objects.filter(ip=None) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 61 | |
| 62 | def check_dependencies(self, obj, failed): |
| 63 | for dep in self.dependencies: |
Sapan Bhatia | 3e32835 | 2014-07-23 10:03:50 -0400 | [diff] [blame] | 64 | peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower |
| 65 | peer_object = getattr(obj, peer_name) |
Sapan Bhatia | 9547062 | 2014-08-04 10:48:28 -0400 | [diff] [blame] | 66 | |
| 67 | # peer_object can be None, and if so there |
| 68 | # is no object-level dependency |
| 69 | if (peer_object and peer_object.pk==failed.pk): |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 70 | raise FailedDependency |
| 71 | |
| 72 | def call(self, failed=[], deletion=False): |
| 73 | pending = self.fetch_pending(deletion) |
| 74 | for o in pending: |
| 75 | try: |
| 76 | for f in failed: |
| 77 | self.check_dependencies(o,f) # Raises exception if failed |
| 78 | if (deletion): |
| 79 | self.delete_record(o) |
| 80 | o.delete(purge=True) |
| 81 | else: |
| 82 | self.sync_record(o) |
| 83 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 84 | o.backend_status = "OK" |
| 85 | o.save(update_fields=['enacted']) |
| 86 | except Exception,e: |
| 87 | try: |
| 88 | o.backend_status = self.error_map.map(str(e)) |
| 89 | except: |
| 90 | o.backend_status = str(e) |
| 91 | |
Sapan Bhatia | f1d3d27 | 2014-08-18 02:24:22 -0400 | [diff] [blame] | 92 | if (o.pk): |
| 93 | o.save(update_fields=['backend_status']) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 94 | |
Sapan Bhatia | 8dddf66 | 2016-04-06 19:04:05 +0200 | [diff] [blame] | 95 | logger.log_exc("sync step failed!",extra=o.tologdict()) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 96 | failed.append(o) |
| 97 | |
| 98 | return failed |
| 99 | |
| 100 | def __call__(self, **args): |
| 101 | return self.call(**args) |