FireBot/core.py

46 lines
1,012 B
Python
Raw Normal View History

2023-10-23 22:10:07 +00:00
#!/usr/bin/python3
from os import system
from time import sleep
from threading import Thread
2023-11-05 01:37:35 +00:00
from logs import log
2023-10-23 22:10:07 +00:00
def launch(server: str) -> None:
system(f"python3 -u ircbot.py {server}")
2023-10-23 22:10:07 +00:00
threads = {}
servers = [
"ircnow",
"replirc",
2023-11-17 22:47:18 +00:00
#"efnet",
2023-11-13 22:27:20 +00:00
"backupbox",
2023-10-23 22:10:07 +00:00
]
2023-11-05 01:37:35 +00:00
def is_dead(thr: Thread) -> bool:
2023-10-23 22:10:07 +00:00
thr.join(timeout=0)
return not thr.is_alive()
2023-10-28 04:20:18 +00:00
2023-11-05 01:37:35 +00:00
def start(server: str) -> Thread:
t = Thread(target=launch, args=(server,))
t.daemon = True
t.start()
return t
2023-10-23 22:10:07 +00:00
2023-10-28 04:20:18 +00:00
2023-10-23 22:10:07 +00:00
if __name__ == "__main__":
2023-11-05 01:37:35 +00:00
log("Begin initialization", "CORE")
2023-10-23 22:10:07 +00:00
for server in servers:
threads[server] = start(server)
2023-11-05 01:37:35 +00:00
log("Started all instances. Idling...", "CORE")
2023-10-23 22:10:07 +00:00
while 1:
sleep(60)
2023-11-05 01:37:35 +00:00
log("Running a checkup on all running instances", "CORE")
2023-10-23 22:10:07 +00:00
for server in threads:
t = threads[server]
if is_dead(t):
2023-11-05 01:37:35 +00:00
log(f"The thread for {server} died, restarting it...", "CORE", "WARN")
threads[server] = start(server)