cleanup after sigkill
This commit is contained in:
parent
86a866b2bf
commit
3d149faa42
1 changed files with 21 additions and 1 deletions
22
app.py
22
app.py
|
@ -3,12 +3,28 @@ import os
|
|||
import subprocess
|
||||
import signal
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
|
||||
mpv_pidfile = Path(tempfile.gettempdir()).joinpath("laas_mpv.pidfile")
|
||||
music_path = Path(os.environ.get("MUSIC_PATH", "./music"))
|
||||
mpv_process: subprocess.Popen | None = None
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def cleanup_unclean():
|
||||
if mpv_pidfile.is_file():
|
||||
print("Unclean shutdown detected")
|
||||
try:
|
||||
mpv_pid = int(open(mpv_pidfile, "r").read().strip())
|
||||
print(f"Killing mpv process with pid {mpv_pid}")
|
||||
os.kill(mpv_pid, signal.SIGTERM)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
mpv_pidfile.unlink()
|
||||
pass
|
||||
|
||||
|
||||
def shutdown(status: int = 0):
|
||||
playback_stop()
|
||||
os._exit(status)
|
||||
|
@ -28,8 +44,9 @@ def playback_start():
|
|||
"--shuffle",
|
||||
str(music_path),
|
||||
"--no-video",
|
||||
]
|
||||
],
|
||||
)
|
||||
open(mpv_pidfile, 'w').write(str(mpv_process.pid))
|
||||
|
||||
|
||||
def playback_stop():
|
||||
|
@ -38,6 +55,8 @@ def playback_stop():
|
|||
mpv_process.terminate()
|
||||
mpv_process.wait()
|
||||
mpv_process = None
|
||||
if mpv_pidfile.exists():
|
||||
mpv_pidfile.unlink()
|
||||
|
||||
|
||||
def is_playing():
|
||||
|
@ -122,6 +141,7 @@ def api_status():
|
|||
|
||||
signal.signal(signal.SIGTERM, sigh)
|
||||
signal.signal(signal.SIGINT, sigh)
|
||||
cleanup_unclean()
|
||||
|
||||
if not music_path.exists():
|
||||
music_path.mkdir()
|
||||
|
|
Loading…
Add table
Reference in a new issue