Proper self-healing
This commit is contained in:
parent
6909706f1e
commit
3ea5a3f7b6
3 changed files with 62 additions and 33 deletions
|
@ -3,11 +3,14 @@ from discord import Intents
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.utils import find
|
from discord.utils import find
|
||||||
from psutil import Process
|
from psutil import Process
|
||||||
from asyncio import sleep
|
import asyncio
|
||||||
|
|
||||||
from jarvis.config import get_config
|
from jarvis.config import get_config
|
||||||
from jarvis import utils
|
from jarvis import utils
|
||||||
|
|
||||||
|
if asyncio.get_event_loop().is_closed():
|
||||||
|
asyncio.set_event_loop(asyncio.new_event_loop())
|
||||||
|
|
||||||
intents = Intents.default()
|
intents = Intents.default()
|
||||||
intents.members = True
|
intents.members = True
|
||||||
|
|
||||||
|
@ -20,6 +23,7 @@ async def on_ready():
|
||||||
print(" Logged in as {0.user}".format(jarvis))
|
print(" Logged in as {0.user}".format(jarvis))
|
||||||
print(" Connected to {} guild(s)".format(len(jarvis.guilds)))
|
print(" Connected to {} guild(s)".format(len(jarvis.guilds)))
|
||||||
with jarvis_self.oneshot():
|
with jarvis_self.oneshot():
|
||||||
|
print(f" Current PID: {jarvis_self.pid}")
|
||||||
Path(f"jarvis.{jarvis_self.pid}.pid").touch()
|
Path(f"jarvis.{jarvis_self.pid}.pid").touch()
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,9 +37,9 @@ async def on_guild_join(guild):
|
||||||
+ "variety of tasks as best I can, "
|
+ "variety of tasks as best I can, "
|
||||||
+ "24 hours a day, seven days a week."
|
+ "24 hours a day, seven days a week."
|
||||||
)
|
)
|
||||||
await sleep(1)
|
await asyncio.sleep(1)
|
||||||
await general.send("Importing all preferences from home interface...")
|
await general.send("Importing all preferences from home interface...")
|
||||||
await sleep(5)
|
await asyncio.sleep(5)
|
||||||
await general.send("Systems are now fully operational")
|
await general.send("Systems are now fully operational")
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,4 +52,3 @@ def run():
|
||||||
+ "{}&permissions=8&scope=bot".format(config.client_id)
|
+ "{}&permissions=8&scope=bot".format(config.client_id)
|
||||||
)
|
)
|
||||||
jarvis.run(config.token, bot=True, reconnect=True)
|
jarvis.run(config.token, bot=True, reconnect=True)
|
||||||
Path(f"jarvis.{jarvis_self.pid}.pid").unlink()
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from jarvis.config import get_config
|
from jarvis.config import get_config
|
||||||
|
from jarvis.utils import get_prefix
|
||||||
|
|
||||||
|
|
||||||
class OwnerCog(commands.Cog):
|
class OwnerCog(commands.Cog):
|
||||||
|
@ -62,6 +63,22 @@ class OwnerCog(commands.Cog):
|
||||||
else:
|
else:
|
||||||
await ctx.send("I'm afraid I can't let you do that")
|
await ctx.send("I'm afraid I can't let you do that")
|
||||||
|
|
||||||
|
@commands.group(name="system", hidden=True, pass_context=True)
|
||||||
|
async def _system(self, ctx):
|
||||||
|
if ctx.invoked_subcommand is None:
|
||||||
|
await ctx.send(
|
||||||
|
f"Usage: `{ctx.message.content} <subcommand>`\n"
|
||||||
|
+ "Subcommands: `restart`, `update`"
|
||||||
|
)
|
||||||
|
|
||||||
|
@_system.command(name="restart", hidden=True)
|
||||||
|
async def _restart(self, ctx):
|
||||||
|
await ctx.send("Restarting core systems...")
|
||||||
|
try:
|
||||||
|
await self.bot.close()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_cog(OwnerCog(bot))
|
bot.add_cog(OwnerCog(bot))
|
||||||
|
|
51
run.py
51
run.py
|
@ -1,27 +1,22 @@
|
||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import jarvis
|
import jarvis
|
||||||
import os
|
|
||||||
import signal
|
|
||||||
import git
|
import git
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from multiprocessing import Process
|
from multiprocessing import Value, Process, freeze_support
|
||||||
from importlib import reload as ireload
|
from importlib import reload as ireload
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
while True:
|
||||||
ireload(jarvis)
|
ireload(jarvis)
|
||||||
jarvis.run()
|
jarvis.run()
|
||||||
|
|
||||||
|
|
||||||
jarvis_process = Process(target=run, name="jarvis")
|
def restart():
|
||||||
|
|
||||||
|
|
||||||
def start_process():
|
|
||||||
global jarvis_process
|
global jarvis_process
|
||||||
if jarvis_process.is_alive():
|
Path(get_pid_file()).unlink()
|
||||||
jarvis_process.terminate()
|
jarvis_process.terminate()
|
||||||
jarvis_process.join(10)
|
|
||||||
jarvis_process = Process(target=run, name="jarvis")
|
jarvis_process = Process(target=run, name="jarvis")
|
||||||
jarvis_process.start()
|
jarvis_process.start()
|
||||||
|
|
||||||
|
@ -42,9 +37,18 @@ def update():
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def get_pid_file():
|
||||||
|
return f"jarvis.{get_pid()}.pid"
|
||||||
|
|
||||||
|
|
||||||
|
def get_pid():
|
||||||
|
global jarvis_process
|
||||||
|
return jarvis_process.pid
|
||||||
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
|
pfile = Path(get_pid_file())
|
||||||
while not pid_file.exists():
|
while not pfile.exists():
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
print(
|
print(
|
||||||
"""
|
"""
|
||||||
|
@ -59,15 +63,16 @@ Command List:
|
||||||
while True:
|
while True:
|
||||||
cmd = input("> ")
|
cmd = input("> ")
|
||||||
if cmd.lower() in ["q", "quit", "e", "exit"]:
|
if cmd.lower() in ["q", "quit", "e", "exit"]:
|
||||||
pid_file.unlink()
|
print(" Shutting down core systems...")
|
||||||
|
pfile.unlink()
|
||||||
break
|
break
|
||||||
if cmd.lower() in ["u", "update"]:
|
if cmd.lower() in ["u", "update"]:
|
||||||
print("-> Updating core systems...")
|
print(" Updating core systems...")
|
||||||
status = update()
|
status = update()
|
||||||
if status == 0:
|
if status == 0:
|
||||||
start_process()
|
restart()
|
||||||
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
|
pfile = Path(get_pid_file())
|
||||||
while not pid_file.exists():
|
while not pfile.exists():
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
print(" Core systems successfully updated.")
|
print(" Core systems successfully updated.")
|
||||||
elif status == 1:
|
elif status == 1:
|
||||||
|
@ -76,15 +81,17 @@ Command List:
|
||||||
print(" Core updates available, but not applied.")
|
print(" Core updates available, but not applied.")
|
||||||
if cmd.lower() in ["r", "reload"]:
|
if cmd.lower() in ["r", "reload"]:
|
||||||
print(" Reloading core systems...")
|
print(" Reloading core systems...")
|
||||||
pid_file.unlink()
|
restart()
|
||||||
start_process()
|
pfile = Path(get_pid_file())
|
||||||
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
|
while not pfile.exists():
|
||||||
while not pid_file.exists():
|
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
print(" All systems reloaded.")
|
print(" All systems reloaded.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
freeze_support()
|
||||||
|
pid_file = Value("i", 0)
|
||||||
|
jarvis_process = Process(target=run, name="jarvis")
|
||||||
print("J.A.R.V.I.S. initializing....")
|
print("J.A.R.V.I.S. initializing....")
|
||||||
print(" Updating core systems...")
|
print(" Updating core systems...")
|
||||||
status = update()
|
status = update()
|
||||||
|
@ -94,8 +101,10 @@ if __name__ == "__main__":
|
||||||
print(" No core updates available.")
|
print(" No core updates available.")
|
||||||
elif status == 2:
|
elif status == 2:
|
||||||
print(" Core updates available, but not applied.")
|
print(" Core updates available, but not applied.")
|
||||||
start_process()
|
print(" Starting core systems...")
|
||||||
|
jarvis_process.start()
|
||||||
cli()
|
cli()
|
||||||
if jarvis_process.is_alive():
|
if jarvis_process.is_alive():
|
||||||
jarvis_process.terminate()
|
jarvis_process.terminate()
|
||||||
jarvis_process.join(30)
|
jarvis_process.join(30)
|
||||||
|
print("All systems shut down.")
|
||||||
|
|
Loading…
Add table
Reference in a new issue