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 sys
import optparse import optparse
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
import simplejson try:
import simplejson as json
except ImportError:
import json
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
from swift.common.utils import get_logger, dump_recon_cache from swift.common.utils import get_logger, dump_recon_cache
@@ -44,17 +47,18 @@ def main():
try: try:
os.mkdir("/var/lock/swift-recon-object-cron") os.mkdir("/var/lock/swift-recon-object-cron")
except OSError as e: except OSError as e:
logger.critical("%s" % e) logger.critical(_(str(e)))
print str(e)
sys.exit(1) sys.exit(1)
asyncs = async_count(device_dir, logger) asyncs = async_count(device_dir, logger)
try: try:
dump_recon_cache('object_replication_time', total, cache_file) dump_recon_cache('async_pending', asyncs, cache_file)
except ValueError:
logger.exception(_('Exception decoding recon cache'))
except Exception: except Exception:
logger.exception(_('Exception dumping recon cache')) logger.exception(_('Exception dumping recon cache'))
try:
os.rmdir("/var/lock/swift-recon-object-cron") os.rmdir("/var/lock/swift-recon-object-cron")
except Exception:
logger.exception(_('Exception remove cronjob lock'))
if __name__ == '__main__': if __name__ == '__main__':
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.utils import split_path, cache_from_env, get_logger
from swift.common.constraints import check_mount from swift.common.constraints import check_mount
from hashlib import md5 from hashlib import md5
try:
import simplejson as json import simplejson as json
except ImportError:
import json
import os import os

View File

@@ -34,7 +34,10 @@ from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \
RawConfigParser RawConfigParser
from optparse import OptionParser from optparse import OptionParser
from tempfile import mkstemp, NamedTemporaryFile from tempfile import mkstemp, NamedTemporaryFile
import simplejson try:
import simplejson as json
except ImportError:
import json
import cPickle as pickle import cPickle as pickle
import glob import glob
from urlparse import urlparse as stdlib_urlparse, ParseResult 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: try:
existing_entry = cf.readline() existing_entry = cf.readline()
if existing_entry: if existing_entry:
cache_entry = simplejson.loads(existing_entry) cache_entry = json.loads(existing_entry)
except ValueError: except ValueError:
#file doesn't have a valid entry, we'll recreate it #file doesn't have a valid entry, we'll recreate it
pass pass
cache_entry[cache_key] = cache_value cache_entry[cache_key] = cache_value
try: try:
with NamedTemporaryFile(delete=False) as tf: 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) os.rename(tf.name, cache_file)
finally: finally:
try: try:

View File

@@ -32,7 +32,8 @@ from eventlet.support.greenlets import GreenletExit
from swift.common.ring import Ring from swift.common.ring import Ring
from swift.common.utils import whataremyips, unlink_older_than, lock_path, \ 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.bufferedhttp import http_connect
from swift.common.daemon import Daemon from swift.common.daemon import Daemon
@@ -244,7 +245,7 @@ class ObjectReplicator(Daemon):
self.http_timeout = int(conf.get('http_timeout', 60)) self.http_timeout = int(conf.get('http_timeout', 60))
self.lockup_timeout = int(conf.get('lockup_timeout', 1800)) self.lockup_timeout = int(conf.get('lockup_timeout', 1800))
self.recon_enable = conf.get( 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( self.recon_cache_path = conf.get(
'recon_cache_path', '/var/cache/swift') 'recon_cache_path', '/var/cache/swift')
self.recon_object = os.path.join(self.recon_cache_path, "object.recon") self.recon_object = os.path.join(self.recon_cache_path, "object.recon")