sjstein / R8DIUM

A discord bot to manage individual run8 users
GNU General Public License v3.0
2 stars 0 forks source link

Feature request /restart_run8 #77

Closed jackfruh closed 1 month ago

jackfruh commented 8 months ago

Feature is self explaining.

From an implementation perspective the simplest form would be something like taskkill -IM "Run-8 Train Simulator V3.exe /T followed by a batch file that starts up Run8 servers.

In practice, it would be great if we could task kill only one Run8 process, since there will be more than one running on an average server.

ChatGPT suggested the psutil python library for this and provided this sample code: import psutil

def find_executable_path(process_name): for process in psutil.process_iter(attrs=['pid', 'name']): if process.info['name'] == process_name: try: return psutil.Process(process.info['pid']).exe() except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass

return None

Replace 'your_process_name' with the name of the running process you want to find

executable_path = find_executable_path('your_process_name')

if executable_path: print(f"Path to the running executable: {executable_path}") else: print("The specified process is not running or the path could not be determined.")

I suspect you could tweak this to look for the path of Run8 that's associated with the bot that received the restart_run8 command. In that way, you could selectively kill and restart one Run8 process without disrupting others.

This might be the single most useful piece, as this would allow a "first attempt" restart by a larger range of admins than just those with RDP access. Additionally since the process flow is known, it lowers the risk to the server owner, when compared to RDP access. 

Darthyoda714 commented 7 months ago

The title caught my attention and would be an impressive feature if possible. Run8 does have an auto server start which works pretty well from my testing, that works with adding a target to a windows shortcut for the .exe file, so restarting the server, after checking it's down and killing the task, would theoretically just be running that shortcut.

sjstein commented 7 months ago

I'll look into this a bit more. @Darthyoda714 - can you tell me what the task names are each server when you are running them both? I'm trying to figure out how to differentiate.

jackfruh commented 7 months ago

pip install psutil

import psutil

# List all processes
for proc in psutil.process_iter(['pid', 'name', 'exe']):
    try:
        # Print process ID, Name, and Executable path
        if (proc.info['name'] == "Run-8 Train Simulator V3.exe") :
            print(f"PID: {proc.info['pid']}, Name: {proc.info['name']}, Executable: {proc.info['exe']}")
            if (proc.info['exe'] == "C:\Run8Studios\SOUTHWEST\Run8 Train Simulator V3\Run-8 Train Simulator V3.exe"):
                process = psutil.Process(proc.info['pid'])
                process.terminate()
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
        pass  # This can happen if the process terminates or is not accessible

The above code should kill run8 that's running in the SOUTHWEST folder, while leaving other Run8 processes alone.

Darthyoda714 commented 7 months ago

I'll look into this a bit more. @Darthyoda714 - can you tell me what the task names are each server when you are running them both? I'm trying to figure out how to differentiate.

It looks like the task names are both Run 8 Train Simulator III, process names are both Run-8 Train Simulator V3.exe, with one of the processes having Run8 Train Simulator Version 3 in the description while the other one doesn't. Host is running windows 7 at the moment for reference.

sjstein commented 7 months ago

I'll look into this a bit more. @Darthyoda714 - can you tell me what the task names are each server when you are running them both? I'm trying to figure out how to differentiate.

It looks like the task names are both Run 8 Train Simulator III, process names are both Run-8 Train Simulator V3.exe, with one of the processes having Run8 Train Simulator Version 3 in the description while the other one doesn't. Host is running windows 7 at the moment for reference.

Thanks - in hindsight, given what Jack found, I don't think I really need the task ID per-se, as the task manager seems to store the source directory for the particular instance.

I'm waiting on adding this functionality until the current main development branch (91) is merged into the master. That's gonna be when @Garrisonsan is able to test on his end and approve the merge. That branch will incorporate the multi-server change you had requested, along with the ability to mark users as inactive if they are not logged in for a certain duration.

Once that is approved and merged and a new release made, I'll start work on the restart feature. Things were getting a bit disjointed in my head trying to maintain more than one dev branch and I got myself into a mess a few weeks ago - so going to keep to one release branch and one dev branch for now.

sjstein commented 1 month ago

This issue was resolved with #120