better server-ready event: notify waiting thread if server really ready (communication ready) or failed to start

pull/1577/head
sebres 2016-10-14 22:32:02 +02:00
parent c809c3e61e
commit 98f87a1a52
3 changed files with 13 additions and 2 deletions

View File

@ -184,13 +184,19 @@ class Fail2banServer(Fail2banCmdLine):
logSys.log(5, ' server phase %s', phase)
if not phase.get('start', False):
raise ServerExecutionException('Async configuration of server failed')
# event for server ready flag:
def _server_ready():
phase['start-ready'] = True
logSys.log(5, ' server phase %s', phase)
# notify waiting thread if server really ready
self._conf['onstart'] = _server_ready
# Start server, daemonize it, etc.
pid = os.getpid()
server = Fail2banServer.startServerDirect(self._conf, background)
# notify waiting thread server ready resp. done (background execution, error case, etc):
if not async:
phase['start-ready'] = True
logSys.log(5, ' server phase %s', phase)
_server_ready()
# If forked - just exit other processes
if pid != os.getpid(): # pragma: no cover
os._exit(0)

View File

@ -147,6 +147,7 @@ class AsyncServer(asyncore.dispatcher):
self.__sock = "/var/run/fail2ban/fail2ban.sock"
self.__init = False
self.__active = False
self.onstart = None
##
# Returns False as we only read the socket first.
@ -196,6 +197,9 @@ class AsyncServer(asyncore.dispatcher):
self.listen(1)
# Sets the init flag.
self.__init = self.__loop = self.__active = True
# Execute on start event (server ready):
if self.onstart:
self.onstart()
# Event loop as long as active:
loop(lambda: self.__loop, use_poll=use_poll)
self.__active = False

View File

@ -148,6 +148,7 @@ class Server:
logSys.debug("Starting communication")
try:
self.__asyncServer = AsyncServer(self.__transm)
self.__asyncServer.onstart = conf.get('onstart')
self.__asyncServer.start(sock, force)
except AsyncServerException as e:
logSys.error("Could not start server: %s", e)