mirror of https://github.com/fail2ban/fail2ban
better server-ready event: notify waiting thread if server really ready (communication ready) or failed to start
parent
c809c3e61e
commit
98f87a1a52
|
@ -184,13 +184,19 @@ class Fail2banServer(Fail2banCmdLine):
|
||||||
logSys.log(5, ' server phase %s', phase)
|
logSys.log(5, ' server phase %s', phase)
|
||||||
if not phase.get('start', False):
|
if not phase.get('start', False):
|
||||||
raise ServerExecutionException('Async configuration of server failed')
|
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.
|
# Start server, daemonize it, etc.
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
server = Fail2banServer.startServerDirect(self._conf, background)
|
server = Fail2banServer.startServerDirect(self._conf, background)
|
||||||
|
# notify waiting thread server ready resp. done (background execution, error case, etc):
|
||||||
if not async:
|
if not async:
|
||||||
phase['start-ready'] = True
|
_server_ready()
|
||||||
logSys.log(5, ' server phase %s', phase)
|
|
||||||
# If forked - just exit other processes
|
# If forked - just exit other processes
|
||||||
if pid != os.getpid(): # pragma: no cover
|
if pid != os.getpid(): # pragma: no cover
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
|
@ -147,6 +147,7 @@ class AsyncServer(asyncore.dispatcher):
|
||||||
self.__sock = "/var/run/fail2ban/fail2ban.sock"
|
self.__sock = "/var/run/fail2ban/fail2ban.sock"
|
||||||
self.__init = False
|
self.__init = False
|
||||||
self.__active = False
|
self.__active = False
|
||||||
|
self.onstart = None
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns False as we only read the socket first.
|
# Returns False as we only read the socket first.
|
||||||
|
@ -196,6 +197,9 @@ class AsyncServer(asyncore.dispatcher):
|
||||||
self.listen(1)
|
self.listen(1)
|
||||||
# Sets the init flag.
|
# Sets the init flag.
|
||||||
self.__init = self.__loop = self.__active = True
|
self.__init = self.__loop = self.__active = True
|
||||||
|
# Execute on start event (server ready):
|
||||||
|
if self.onstart:
|
||||||
|
self.onstart()
|
||||||
# Event loop as long as active:
|
# Event loop as long as active:
|
||||||
loop(lambda: self.__loop, use_poll=use_poll)
|
loop(lambda: self.__loop, use_poll=use_poll)
|
||||||
self.__active = False
|
self.__active = False
|
||||||
|
|
|
@ -148,6 +148,7 @@ class Server:
|
||||||
logSys.debug("Starting communication")
|
logSys.debug("Starting communication")
|
||||||
try:
|
try:
|
||||||
self.__asyncServer = AsyncServer(self.__transm)
|
self.__asyncServer = AsyncServer(self.__transm)
|
||||||
|
self.__asyncServer.onstart = conf.get('onstart')
|
||||||
self.__asyncServer.start(sock, force)
|
self.__asyncServer.start(sock, force)
|
||||||
except AsyncServerException as e:
|
except AsyncServerException as e:
|
||||||
logSys.error("Could not start server: %s", e)
|
logSys.error("Could not start server: %s", e)
|
||||||
|
|
Loading…
Reference in New Issue