Browse Source

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 <zbitter@redhat.com>
Duc Truong 1 year ago
parent
commit
48c51fe9cd

+ 3
- 2
oslo_service/tests/test_threadgroup.py View File

@@ -55,7 +55,8 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
55 55
 
56 56
         self.tg.add_dynamic_timer_args(foo, ['arg'], {'kwarg': 'kwarg'},
57 57
                                        initial_delay=1,
58
-                                       periodic_interval_max=2)
58
+                                       periodic_interval_max=2,
59
+                                       stop_on_exception=False)
59 60
 
60 61
         self.assertEqual(1, len(self.tg.timers))
61 62
 
@@ -83,7 +84,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
83 84
             pass
84 85
 
85 86
         self.tg.add_timer_args(1, foo, ['arg'], {'kwarg': 'kwarg'},
86
-                               initial_delay=1)
87
+                               initial_delay=1, stop_on_exception=False)
87 88
 
88 89
         self.assertEqual(1, len(self.tg.timers))
89 90
 

+ 7
- 4
oslo_service/threadgroup.py View File

@@ -89,12 +89,14 @@ class ThreadGroup(object):
89 89
             periodic_interval_max=periodic_interval_max)
90 90
 
91 91
     def add_dynamic_timer_args(self, callback, args=None, kwargs=None,
92
-                               initial_delay=None, periodic_interval_max=None):
92
+                               initial_delay=None, periodic_interval_max=None,
93
+                               stop_on_exception=True):
93 94
         args = args or []
94 95
         kwargs = kwargs or {}
95 96
         timer = loopingcall.DynamicLoopingCall(callback, *args, **kwargs)
96 97
         timer.start(initial_delay=initial_delay,
97
-                    periodic_interval_max=periodic_interval_max)
98
+                    periodic_interval_max=periodic_interval_max,
99
+                    stop_on_exception=stop_on_exception)
98 100
         self.timers.append(timer)
99 101
         return timer
100 102
 
@@ -109,12 +111,13 @@ class ThreadGroup(object):
109 111
                                    initial_delay=initial_delay)
110 112
 
111 113
     def add_timer_args(self, interval, callback, args=None, kwargs=None,
112
-                       initial_delay=None):
114
+                       initial_delay=None, stop_on_exception=True):
113 115
         args = args or []
114 116
         kwargs = kwargs or {}
115 117
         pulse = loopingcall.FixedIntervalLoopingCall(callback, *args, **kwargs)
116 118
         pulse.start(interval=interval,
117
-                    initial_delay=initial_delay)
119
+                    initial_delay=initial_delay,
120
+                    stop_on_exception=stop_on_exception)
118 121
         self.timers.append(pulse)
119 122
         return pulse
120 123
 

+ 6
- 0
releasenotes/notes/timer-stop_on_exception-9f21d7c4d6d1b0d9.yaml View File

@@ -0,0 +1,6 @@
1
+---
2
+features:
3
+  - |
4
+    The ThreadGroup add_timer_args() and add_dynamic_timer_args() methods now
5
+    support passing a stop_on_exception=False argument to allow the timer to
6
+    keep running even when an exception is raised by the callback function.

Loading…
Cancel
Save