1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 13:02:38 +00:00

WIP task cancellation on loop close, formatting

This commit is contained in:
Myned 2017-10-15 12:58:16 -04:00
parent 06c7428cc2
commit bfc39d7a99
3 changed files with 18 additions and 10 deletions

View file

@ -28,13 +28,12 @@ class Bot:
@checks.del_ctx()
async def die(self, ctx):
if isinstance(self.bot.get_channel(u.config['shutdown_channel']), d.TextChannel):
await self.bot.get_channel(u.config['shutdown_channel']).send('**Shutting down . . .** 🌙')
await self.bot.get_channel(u.config['shutdown_channel']).send('**Shutting down 🌙 . . .**')
# loop = self.bot.loop.all_tasks()
# for task in loop:
# task.cancel()
u.close()
await self.bot.logout()
await self.bot.close()
u.close(self.bot.loop)
print('\n/ / / / / / / / / / / /\nD I S C O N N E C T E D\n\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\\n')
# u.notify('D I S C O N N E C T E D')
@ -44,14 +43,13 @@ class Bot:
async def restart(self, ctx):
print('\n| | | | | | | | | |\nR E S T A R T I N G\n| | | | | | | | | |\n')
if isinstance(self.bot.get_channel(u.config['shutdown_channel']), d.TextChannel):
await self.bot.get_channel(u.config['shutdown_channel']).send('**Restarting . . .** 💤')
await self.bot.get_channel(u.config['shutdown_channel']).send('**Restarting 💤 . . .**')
# u.notify('R E S T A R T I N G')
# loop = self.bot.loop.all_tasks()
# for task in loop:
# task.cancel()
u.close()
await self.bot.logout()
await self.bot.close()
u.close(self.bot.loop)
os.execl(sys.executable, 'python3', 'run.py')
# Invite bot to bot owner's server

View file

@ -35,7 +35,7 @@ async def on_ready():
# bot.loop.create_task(u.clear(booru.temp_urls, 30*60))
if isinstance(bot.get_channel(u.config['startup_channel']), d.TextChannel):
await bot.get_channel(u.config['startup_channel']).send('**Started.** ☀️')
await bot.get_channel(u.config['startup_channel']).send('**Started ☀️ .**')
print('\n\\ \\ \\ \\ \\ \\ \\ \\ \\\nC O N N E C T E D : {}\n/ / / / / / / / /\n'.format(bot.user.name))
# u.notify('C O N N E C T E D')
@ -44,9 +44,8 @@ async def on_ready():
async def on_error(error, *args, **kwargs):
if isinstance(bot.get_channel(u.config['shutdown_channel']), d.TextChannel):
await bot.get_channel(u.config['shutdown_channel']).send('**ERROR** ⚠️ {}'.format(error))
u.close()
await bot.logout()
await bot.close()
u.close(bot.loop)
print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr)
tb.print_exc()
# u.notify('E R R O R')

View file

@ -3,6 +3,7 @@ import json
import os
import pickle as pkl
import subprocess
from contextlib import suppress
import aiohttp
from pync import Notifier
@ -72,12 +73,22 @@ async def clear(obj, interval=10 * 60, replace=None):
session = aiohttp.ClientSession()
def close():
def close(loop):
global session
if session:
session.close()
loop.stop()
pending = asyncio.Task.all_tasks()
for task in pending:
task.cancel()
# with suppress(asyncio.CancelledError):
# loop.run_until_complete(task)
# loop.close()
print('Finished cancelling tasks.')
async def fetch(url, *, params={}, json=False):
global session