-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a wrapper over discord.ext.tasks.loop #186
Comments
We'll need to define exactly what we want to do with the exception. The thing is, our error handler handles errors raised inside commands, which isn't the case for tasks. We could generate a "fake context" then dispatch that to the error handler cog.
from bot import instance as bot_instance
....
@tasks.loop(bot=bot_instance, seconds=20)
async def my_task(self)
.... OR class MyCog():
def __init__(self, bot):
self.task = loop(bot=bot, secondes=10)
self.task.start()
async def my_task(self):
.... What we could also do is override the We'd then have, in from discord.ext.tasks import Loop as BaseLoop, LF
from discord.utils import MISSING
from typing import Any, Callable, Optional, Union, Sequence, Generic
from datetime import datetime
from pydis_core.utils.logging import get_logger
logger = get_logger(__name__)
class Loop(BaseLoop, Generic[LF]):
def __init__(self, **kwargs):
super().__init__(**kwargs)
async def _error(self, *args: Any) -> None:
exception: Exception = args[-1]
# Define what we want to do here.
logger.exception(
msg=f"Task: {self.coro.__qualname__} threw an unexpected {exception.__class__.__name__} exception.",
exc_info=exception
)
return
def loop(
*,
seconds: float = MISSING,
minutes: float = MISSING,
hours: float = MISSING,
time: Union[datetime.time, Sequence[datetime.time]] = MISSING,
count: Optional[int] = None,
reconnect: bool = True,
) -> Callable[[LF], Loop[LF]]:
def decorator(func: LF) -> Loop[LF]:
return Loop[LF](
coro=func,
seconds=seconds,
minutes=minutes,
hours=hours,
count=count,
time=time,
reconnect=reconnect,
)
return decorator And then, in some cog from pydis_core.ext.tasks import loop
class MyCog(Cog):
@loop(minute=5)
async def my_task(self):
# body goes here
|
discord.ext.tasks.loop
automatically reruns tasks by default on some errors. The implementation doesn't seem great though because:A solution would be to default to not reconnecting. A util in botcore gives us more flexibility to change how things work in the future (e.g. error handling, custom retry logic in some cases, etc).
The text was updated successfully, but these errors were encountered: