From 48c51fe9cd74d75125d4ee2541fe8a8d5e7462c8 Mon Sep 17 00:00:00 2001 From: Duc Truong Date: Thu, 12 Apr 2018 18:21:42 +0000 Subject: [PATCH] Add stop_on_exception to TG timers ThreadGroup add_dynamic_timer_args and add_timer_args use DynamicLoopingCall and FixedIntervalLoopingCall respectively. Both classes have support for stop_on_exception, but this parameter was not exposed in ThreadGroup functions to create timers. This change adds the missing stop_on_exception to the timer functions so that ThreadGroup timers can continue on exceptions if the user chooses to do so. Change-Id: If03276f290e86e95ddc0b1d749b7460ed752b8ef Co-Authored-By: Zane Bitter --- oslo_service/tests/test_threadgroup.py | 5 +++-- oslo_service/threadgroup.py | 11 +++++++---- .../timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml diff --git a/oslo_service/tests/test_threadgroup.py b/oslo_service/tests/test_threadgroup.py index a4ab26c0..81ca9d40 100644 --- a/oslo_service/tests/test_threadgroup.py +++ b/oslo_service/tests/test_threadgroup.py @@ -55,7 +55,8 @@ class ThreadGroupTestCase(test_base.BaseTestCase): self.tg.add_dynamic_timer_args(foo, ['arg'], {'kwarg': 'kwarg'}, initial_delay=1, - periodic_interval_max=2) + periodic_interval_max=2, + stop_on_exception=False) self.assertEqual(1, len(self.tg.timers)) @@ -83,7 +84,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase): pass self.tg.add_timer_args(1, foo, ['arg'], {'kwarg': 'kwarg'}, - initial_delay=1) + initial_delay=1, stop_on_exception=False) self.assertEqual(1, len(self.tg.timers)) diff --git a/oslo_service/threadgroup.py b/oslo_service/threadgroup.py index bcb79d0b..f64c10fa 100644 --- a/oslo_service/threadgroup.py +++ b/oslo_service/threadgroup.py @@ -89,12 +89,14 @@ class ThreadGroup(object): periodic_interval_max=periodic_interval_max) def add_dynamic_timer_args(self, callback, args=None, kwargs=None, - initial_delay=None, periodic_interval_max=None): + initial_delay=None, periodic_interval_max=None, + stop_on_exception=True): args = args or [] kwargs = kwargs or {} timer = loopingcall.DynamicLoopingCall(callback, *args, **kwargs) timer.start(initial_delay=initial_delay, - periodic_interval_max=periodic_interval_max) + periodic_interval_max=periodic_interval_max, + stop_on_exception=stop_on_exception) self.timers.append(timer) return timer @@ -109,12 +111,13 @@ class ThreadGroup(object): initial_delay=initial_delay) def add_timer_args(self, interval, callback, args=None, kwargs=None, - initial_delay=None): + initial_delay=None, stop_on_exception=True): args = args or [] kwargs = kwargs or {} pulse = loopingcall.FixedIntervalLoopingCall(callback, *args, **kwargs) pulse.start(interval=interval, - initial_delay=initial_delay) + initial_delay=initial_delay, + stop_on_exception=stop_on_exception) self.timers.append(pulse) return pulse diff --git a/releasenotes/notes/timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml b/releasenotes/notes/timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml new file mode 100644 index 00000000..87b58fed --- /dev/null +++ b/releasenotes/notes/timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The ThreadGroup add_timer_args() and add_dynamic_timer_args() methods now + support passing a stop_on_exception=False argument to allow the timer to + keep running even when an exception is raised by the callback function.