From e9b5cb83acc0dd58160afd701ff59680d4025b34 Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Thu, 1 Sep 2011 13:46:13 -0500 Subject: [PATCH] simplejson import and exception/logging fixes --- bin/swift-recon-cron | 18 +++++++++++------- swift/common/middleware/recon.py | 5 ++++- swift/common/utils.py | 9 ++++++--- swift/obj/replicator.py | 5 +++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/bin/swift-recon-cron b/bin/swift-recon-cron index 5a14de02df..a66a696f06 100755 --- a/bin/swift-recon-cron +++ b/bin/swift-recon-cron @@ -7,7 +7,10 @@ import os import sys import optparse from tempfile import NamedTemporaryFile -import simplejson +try: + import simplejson as json +except ImportError: + import json from ConfigParser import ConfigParser from swift.common.utils import get_logger, dump_recon_cache @@ -44,17 +47,18 @@ def main(): try: os.mkdir("/var/lock/swift-recon-object-cron") except OSError as e: - logger.critical("%s" % e) + logger.critical(_(str(e))) + print str(e) sys.exit(1) asyncs = async_count(device_dir, logger) try: - dump_recon_cache('object_replication_time', total, cache_file) - except ValueError: - logger.exception(_('Exception decoding recon cache')) + dump_recon_cache('async_pending', asyncs, cache_file) except Exception: logger.exception(_('Exception dumping recon cache')) - os.rmdir("/var/lock/swift-recon-object-cron") - + try: + os.rmdir("/var/lock/swift-recon-object-cron") + except Exception: + logger.exception(_('Exception remove cronjob lock')) if __name__ == '__main__': main() diff --git a/swift/common/middleware/recon.py b/swift/common/middleware/recon.py index f2b9e777d4..438e1e8c36 100644 --- a/swift/common/middleware/recon.py +++ b/swift/common/middleware/recon.py @@ -17,7 +17,10 @@ from webob import Request, Response from swift.common.utils import split_path, cache_from_env, get_logger from swift.common.constraints import check_mount from hashlib import md5 -import simplejson as json +try: + import simplejson as json +except ImportError: + import json import os diff --git a/swift/common/utils.py b/swift/common/utils.py index 44eaf4a507..5ba49914d2 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -34,7 +34,10 @@ from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \ RawConfigParser from optparse import OptionParser from tempfile import mkstemp, NamedTemporaryFile -import simplejson +try: + import simplejson as json +except ImportError: + import json import cPickle as pickle import glob from urlparse import urlparse as stdlib_urlparse, ParseResult @@ -1086,14 +1089,14 @@ def dump_recon_cache(cache_key, cache_value, cache_file, lock_timeout=2): try: existing_entry = cf.readline() if existing_entry: - cache_entry = simplejson.loads(existing_entry) + cache_entry = json.loads(existing_entry) except ValueError: #file doesn't have a valid entry, we'll recreate it pass cache_entry[cache_key] = cache_value try: with NamedTemporaryFile(delete=False) as tf: - tf.write(simplejson.dumps(cache_entry) + '\n') + tf.write(json.dumps(cache_entry) + '\n') os.rename(tf.name, cache_file) finally: try: diff --git a/swift/obj/replicator.py b/swift/obj/replicator.py index 89d157aa09..934df82ec5 100644 --- a/swift/obj/replicator.py +++ b/swift/obj/replicator.py @@ -32,7 +32,8 @@ from eventlet.support.greenlets import GreenletExit from swift.common.ring import Ring from swift.common.utils import whataremyips, unlink_older_than, lock_path, \ - compute_eta, get_logger, write_pickle, renamer, dump_recon_cache + compute_eta, get_logger, write_pickle, renamer, dump_recon_cache, \ + TRUE_VALUES from swift.common.bufferedhttp import http_connect from swift.common.daemon import Daemon @@ -244,7 +245,7 @@ class ObjectReplicator(Daemon): self.http_timeout = int(conf.get('http_timeout', 60)) self.lockup_timeout = int(conf.get('lockup_timeout', 1800)) self.recon_enable = conf.get( - 'recon_enable', 'no').lower() in ('yes', 'true', 'on', '1') + 'recon_enable', 'no').lower() in TRUE_VALUES self.recon_cache_path = conf.get( 'recon_cache_path', '/var/cache/swift') self.recon_object = os.path.join(self.recon_cache_path, "object.recon")