forked from platypush/platypush
Added SIGTERM handler for clean termination in Docker contexts.
This commit is contained in:
parent
4dd713ffd2
commit
e6b5abe909
1 changed files with 15 additions and 2 deletions
|
@ -1,4 +1,6 @@
|
|||
import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
from threading import Thread
|
||||
from typing import Optional
|
||||
|
@ -23,6 +25,7 @@ class ApplicationRunner:
|
|||
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
self.logger = logging.getLogger('platypush:runner')
|
||||
self._proc: Optional[ApplicationProcess] = None
|
||||
self._stream: Optional[CommandStream] = None
|
||||
|
||||
def _listen(self, stream: CommandStream):
|
||||
"""
|
||||
|
@ -48,12 +51,16 @@ class ApplicationRunner:
|
|||
if parsed_args.version:
|
||||
self._print_version()
|
||||
|
||||
signal.signal(signal.SIGTERM, lambda *_: self.stop())
|
||||
|
||||
while True:
|
||||
with CommandStream(parsed_args.ctrl_sock) as stream, ApplicationProcess(
|
||||
with CommandStream(
|
||||
parsed_args.ctrl_sock
|
||||
) as self._stream, ApplicationProcess(
|
||||
*args, pidfile=parsed_args.pidfile, timeout=self._default_timeout
|
||||
) as self._proc:
|
||||
try:
|
||||
self._listen(stream)
|
||||
self._listen(self._stream)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
@ -63,6 +70,8 @@ class ApplicationRunner:
|
|||
|
||||
break
|
||||
|
||||
self._stream = None
|
||||
|
||||
def run(self, *args: str) -> None:
|
||||
try:
|
||||
self._run(*args)
|
||||
|
@ -73,6 +82,10 @@ class ApplicationRunner:
|
|||
if self._proc is not None:
|
||||
self._proc.stop()
|
||||
|
||||
if self._stream and self._stream.pid:
|
||||
os.kill(self._stream.pid, signal.SIGKILL)
|
||||
self._stream = None
|
||||
|
||||
def restart(self):
|
||||
if self._proc is not None:
|
||||
self._proc.mark_for_restart()
|
||||
|
|
Loading…
Reference in a new issue