changed started_callback mechanism
- using threading.Events instead of Queue - started_callback has no more argument - introduced timeout for starting modules Change-Id: I5a8b59cf552918cf7e61ae93cda907f7b0d97836 Reviewed-on: https://forge.frm2.tum.de/review/20281 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
@@ -39,12 +39,6 @@ try:
|
||||
except ImportError:
|
||||
import ConfigParser as configparser # py2
|
||||
|
||||
try:
|
||||
from queue import Queue # py 3
|
||||
except ImportError:
|
||||
from Queue import Queue # py 2
|
||||
|
||||
|
||||
try:
|
||||
import daemon.pidlockfile as pidlockfile
|
||||
except ImportError:
|
||||
@@ -198,18 +192,14 @@ class Server(object):
|
||||
for modname, modobj in self.modules.items():
|
||||
modobj.init_module()
|
||||
|
||||
starting_modules = set()
|
||||
finished_modules = Queue()
|
||||
start_events = []
|
||||
for modname, modobj in self.modules.items():
|
||||
starting_modules.add(modobj)
|
||||
modobj.start_module(started_callback=finished_modules.put)
|
||||
# remark: it is the module implementors responsibility to call started_callback
|
||||
# within reasonable time (using timeouts). If we find later, that this is not
|
||||
# enough, we might insert checking for a timeout here, and somehow set the remaining
|
||||
# starting_modules to an error state.
|
||||
while starting_modules:
|
||||
finished = finished_modules.get()
|
||||
self.log.info(u'%s has started' % finished.name)
|
||||
# use discard instead of remove here, catching the case when started_callback is called twice
|
||||
starting_modules.discard(finished)
|
||||
finished_modules.task_done()
|
||||
event = threading.Event()
|
||||
# start_module must return either a timeout value or None (default 30 sec)
|
||||
timeout = modobj.start_module(started_callback=event.set) or 30
|
||||
start_events.append((time.time() + timeout, modname, event))
|
||||
self.log.info(u'waiting for modules being started')
|
||||
for deadline, modname, event in sorted(start_events):
|
||||
if not event.wait(timeout=max(0, deadline - time.time())):
|
||||
self.log.info('WARNING: timeout when starting module %s' % modname)
|
||||
self.log.info(u'all modules started')
|
||||
|
||||
Reference in New Issue
Block a user