added further handling for lock file, added some extra crash condition, doing less retrying

This commit is contained in:
jenz 2025-06-06 21:31:23 +02:00
parent 5e587b1536
commit f5d8b6d584

View File

@ -14,7 +14,7 @@ import datetime
import time
import glob
restart_time = datetime.datetime.now() + datetime.timedelta(hours=5)
restart_time = datetime.datetime.now() + datetime.timedelta(hours=3)
whoami = subprocess.getoutput(["whoami"])
with open(f'/home/{whoami}/ze_runner/config.json') as jsonfile:
@ -32,11 +32,8 @@ def writeCfgInput(Input_user):
#print("wrote to file: ", Input_user)
if "connect " in Input_user:
time.sleep(1.0)
open(looptestPath, 'w').close() #clearing file.
elif "wait" in Input_user:
time.sleep(0.35)
else:
time.sleep(0.5)
time.sleep(0.35)
open(looptestPath, 'w').close() #clearing file.
def close_observer(observer):
@ -152,6 +149,7 @@ def restart_sdl_and_steam():
kill_owned_process("pidof xterm")
subprocess.getoutput([f'vncserver -kill']) #only displays vncservers for the specific user.
subprocess.getoutput([f'vncserver -list -cleanstale']) #cleans stale sessions by removing files from disk
time.sleep(5)
cmd = f'vncserver -localhost no -geometry 1x1 -depth 24'
@ -176,13 +174,13 @@ def cpulimit_pid_of_game():
cmd = f"cpulimit --pid={pid} --limit=35 --background > /dev/null 2>&1"
subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE).communicate()[0]
def delete_lock_file():
#just delete the tmp source_engine .lock file here so multiple instances can run at same time.
subprocess.Popen(["rm -f /tmp/source_engine*.lock"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
def my_file_created_function(event_path):
#print(f"New file created: {event_path}")
if not event_path.lower().endswith(".bsp.bz2"):
if event_path.startswith("/tmp/source_engine") and event_path.endswith(".lock"):
subprocess.Popen([f"rm -f {event_path}"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
return
if not event_path.lower().endswith(".bsp.bz2") or event_path.lower().startswith("/tmp/"): #dont do anything with /tmp/ files except the .lock file
return
file_size = None
@ -191,22 +189,24 @@ def my_file_created_function(event_path):
#print('stdout: ', stdout)
#print('stderr: ', stderr)
if stderr:
print('finished downloading the bz2 file. Reconnecting client again.')
writeCfgInput("retry;")
print('finished downloading the bz2 file')
break
user = stdout.decode().split("autismbots")[0]
if whoami not in user:
print("disconnected from server until other user finished downloading bz2 file")
writeCfgInput("disconnect;")
time.sleep(5)
continue
#in case the bz2 download is not progressing just delete the file and disconnect.
cur_file_size = stdout.decode().split("autismbots")[1].strip().split(" ")[0]
print('cur_file_size: ', cur_file_size, ' stdout decode: ', stdout.decode())
if file_size == cur_file_size:
print("Aborting connection. file download is stuck.")
writeCfgInput("disconnect;")
subprocess.Popen(["rm", event_path], stdout=subprocess.DEVNULL).communicate()[0]
time.sleep(2)
writeCfgInput("disconnect;")
break
file_size = cur_file_size
time.sleep(10)
@ -215,6 +215,16 @@ class NewFileHandler(FileSystemEventHandler):
def on_created(self, event):
my_file_created_function(event.src_path)
def handle_temp_files():
#mostly its just downloading maps that we will be doing
root_dir = f"/tmp"
event_handler = NewFileHandler()
observer = Observer()
# Schedule the observer to watch the root directory and its subdirectories recursively
observer.schedule(event_handler, root_dir, recursive=True)
observer.start()
return observer
def handle_bz2_files():
#mostly its just downloading maps that we will be doing
root_dir = f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download"
@ -232,8 +242,8 @@ if __name__ == '__main__':
buffer_size = 4096 #potentially not large enough?
clean_up_files()
delete_lock_file()
observer = handle_bz2_files()
observer1 = handle_temp_files()
maps_folder_size = subprocess.Popen(["du", "-sh", f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps"], stdout=subprocess.PIPE).communicate()[0].decode().split("\t")[0]
#deleting when maps folder larger than some GB
@ -279,7 +289,7 @@ if __name__ == '__main__':
#print('data: ', data)
if not data:
continue
delete_lock_file()
#print("ip: ", ip, " port: ", port)
if ip == data_ports['discord_bot_ip'] and port == external_port_messages:
if messager_name in data:
@ -305,10 +315,11 @@ if __name__ == '__main__':
is_bot_connected_to_ze2 = True
overwrite_file_access()
elif "connect to ze" == data or ("connect to ze2" == data and not is_bot_connected_to_ze2):
if datetime.datetime.now() >= restart_time:
if datetime.datetime.now() >= restart_time or return_user_owned_pid("pidof cstrike_linux64") is None: #the game died
kill_owned_process("pidof cstrike_linux64")
sock.close()
close_observer(observer)
close_observer(observer1)
print('exiting after running the game for several hours.')
sys.exit(1)
else:
@ -334,3 +345,4 @@ if __name__ == '__main__':
finally:
sock.close()
close_observer(observer)
close_observer(observer1)