Skip to content

Commit cd93570

Browse files
committed
Shutdown handlers
1 parent a5d5b01 commit cd93570

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Diff for: launcher.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ async def start():
3131
await bot.load_extension(f"lib.cogs.{cog}")
3232
progress.next()
3333

34-
# @bot.event
35-
# async def on_interaction(ctx: Interaction):
36-
# if isinstance(ctx.command, Command):
37-
# print('is Command')
38-
# raise BotNotReady()
39-
4034
@bot.tree.error
4135
async def on_command_error(ctx: Interaction, exc) -> None:
4236
if any([isinstance(exc, error) for error in IGNORE_EXCEPTIONS]):
@@ -57,8 +51,8 @@ async def on_command_error(ctx: Interaction, exc) -> None:
5751
await bot.send(ctx, embed=embed)
5852
elif isinstance(exc, NotFound):
5953
await bot.send(ctx,
60-
embed=Embed(title='Not Found Error', description='One or more items could not be found.',
61-
colour=0xff0000))
54+
embed=Embed(title='Not Found Error', description='One or more items could not be found.',
55+
colour=0xff0000))
6256
elif hasattr(exc, "original"):
6357
if isinstance(exc.original, HTTPException):
6458
embed: Embed = Embed(title="Http Error", description='Message failed to send', colour=0xff0000)
@@ -75,12 +69,6 @@ async def on_command_error(ctx: Interaction, exc) -> None:
7569

7670
def exit_handler():
7771
bot.db.commit()
78-
# try:
79-
# loop = asyncio.get_running_loop()
80-
# except RuntimeError:
81-
# loop = asyncio.new_event_loop()
82-
# loop.run_until_complete(bot.close())
83-
# asyncio.run(bot.close())
8472

8573
atexit.register(exit_handler)
8674
await bot.start(bot.config.token)

Diff for: lib/bot/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def on_connect(self):
5454
print('Shards Not Ready')
5555
await asyncio.sleep(.5)
5656
self.register_guilds()
57+
asyncio.ensure_future(self.monitor_shutdown())
5758
print(f"Signed into {self.user.display_name}#{self.user.discriminator}")
5859
# asyncio.ensure_future(self.timer.start())
5960

@@ -94,6 +95,20 @@ async def send(ctx: Interaction, *args, **kwargs):
9495
except NotFound:
9596
return await ctx.response.send_message(*args, **kwargs)
9697

98+
async def monitor_shutdown(self):
99+
while True:
100+
if psutil.Process().status() == psutil.STATUS_ZOMBIE:
101+
# Perform cleanup actions, such as committing the database
102+
self.db.commit()
103+
break
104+
if psutil.process_iter(['pid', 'name']):
105+
for process in psutil.process_iter():
106+
if process.name() == 'shutdown.exe':
107+
self.db.commit()
108+
await self.close()
109+
exit()
110+
await asyncio.sleep(1)
111+
97112
def register_guilds(self):
98113
progress = Progress('Registering guilds', len(self.guilds))
99114
for guild in self.guilds:

0 commit comments

Comments
 (0)