forked from platypush/platypush
Ensure that the application always terminates on Ctrl+C.
This commit is contained in:
parent
f75a2159c7
commit
4d582bb6bc
1 changed files with 23 additions and 9 deletions
|
@ -3,6 +3,7 @@ import os
|
||||||
import signal
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
from platypush.process import ControllableProcess
|
from platypush.process import ControllableProcess
|
||||||
|
|
||||||
|
@ -28,18 +29,31 @@ class ApplicationProcess(ControllableProcess):
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
self.logger.info('Starting application...')
|
self.logger.info('Starting application...')
|
||||||
|
app = None
|
||||||
|
|
||||||
|
try:
|
||||||
with subprocess.Popen(
|
with subprocess.Popen(
|
||||||
[sys.executable, '-m', 'platypush.app', *self.args],
|
[sys.executable, '-m', 'platypush.app', *self.args],
|
||||||
stdin=sys.stdin,
|
stdin=sys.stdin,
|
||||||
stdout=sys.stdout,
|
stdout=sys.stdout,
|
||||||
stderr=sys.stderr,
|
stderr=sys.stderr,
|
||||||
) as app:
|
) as app:
|
||||||
try:
|
|
||||||
app.wait()
|
app.wait()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if app and app.poll() is None:
|
||||||
|
app.terminate()
|
||||||
|
|
||||||
|
wait_start = time.time()
|
||||||
|
while app and app.poll() is None:
|
||||||
|
if time.time() - wait_start > 5:
|
||||||
|
self.logger.warning('Application did not terminate, killing it')
|
||||||
|
app.kill()
|
||||||
|
break
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
def on_stop(self):
|
def on_stop(self):
|
||||||
try:
|
try:
|
||||||
with open(self.pidfile, 'r') as f:
|
with open(self.pidfile, 'r') as f:
|
||||||
|
|
Loading…
Reference in a new issue