Skip to content

Commit

Permalink
Continue on cache write failure
Browse files Browse the repository at this point in the history
Fixes issue david-caro#67
  • Loading branch information
jcmcken committed Mar 23, 2016
1 parent afc0413 commit a4071a7
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions foreman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,24 +737,6 @@ def _get_remote_defs(self):

if res.ok:
data = json.loads(res.text)
defs_path = os.path.join(self.cache_dir, 'definitions')

if not os.path.exists(defs_path):
try:
os.makedirs(defs_path)
except:
logger.debug('Unable to create cache dir %s', defs_path)
return
cache_fn = '%s/%s-v%s.json' % (
defs_path, self.version,
self.api_version,
)
try:
with open(cache_fn, 'w') as cache_fd:
cache_fd.write(json.dumps(data, indent=4, default=str))
logger.debug('Wrote cache file %s', cache_fn)
except:
logger.debug('Unable to write cache file %s', cache_fn)
else:
if res.status_code == 404:
logger.warn(
Expand All @@ -769,6 +751,23 @@ def _get_remote_defs(self):
"There was an error trying to get api definition from %s/%s"
% (self.url, 'apidoc/v%s.json' % self.api_version)
)
defs_path = os.path.join(self.cache_dir, 'definitions')

if not os.path.exists(defs_path):
try:
os.makedirs(defs_path)
except:
logger.debug('Unable to create cache dir %s', defs_path)

This comment has been minimized.

Copy link
@david-caro

david-caro Mar 23, 2016

you can do the return data here and short-circuit the file creation (if makedirs failed, for sure it will fail too)

This comment has been minimized.

Copy link
@jcmcken

jcmcken Mar 23, 2016

Author Owner

True, I guess I was trying to un-nest things a bit, but that's a simpler change

cache_fn = '%s/%s-v%s.json' % (
defs_path, self.version,
self.api_version,
)
try:
with open(cache_fn, 'w') as cache_fd:
cache_fd.write(json.dumps(data, indent=4, default=str))
logger.debug('Wrote cache file %s', cache_fn)
except:
logger.debug('Unable to write cache file %s', cache_fn)
return data

def _get_defs(self, use_cache, strict_cache):
Expand Down

0 comments on commit a4071a7

Please sign in to comment.