ADD signal handling for graceful shutdown in service.py
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -10,6 +11,7 @@ from rewind.paths import load_state, write_state
|
|||||||
|
|
||||||
INTERVAL = 10
|
INTERVAL = 10
|
||||||
MAX_AGE_SECONDS = 60 * 60 * 1
|
MAX_AGE_SECONDS = 60 * 60 * 1
|
||||||
|
running = True
|
||||||
|
|
||||||
def open_obs():
|
def open_obs():
|
||||||
subprocess.Popen(["obs", "--minimize-to-tray"])
|
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)
|
os.remove(file_path)
|
||||||
print(f"Removed old file: {file_path}")
|
print(f"Removed old file: {file_path}")
|
||||||
|
|
||||||
|
|
||||||
def create_state_file() -> None:
|
def create_state_file() -> None:
|
||||||
state = {"files": []}
|
state = {"files": []}
|
||||||
write_state(state)
|
write_state(state)
|
||||||
@@ -61,7 +62,14 @@ def add_file_to_state(file_path: str) -> None:
|
|||||||
state["files"] = files
|
state["files"] = files
|
||||||
write_state(state)
|
write_state(state)
|
||||||
|
|
||||||
|
def handle_shutdown(signum, frame):
|
||||||
|
global running
|
||||||
|
running = False
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
signal.signal(signal.SIGINT, handle_shutdown)
|
||||||
|
signal.signal(signal.SIGTERM, handle_shutdown)
|
||||||
|
|
||||||
open_obs()
|
open_obs()
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
con = open_obs_connection()
|
con = open_obs_connection()
|
||||||
@@ -74,7 +82,8 @@ def main() -> None:
|
|||||||
|
|
||||||
current_files = os.listdir(recording_dir)
|
current_files = os.listdir(recording_dir)
|
||||||
|
|
||||||
while True:
|
try:
|
||||||
|
while running:
|
||||||
cleanup_old_files(recording_dir, MAX_AGE_SECONDS)
|
cleanup_old_files(recording_dir, MAX_AGE_SECONDS)
|
||||||
|
|
||||||
new_files = os.listdir(recording_dir)
|
new_files = os.listdir(recording_dir)
|
||||||
@@ -88,9 +97,10 @@ def main() -> None:
|
|||||||
|
|
||||||
current_files = new_files
|
current_files = new_files
|
||||||
time.sleep(INTERVAL)
|
time.sleep(INTERVAL)
|
||||||
|
finally:
|
||||||
stop_recording(con)
|
stop_recording(con)
|
||||||
con.disconnect()
|
con.disconnect()
|
||||||
|
print("Daemon stopped")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import os, subprocess, datetime, json
|
import os, subprocess
|
||||||
|
|
||||||
from rewind.paths import load_state
|
from rewind.paths import load_state
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user