Add ability to stop drivers

This adds an optional method to the driver interface that will stop a
driver when the scheduler's connections object is stopped. This is
useful to clean up drivers that have state that are not connections. In
particular it prevents timers from leaking apscheduler threads all over
the test suite.

For defensive purposes we add code to start a new apsched within the
driver if the driver has been previously stopped. This is because
without having connection objects which can be replaced the driver may
stay in use though it currently only stops when the scheduler stops.

Change-Id: Ibfc638d88c77f18bdc9f9509776a665a9edfc2ae
This commit is contained in:
Clark Boylan 2017-04-18 13:59:01 -07:00
parent afc49ea495
commit fa65180541
3 changed files with 20 additions and 1 deletions

View File

@ -68,6 +68,17 @@ class Driver(object):
"""
pass
def stop(self):
"""Stop the driver from running.
This method is optional; the base implementation does nothing.
This method is called when the connection registry is stopped
allowing you additionally stop any running Driver computation
not specific to a connection.
"""
pass
@six.add_metaclass(abc.ABCMeta)
class ConnectionInterface(object):

View File

@ -38,6 +38,10 @@ class TimerDriver(Driver, TriggerInterface):
def reconfigure(self, tenant):
self._removeJobs(tenant)
if not self.apsched:
# Handle possible reuse of the driver without connection objects.
self.apsched = BackgroundScheduler()
self.apsched.start()
self._addJobs(tenant)
def _removeJobs(self, tenant):
@ -87,7 +91,9 @@ class TimerDriver(Driver, TriggerInterface):
self.sched.addEvent(event)
def stop(self):
self.apsched.shutdown()
if self.apsched:
self.apsched.shutdown()
self.apsched = None
def getTrigger(self, connection_name, config=None):
return timertrigger.TimerTrigger(self, config)

View File

@ -67,6 +67,8 @@ class ConnectionRegistry(object):
def stop(self):
for connection_name, connection in self.connections.items():
connection.onStop()
for driver in self.drivers.values():
driver.stop()
def configure(self, config):
# Register connections from the config