Skip to content

Commit

Permalink
Fixed a crash when the network was unavailable.
Browse files Browse the repository at this point in the history
  • Loading branch information
abalijepalli committed Feb 14, 2018
1 parent cd34c37 commit 072a4b4
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions mosaic/utilities/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,39 @@ def _gaPost(eventType, content):
pass

def _gaCredentialCache():
logger=mlog.mosaicLogging().getLogger(name=__name__)
# ga_cache=resource_path("mosaic/utilities/.ga")
ga_cache=format_path(tempfile.gettempdir()+'/.ga')
logger.debug(_d("Looking for GA cache {0}", ga_cache))

try:
gaModTime = datetime.fromtimestamp(os.stat(ga_cache).st_mtime)
gaExpireAge=timedelta(hours=24)
gaAge=datetime.today() - gaModTime

if gaAge > gaExpireAge:
logger.debug(_d("GA settings cache has expired."))
logger=mlog.mosaicLogging().getLogger(name=__name__)
# ga_cache=resource_path("mosaic/utilities/.ga")
ga_cache=format_path(tempfile.gettempdir()+'/.ga')
logger.debug(_d("Looking for GA cache {0}", ga_cache))

try:
gaModTime = datetime.fromtimestamp(os.stat(ga_cache).st_mtime)
gaExpireAge=timedelta(hours=24)
gaAge=datetime.today() - gaModTime

if gaAge > gaExpireAge:
logger.debug(_d("GA settings cache has expired."))
ga_old=_gaSettingsDict(ga_cache)
_getGASettings(ga_cache)
ga_new=_gaSettingsDict(ga_cache)

if ga_old["gaenable"]==False:
ga_new["gaenable"]=False

with open(ga_cache, "w") as ga:
ga.write(json.dumps(ga_new))
else:
logger.debug(_d("GA settings cache found ({0}). gaAge={1}", str(ga_cache), str(gaAge)))
except:
logger.debug(_d("GA settings are not cached."))
_getGASettings(ga_cache)
else:
logger.debug(_d("GA settings cache found. gaAge={0}", gaAge))

except:
logger.debug(_d("GA settings are not cached."))
_getGASettings(ga_cache)

with open(ga_cache, 'r') as ga:
return json.loads(ga.read())
with open(ga_cache, 'r') as ga:
return json.loads(ga.read())
except BaseException as err:
logger.debug(_d("Exception ignored: {0}\n{1}", repr(err), traceback.format_exc()))
pass

def _getGASettings(ga_cache):
logger=mlog.mosaicLogging().getLogger(name=__name__)
Expand Down

0 comments on commit 072a4b4

Please sign in to comment.