better database shutdown handling

This commit is contained in:
AF 2022-10-11 14:31:33 +00:00
parent 0793443e70
commit dabbc6c452
2 changed files with 28 additions and 17 deletions

View File

@ -47,9 +47,10 @@ class QueueAudio(discord.AudioSource):
async def create(cls, guild: discord.Guild): async def create(cls, guild: discord.Guild):
return cls(guild, await QueueAudio.respawned(guild)) return cls(guild, await QueueAudio.respawned(guild))
async def save(self): async def save(self, delay: bool):
hybernated = [] hybernated = []
for audio in list(self.queue): for audio in list(self.queue):
if delay:
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
hybernated.append(audio.hybernate()) hybernated.append(audio.hybernate())
queue_db.set_nowait(self.guild.id, hybernated) queue_db.set_nowait(self.guild.id, hybernated)

View File

@ -6,7 +6,6 @@ import time
import discord import discord
from v6d1tokens.client import request_token from v6d1tokens.client import request_token
from v6d2ctx.context import Benchmark, monitor
from v6d2ctx.handle_content import handle_content from v6d2ctx.handle_content import handle_content
from v6d2ctx.lock_for import lock_for from v6d2ctx.lock_for import lock_for
from v6d2ctx.serve import serve from v6d2ctx.serve import serve
@ -83,17 +82,19 @@ async def on_message(message: discord.Message) -> None:
await handle_content(message, message.content, prefix) await handle_content(message, message.content, prefix)
async def save_queues(): async def save_queues(delay: bool):
for mainasrc in list(mainasrcs.values()): for mainasrc in list(mainasrcs.values()):
if delay:
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
await mainasrc.queue.save() await mainasrc.queue.save(delay)
async def save_vcs(): async def save_vcs(delay: bool):
if vcs_restored: if vcs_restored:
vcs = [] vcs = []
vc: discord.VoiceClient vc: discord.VoiceClient
for vc in list(client.voice_clients): for vc in list(client.voice_clients):
if delay:
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
if vc.is_playing(): if vc.is_playing():
if vc.guild is not None and vc.channel is not None: if vc.guild is not None and vc.channel is not None:
@ -105,15 +106,23 @@ async def save_commit():
await queue_db.set('commit', time.time()) await queue_db.set('commit', time.time())
async def save_all(delay: bool):
await save_queues(delay)
await save_vcs(delay)
await save_commit()
async def save_job(): async def save_job():
while True: while True:
await asyncio.sleep(1) await asyncio.sleep(1)
with Benchmark('SVQ'): await save_all(True)
await save_queues()
with Benchmark('SVV'):
await save_vcs() async def save_daemon():
with Benchmark('SVC'): try:
await save_commit() await save_job()
except asyncio.CancelledError:
pass
async def start_app(): async def start_app():
@ -121,7 +130,8 @@ async def start_app():
async def setup_tasks(): async def setup_tasks():
loop.create_task(save_job()) global save_task
save_task = loop.create_task(save_daemon())
loop.create_task(start_app()) loop.create_task(start_app())
@ -139,14 +149,14 @@ async def main():
token = await request_token('music', 'token') token = await request_token('music', 'token')
await client.login(token) await client.login(token)
loop.create_task(setup_tasks()) loop.create_task(setup_tasks())
if os.getenv('v6monitor'):
loop.create_task(monitor())
if os.getenv('v6tor', None) is None: if os.getenv('v6tor', None) is None:
try: try:
subprocess.Popen('tor') subprocess.Popen('tor')
except FileNotFoundError: except FileNotFoundError:
print('no tor') print('no tor')
await client.connect() await client.connect()
save_task.cancel()
await save_all(False)
if __name__ == '__main__': if __name__ == '__main__':