ADD signal handling for graceful shutdown in service.py

This commit is contained in:
2025-12-18 01:21:44 +00:00
parent 36868a08ec
commit 7b903fb27e
2 changed files with 26 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import os
import datetime
import signal
import time
import obsws_python as obs
import subprocess
@@ -10,6 +11,7 @@ from rewind.paths import load_state, write_state
INTERVAL = 10
MAX_AGE_SECONDS = 60 * 60 * 1
running = True
def open_obs():
subprocess.Popen(["obs", "--minimize-to-tray"])
@@ -39,7 +41,6 @@ def cleanup_old_files(directory: str, max_age_seconds: int) -> None:
os.remove(file_path)
print(f"Removed old file: {file_path}")
def create_state_file() -> None:
state = {"files": []}
write_state(state)
@@ -61,7 +62,14 @@ def add_file_to_state(file_path: str) -> None:
state["files"] = files
write_state(state)
def handle_shutdown(signum, frame):
global running
running = False
def main() -> None:
signal.signal(signal.SIGINT, handle_shutdown)
signal.signal(signal.SIGTERM, handle_shutdown)
open_obs()
time.sleep(5)
con = open_obs_connection()
@@ -74,7 +82,8 @@ def main() -> None:
current_files = os.listdir(recording_dir)
while True:
try:
while running:
cleanup_old_files(recording_dir, MAX_AGE_SECONDS)
new_files = os.listdir(recording_dir)
@@ -88,9 +97,10 @@ def main() -> None:
current_files = new_files
time.sleep(INTERVAL)
finally:
stop_recording(con)
con.disconnect()
print("Daemon stopped")
if __name__ == "__main__":
main()

View File

@@ -1,4 +1,4 @@
import os, subprocess, datetime, json
import os, subprocess
from rewind.paths import load_state