Update pagebot.py

This commit is contained in:
majmongoose 2024-10-25 10:50:56 -04:00
parent 94423eaa9a
commit d990773319

View File

@ -5,6 +5,7 @@ import subprocess
import time import time
import secrets_file import secrets_file
import threading import threading
import psutil
from datetime import datetime from datetime import datetime
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
@ -94,6 +95,17 @@ async def upload_to_discord(mp4_file,text):
print(f"Could not find channel with ID {channel}") print(f"Could not find channel with ID {channel}")
def kill_existing_ttd_instances():
"""Terminate all running instances of TTD."""
for proc in psutil.process_iter(['name', 'exe', 'cmdline']):
try:
if 'ttd' in proc.name().lower() or 'ttd' in ' '.join(proc.cmdline()).lower():
print("Terminating an existing instance of TTD...")
proc.terminate() # Send SIGTERM
proc.wait() # Wait for the process to terminate
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def launch_and_watch(program_path): def launch_and_watch(program_path):
program_directory = os.path.dirname(program_path) program_directory = os.path.dirname(program_path)
restart_time = secrets_file.restart_time # Format: "HH:MM" or None restart_time = secrets_file.restart_time # Format: "HH:MM" or None
@ -102,14 +114,10 @@ def launch_and_watch(program_path):
restart_hour, restart_minute = map(int, restart_time.split(":")) restart_hour, restart_minute = map(int, restart_time.split(":"))
restart_triggered = False # Flag to prevent multiple restarts within the same minute restart_triggered = False # Flag to prevent multiple restarts within the same minute
process = None # Initialize the process variable outside the loop
while True: while True:
# If a process is already running, terminate it before starting a new one # Terminate any existing TTD processes before starting a new one
if process and process.poll() is None: kill_existing_ttd_instances()
print("Terminating the existing TTD process before restarting...")
process.terminate()
process.wait()
# Launch the TTD program # Launch the TTD program
print("Starting TTD program...") print("Starting TTD program...")
@ -143,7 +151,6 @@ def launch_and_watch(program_path):
print("Waiting before relaunching TTD...") print("Waiting before relaunching TTD...")
time.sleep(2) time.sleep(2)
if __name__ == "__main__": if __name__ == "__main__":
## initialize watchdogs ## initialize watchdogs