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 |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 6 | from observer.steps import * |
Sapan Bhatia | d9468eb | 2014-08-20 03:03:12 -0400 | [diff] [blame] | 7 | from django.db.models import F, Q |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 8 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 9 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 11 | class FailedDependency(Exception): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 12 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 13 | |
Tony Mack | e10fbe5 | 2015-01-03 19:37:39 -0500 | [diff] [blame] | 14 | class SyncStep(object): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 15 | """ A PlanetStack Sync step. |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 16 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 17 | Attributes: |
| 18 | psmodel Model name the step synchronizes |
| 19 | dependencies list of names of models that must be synchronized first if the current model depends on them |
| 20 | """ |
| 21 | slow=False |
| 22 | def get_prop(prop): |
| 23 | try: |
| 24 | sync_config_dir = Config().sync_config_dir |
| 25 | except: |
| 26 | sync_config_dir = '/etc/planetstack/sync' |
| 27 | prop_config_path = '/'.join(sync_config_dir,self.name,prop) |
| 28 | return open(prop_config_path).read().rstrip() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 29 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 30 | def __init__(self, **args): |
| 31 | """Initialize a sync step |
| 32 | Keyword arguments: |
| 33 | name -- Name of the step |
| 34 | provides -- PlanetStack models sync'd by this step |
| 35 | """ |
| 36 | dependencies = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 37 | self.driver = args.get('driver') |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 38 | self.error_map = args.get('error_map') |
| 39 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 40 | try: |
| 41 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 42 | except: |
| 43 | self.soft_deadline = 5 # 5 seconds |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 44 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 45 | return |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | e17bc5b | 2014-04-30 00:53:06 -0400 | [diff] [blame] | 47 | def fetch_pending(self, deletion=False): |
Sapan Bhatia | 2176566 | 2014-07-23 08:59:30 -0400 | [diff] [blame] | 48 | # This is the most common implementation of fetch_pending |
| 49 | # Steps should override it if they have their own logic |
| 50 | # for figuring out what objects are outstanding. |
| 51 | main_obj = self.provides[0] |
Sapan Bhatia | d9468eb | 2014-08-20 03:03:12 -0400 | [diff] [blame] | 52 | if (not deletion): |
Sapan Bhatia | 2176566 | 2014-07-23 08:59:30 -0400 | [diff] [blame] | 53 | objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 54 | else: |
| 55 | objs = main_obj.deleted_objects.all() |
| 56 | |
| 57 | return objs |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 58 | #return Sliver.objects.filter(ip=None) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 59 | |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 60 | def check_dependencies(self, obj, failed): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 61 | for dep in self.dependencies: |
Scott Baker | 105b6b7 | 2014-05-12 10:40:25 -0700 | [diff] [blame] | 62 | peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower |
Sapan Bhatia | cfef6ef | 2014-08-20 03:04:03 -0400 | [diff] [blame] | 63 | try: |
| 64 | peer_object = getattr(obj, peer_name) |
| 65 | except: |
| 66 | peer_object = None |
| 67 | |
Scott Baker | 0652f4e | 2014-08-19 15:49:27 -0700 | [diff] [blame] | 68 | if (peer_object and peer_object.pk==failed.pk and type(peer_object)==type(failed)): |
Sapan Bhatia | 7e482de | 2014-08-22 03:05:13 -0400 | [diff] [blame] | 69 | if (obj.backend_status!=peer_object.backend_status): |
| 70 | obj.backend_status = peer_object.backend_status |
| 71 | obj.save(update_fields=['backend_status']) |
| 72 | raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(obj.pk), peer_object.__class__.__name__, str(peer_object.pk), failed.__class__.__name__, str(failed.pk))) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 73 | |
Sapan Bhatia | 6082336 | 2014-04-30 00:52:32 -0400 | [diff] [blame] | 74 | def call(self, failed=[], deletion=False): |
| 75 | pending = self.fetch_pending(deletion) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 76 | for o in pending: |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 77 | try: |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 78 | for f in failed: |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 79 | self.check_dependencies(o,f) # Raises exception if failed |
Sapan Bhatia | 6082336 | 2014-04-30 00:52:32 -0400 | [diff] [blame] | 80 | if (deletion): |
| 81 | self.delete_record(o) |
| 82 | o.delete(purge=True) |
| 83 | else: |
| 84 | self.sync_record(o) |
| 85 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 86 | o.backend_status = "OK" |
| 87 | o.save(update_fields=['enacted']) |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 88 | except Exception,e: |
Sapan Bhatia | 8fd852b | 2014-08-22 03:06:03 -0400 | [diff] [blame] | 89 | logger.log_exc("sync step failed!") |
| 90 | str_e = '%r'%e |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 91 | try: |
Sapan Bhatia | 8fd852b | 2014-08-22 03:06:03 -0400 | [diff] [blame] | 92 | o.backend_status = self.error_map.map(str_e) |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 93 | except: |
Sapan Bhatia | 8fd852b | 2014-08-22 03:06:03 -0400 | [diff] [blame] | 94 | o.backend_status = str_e |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 95 | |
Sapan Bhatia | 9c308fc | 2014-08-22 03:07:59 -0400 | [diff] [blame] | 96 | # TOFIX: |
| 97 | # DatabaseError: value too long for type character varying(140) |
Sapan Bhatia | f1d3d27 | 2014-08-18 02:24:22 -0400 | [diff] [blame] | 98 | if (o.pk): |
Sapan Bhatia | 9c308fc | 2014-08-22 03:07:59 -0400 | [diff] [blame] | 99 | try: |
| 100 | o.save(update_fields=['backend_status']) |
| 101 | except: |
| 102 | print "Could not update backend status field!" |
| 103 | pass |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 104 | |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 105 | failed.append(o) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 106 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 107 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 108 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 109 | def __call__(self, **args): |
| 110 | return self.call(**args) |