simplejson import and exception/logging fixes

This commit is contained in:
Florian Hines 2011-09-01 13:46:13 -05:00
parent 9b276ad74b
commit e9b5cb83ac
4 changed files with 24 additions and 13 deletions

View File

@ -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()

View File

@ -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

View File

@ -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:

View File

@ -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")