Merge pull request #26 from notmyname/error_handling

added error checks to log uploader
This commit is contained in:
David 2012-01-17 08:47:54 -08:00
commit 987339c5d2
2 changed files with 31 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import gzip
import re
import sys
from paste.deploy import appconfig
import zlib
from slogging.internal_proxy import InternalProxy
from swift.common.daemon import Daemon
@ -143,14 +144,22 @@ class LogUploader(Daemon):
return
for filename, match in filename2match.items():
# don't process very new logs
seconds_since_mtime = time.time() - os.stat(filename).st_mtime
try:
seconds_since_mtime = time.time() - os.stat(filename).st_mtime
except OSError:
# filename wasn't found, skip it
continue
if seconds_since_mtime < self.new_log_cutoff:
self.logger.debug(_("Skipping log: %(file)s "
"(< %(cutoff)d seconds old)") % {
'file': filename,
'cutoff': self.new_log_cutoff})
continue
self.upload_one_log(filename, **match)
try:
self.upload_one_log(filename, **match)
except Exception:
self.logger.exception(
_('ERROR: could not upload %s') % filename)
def upload_one_log(self, filename, year, month, day, hour):
"""

View File

@ -81,6 +81,12 @@ class MockLogUploader(_orig_LogUploader):
day, hour)
class ErrorLogUploader(MockLogUploader):
def upload_one_log(self, filename, year, month, day, hour):
raise OSError('foo bar')
class TestLogUploader(unittest.TestCase):
def setUp(self):
@ -94,6 +100,20 @@ class TestLogUploader(unittest.TestCase):
log_uploader.appconfig = self._orig_appconfig
log_uploader.InternalProxy = self._orig_InternalProxy
def test_bad_upload(self):
files = [datetime.now().strftime('%Y%m%d%H')]
with temptree(files, contents=[COMPRESSED_DATA] * len(files)) as t:
# invalid pattern
conf = {'log_dir': t,
'source_filename_pattern': '%Y%m%d%h'} # should be %H
uploader = MockLogUploader(conf)
self.assertRaises(SystemExit, uploader.upload_all_logs)
conf = {'log_dir': t, 'source_filename_pattern': access_regex}
uploader = ErrorLogUploader(conf)
# this tests if the exception is handled
uploader.upload_all_logs()
def test_bad_pattern_in_config(self):
files = [datetime.now().strftime('%Y%m%d%H')]
with temptree(files, contents=[COMPRESSED_DATA] * len(files)) as t: