1
0
Fork 0
mirror of synced 2024-06-02 10:44:32 +12:00

Move tasks into their own methods, to try to gc better

This commit is contained in:
phxntxm 2018-09-22 11:17:11 -05:00
parent 259da83950
commit c7742a8023
5 changed files with 172 additions and 140 deletions

View file

@ -1,18 +1,22 @@
import discord import discord
import pendulum import pendulum
import asyncio import asyncio
import traceback
from pendulum.parsing.exceptions import ParserError from pendulum.parsing.exceptions import ParserError
from discord.ext import commands from discord.ext import commands
from . import utils from . import utils
class Birthday: tzmap = {
def __init__(self, bot): 'us-central': pendulum.timezone('US/Central'),
self.bot = bot 'eu-central': pendulum.timezone('Europe/Paris'),
self.bot.loop.create_task(self.birthday_task()) 'hongkong': pendulum.timezone('Hongkong'),
def sort_birthdays(self, bds): }
def sort_birthdays(bds):
# First sort the birthdays based on the comparison of the actual date # First sort the birthdays based on the comparison of the actual date
bds = sorted(bds, key=lambda x: x['birthday']) bds = sorted(bds, key=lambda x: x['birthday'])
# We want to split this into birthdays after and before todays date # We want to split this into birthdays after and before todays date
@ -31,6 +35,12 @@ class Birthday:
# So all we need to do is put them in order all of "laters" then all of "befores" # So all we need to do is put them in order all of "laters" then all of "befores"
return later_bds + previous_bds return later_bds + previous_bds
class Birthday:
def __init__(self, bot):
self.bot = bot
self.task = self.bot.loop.create_task(self.birthday_task())
def get_birthdays_for_server(self, server, today=False): def get_birthdays_for_server(self, server, today=False):
bds = self.bot.db.load('birthdays') bds = self.bot.db.load('birthdays')
# Get a list of the ID's to compare against # Get a list of the ID's to compare against
@ -46,6 +56,7 @@ class Birthday:
continue continue
day = pendulum.parse(bd['birthday']) day = pendulum.parse(bd['birthday'])
# tz = tzmap.get(server.region)
# Check if it's today, and we want to only get todays birthdays # Check if it's today, and we want to only get todays birthdays
if (today and day.date() == pendulum.today().date()) or not today: if (today and day.date() == pendulum.today().date()) or not today:
# If so, get the member and add them to the entry # If so, get the member and add them to the entry
@ -55,11 +66,17 @@ class Birthday:
'member': member 'member': member
}) })
return self.sort_birthdays(_entries) return sort_birthdays(_entries)
async def birthday_task(self): async def birthday_task(self):
while True: while True:
try:
await self.notify_birthdays() await self.notify_birthdays()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
# Every 12 hours, this is not something that needs to happen often # Every 12 hours, this is not something that needs to happen often
await asyncio.sleep(60 * 60 * 12) await asyncio.sleep(60 * 60 * 12)

View file

@ -2,20 +2,18 @@ import asyncio
import discord import discord
import re import re
import traceback import traceback
import logging
from discord.ext import commands from discord.ext import commands
from . import utils from . import utils
log = logging.getLogger()
BASE_URL = 'https://api.picarto.tv/v1' BASE_URL = 'https://api.picarto.tv/v1'
class Picarto: class Picarto:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.bot.loop.create_task(self.check_channels()) self.task = self.bot.loop.create_task(self.picarto_task())
# noinspection PyAttributeOutsideInit # noinspection PyAttributeOutsideInit
async def get_online_users(self): async def get_online_users(self):
@ -69,11 +67,19 @@ class Picarto:
channel = re.search("(?<=picarto.tv/)(.*)", channel).group(1) channel = re.search("(?<=picarto.tv/)(.*)", channel).group(1)
return channel.lower() in [stream['name'].lower() for stream in self.online_channels] return channel.lower() in [stream['name'].lower() for stream in self.online_channels]
async def check_channels(self): async def picarto_task(self):
await self.bot.wait_until_ready()
# This is a loop that runs every 30 seconds, checking if anyone has gone online
try: try:
await self.bot.wait_until_ready()
while not self.bot.is_closed(): while not self.bot.is_closed():
await self.check_channels()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(30)
async def check_channels(self):
await self.get_online_users() await self.get_online_users()
picarto = await self.bot.db.actual_load('picarto', table_filter={'notifications_on': 1}) picarto = await self.bot.db.actual_load('picarto', table_filter={'notifications_on': 1})
for data in picarto: for data in picarto:
@ -122,12 +128,6 @@ class Picarto:
except (discord.Forbidden, discord.HTTPException, AttributeError): except (discord.Forbidden, discord.HTTPException, AttributeError):
pass pass
await asyncio.sleep(30)
except Exception as e:
tb = traceback.format_exc()
fmt = "{1}\n{0.__class__.__name__}: {0}".format(tb, e)
log.error(fmt)
@commands.group(invoke_without_command=True) @commands.group(invoke_without_command=True)
@utils.custom_perms(send_messages=True) @utils.custom_perms(send_messages=True)
@utils.check_restricted() @utils.check_restricted()

View file

@ -23,6 +23,7 @@ class Raffle:
with open("error_log", 'a') as f: with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f) traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f) print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(60) await asyncio.sleep(60)
async def check_raffles(self): async def check_raffles(self):

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import aiohttp import aiohttp
import traceback
from discord.ext import commands from discord.ext import commands
from base64 import urlsafe_b64encode from base64 import urlsafe_b64encode
@ -20,9 +21,21 @@ class Spotify:
urlsafe_b64encode(self._authorization.encode()).decode() urlsafe_b64encode(self._authorization.encode()).decode()
) )
} }
self.bot.loop.create_task(self.api_token_task()) self.task = self.bot.loop.create_task(self.api_token_task())
async def api_token_task(self): async def api_token_task(self):
while True:
try:
delay = await self.get_api_token()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
delay = 2400
finally:
await asyncio.sleep(delay)
async def get_api_token(self):
url = "https://accounts.spotify.com/api/token" url = "https://accounts.spotify.com/api/token"
opts = {"grant_type": "client_credentials"} opts = {"grant_type": "client_credentials"}
while True: while True:
@ -30,6 +43,7 @@ class Spotify:
response = await session.post(url, data=opts) response = await session.post(url, data=opts)
data = await response.json() data = await response.json()
self._token = data.get("access_token") self._token = data.get("access_token")
return data.get("expires_in")
await asyncio.sleep(data.get("expires_in", 2400)) await asyncio.sleep(data.get("expires_in", 2400))

View file

@ -2,11 +2,9 @@ from discord.ext import commands
from . import utils from . import utils
import aiohttp
import asyncio import asyncio
import discord import discord
import re import re
import rethinkdb as r
import traceback import traceback
import logging import logging
@ -23,6 +21,8 @@ class Twitch:
self.key = utils.twitch_key self.key = utils.twitch_key
self.params = {'client_id': self.key} self.params = {'client_id': self.key}
self.task = bot.loop.create_task(self.check_channels())
def _form_embed(self, data): def _form_embed(self, data):
if not data: if not data:
return None return None
@ -82,11 +82,19 @@ class Twitch:
data = await utils.request(url, payload=self.params) data = await utils.request(url, payload=self.params)
return self._form_embed(data) return self._form_embed(data)
async def check_channels(self): async def twitch_task(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# This is a loop that runs every 30 seconds, checking if anyone has gone online
try:
while not self.bot.is_closed(): while not self.bot.is_closed():
try:
await self.check_channels()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(30)
async def check_channels(self):
twitch = await self.bot.db.actual_load('twitch', table_filter={'notifications_on': 1}) twitch = await self.bot.db.actual_load('twitch', table_filter={'notifications_on': 1})
for data in twitch: for data in twitch:
m_id = int(data['member_id']) m_id = int(data['member_id'])
@ -134,12 +142,6 @@ class Twitch:
except (discord.Forbidden, discord.HTTPException, AttributeError): except (discord.Forbidden, discord.HTTPException, AttributeError):
pass pass
await asyncio.sleep(30)
except Exception as e:
tb = traceback.format_exc()
fmt = "{1}\n{0.__class__.__name__}: {0}".format(tb, e)
log.error(fmt)
@commands.group(invoke_without_command=True) @commands.group(invoke_without_command=True)
@commands.guild_only() @commands.guild_only()
@utils.custom_perms(send_messages=True) @utils.custom_perms(send_messages=True)
@ -329,6 +331,4 @@ class Twitch:
def setup(bot): def setup(bot):
t = Twitch(bot)
bot.loop.create_task(t.check_channels())
bot.add_cog(Twitch(bot)) bot.add_cog(Twitch(bot))