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,12 +32,9 @@ 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)
open(looptestPath, 'w').close() #clearing file.
time.sleep(0.35)
open(looptestPath, 'w').close() #clearing file.
def close_observer(observer):
observer.stop()
@ -47,13 +44,13 @@ def clean_up_files():
#deleting POSIX shared memory objects, as long as one process has them open they exist. THis is to prevent /dev/shm from being full
#due to steam child processes.
#even with steam turned offline might there be chromium web browsers left over from steam who still hold processes open.
#only kind of potential issues from this is steam cloud being out of sync, which is really fucking irrelevant.
#only kind of potential issues from this is steam cloud being out of sync, which is really fucking irrelevant.
subprocess.Popen(["rm -rf /tmp/steam*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /tmp/dbus*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /tmp/pressure*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /tmp/tigervnc*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /tmp/dumps*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /dev/shm/u100*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
subprocess.Popen(["rm -rf /dev/shm/u100*"], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()[0]
#users should only have permissions to delete their own files probably
#check if any bz2 files left over.
@ -105,7 +102,7 @@ def bot_process_movement(input_line):
strInput += "use weapon_p90; wait 2; -forward; wait 2;"
elif state == 1:
strInput += "wait 2; use weapon_elite; wait 3; +forward; wait 2;"
elif state == 2:
elif state == 2:
strInput += "-forward; wait 2; use weapon_elite; wait 3;"
elif state == 8:
strInput += "use weapon_knife; wait 5; +forward; wait 2;"
@ -152,8 +149,9 @@ 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'
#cmd = f'vncserver -localhost no -geometry 800x800 -depth 24'
#print(f'cmd: {cmd}')
@ -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
@ -190,23 +188,25 @@ def my_file_created_function(event_path):
stdout, stderr = subprocess.Popen(["ls", "-l", event_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
#print('stdout: ', stdout)
#print('stderr: ', stderr)
if stderr:
print('finished downloading the bz2 file. Reconnecting client again.')
writeCfgInput("retry;")
break
if stderr:
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,12 +242,12 @@ 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
if maps_folder_size.endswith("G"):
if maps_folder_size.endswith("G"):
maps_folder_size = float(maps_folder_size[:-1])
if maps_folder_size > 40.0:
for f in glob.glob(f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/*"):
@ -279,13 +289,13 @@ 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:
messager_name = ""
response_msg = f"""say {messager_name} {data}"""
print("remote UDP packet response_msg: ", response_msg)
print("remote UDP packet response_msg: ", response_msg)
writeCfgInput(response_msg)
if ip != data_ports['ovh_ip']:
continue
@ -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)