Skip to content

feat: add args, kwargs to task #1478

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

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions interactions/models/internal/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,25 @@ def on_error(self, error: Exception) -> None:
self.on_error_sentry_hook(error)
interactions.Client.default_error_handler("Task", error)

async def __call__(self) -> None:
async def __call__(self, *args, **kwargs) -> None:
try:
if inspect.iscoroutinefunction(self.callback):
val = await self.callback()
val = await self.callback(*args, **kwargs)
else:
val = self.callback()
val = self.callback(*args, **kwargs)

if isinstance(val, BaseTrigger):
self.reschedule(val)
except Exception as e:
self.on_error(e)

def _fire(self, fire_time: datetime) -> None:
def _fire(self, fire_time: datetime, *args, **kwargs) -> None:
"""Called when the task is being fired."""
self.trigger.set_last_call_time(fire_time)
_ = asyncio.create_task(self())
_ = asyncio.create_task(self(*args, **kwargs))
self.iteration += 1

async def _task_loop(self) -> None:
async def _task_loop(self, *args, **kwargs) -> None:
"""The main task loop to fire the task at the specified time based on triggers configured."""
while not self._stop.is_set():
fire_time = self.trigger.next_fire()
Expand All @@ -106,14 +106,14 @@ async def _task_loop(self) -> None:
if future in done:
return None

self._fire(fire_time)
self._fire(fire_time, *args, **kwargs)

def start(self) -> None:
def start(self, *args, **kwargs) -> None:
"""Start this task."""
try:
self.trigger.reschedule()
self._stop.clear()
self.task = asyncio.create_task(self._task_loop())
self.task = asyncio.create_task(self._task_loop(*args, **kwargs))
except RuntimeError:
get_logger().error(
"Unable to start task without a running event loop! We recommend starting tasks within an `on_startup` event."
Expand All @@ -125,10 +125,10 @@ def stop(self) -> None:
if self.task:
self.task.cancel()

def restart(self) -> None:
def restart(self, *args, **kwargs) -> None:
"""Restart this task."""
self.stop()
self.start()
self.start(*args, **kwargs)

def reschedule(self, trigger: BaseTrigger) -> None:
"""
Expand Down