Cleanup the pickling.

1. When loading the pickled file, don't log if it isn't there
  a. Do log though if it is there and unpickling fails
2. On writing log if pickling fails and log if pickle writing fails.
This commit is contained in:
Joshua Harlow 2012-06-29 12:18:54 -07:00
parent 1f007b82d7
commit 4d6bcf81d4

View File

@ -159,15 +159,23 @@ class Init(object):
return merger.cfg return merger.cfg
def _restore_from_cache(self): def _restore_from_cache(self):
pickled_fn = self.paths.get_ipath_cur('obj_pkl') # We try to restore from a current link and static path
try:
# we try to restore from a current link and static path
# by using the instance link, if purge_cache was called # by using the instance link, if purge_cache was called
# the file wont exist # the file wont exist.
return pickle.loads(util.load_file(pickled_fn)) pickled_fn = self.paths.get_ipath_cur('obj_pkl')
pickle_contents = None
try:
pickle_contents = util.load_file(pickled_fn)
except Exception: except Exception:
util.logexc(LOG, "Failed loading pickled datasource from %s", pass
pickled_fn) # This is expected so just return nothing
# successfully loaded...
if not pickle_contents:
return None
try:
return pickle.loads(pickle_contents)
except Exception:
util.logexc(LOG, "Failed loading pickled blob from %s", pickled_fn)
return None return None
def _write_to_cache(self): def _write_to_cache(self):
@ -175,12 +183,16 @@ class Init(object):
return False return False
pickled_fn = self.paths.get_ipath_cur("obj_pkl") pickled_fn = self.paths.get_ipath_cur("obj_pkl")
try: try:
contents = pickle.dumps(self.datasource) pk_contents = pickle.dumps(self.datasource)
util.write_file(pickled_fn, contents, mode=0400) except Exception:
return True util.logexc(LOG, "Failed pickling datasource %s", self.datasource)
return False
try:
util.write_file(pickled_fn, pk_contents, mode=0400)
except Exception: except Exception:
util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn) util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn)
return False return False
return True
def _get_datasources(self): def _get_datasources(self):
# Any config provided??? # Any config provided???