From 1d7f8dcf635f8a919750c253de4e65c5530b1940 Mon Sep 17 00:00:00 2001 From: Justin Riley Date: Fri, 14 Apr 2017 14:11:10 -0400 Subject: [PATCH] catch and log all sync exceptions in daemon ...instead of crashing. --- onedns/server.py | 7 ++++--- onedns/tests/test_onedns.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/onedns/server.py b/onedns/server.py index 03fbaea..7f3c057 100644 --- a/onedns/server.py +++ b/onedns/server.py @@ -94,12 +94,13 @@ class OneDNS(resolver.DynamicResolver): test = kwargs.pop('test', False) test_vms = kwargs.pop('test_vms', None) sync_interval = kwargs.pop('sync_interval', 5 * 60) - self.sync(vms=test_vms) if self._udp_server is None or not self._udp_server.isAlive(): self.start(*args, **kwargs) - time.sleep(sync_interval) while self._udp_server.isAlive(): - self.sync(vms=test_vms) + try: + self.sync(vms=test_vms) + except Exception: + log.exception('onedns sync failed:') time.sleep(sync_interval) if test: break diff --git a/onedns/tests/test_onedns.py b/onedns/tests/test_onedns.py index 2f0ce28..5923062 100644 --- a/onedns/tests/test_onedns.py +++ b/onedns/tests/test_onedns.py @@ -1,5 +1,9 @@ +import mock + import pytest +from testfixtures import LogCapture + from onedns import exception from onedns.tests import vcr from onedns.tests import utils @@ -76,3 +80,14 @@ def test_onedns_remove_vm_by_id(oneclient, one_dns): _add_and_verify(one_dns, vm) one_dns.remove_vm_by_id(vm.id) utils.verify_vm_dns_absent(dns_entries) + + +def test_onedns_daemon_no_crash(one_dns): + def sync_fail(*args, **kwargs): + raise Exception('this should be logged') + with LogCapture() as log_capture: + with mock.patch.object(one_dns, 'sync', sync_fail): + one_dns.daemon(test=True, sync_interval=1) + log_capture.check( + ('onedns', 'ERROR', 'onedns sync failed:') + )