Skip to content

Clean improvements - #3535

Open
mbaruh wants to merge 11 commits into
mainfrom
clean-improvements
Open

Clean improvements#3535
mbaruh wants to merge 11 commits into
mainfrom
clean-improvements

Conversation

@mbaruh

@mbaruh mbaruh commented Jul 4, 2026

Copy link
Copy Markdown
Member

No description provided.

@mbaruh
mbaruh requested a review from MarkKoz as a code owner July 4, 2026 21:56
@mbaruh mbaruh added a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 2 - normal Normal Priority t: enhancement Changes or improvements to existing features labels Jul 4, 2026
Comment thread bot/utils/async_utils.py Outdated
self._running_tasks = set()

def submit[T](self, coro: Awaitable[T]) -> asyncio.Task[T]:
"""Wraps the coroutine with _semaphore logic, schedules it on the event loop, and ensures cleanup."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use imperative mood for docs "Wrap the coroutine" rather than "Wraps the coroutine".

Also, _semaphore is an internal detail so it shouldn't be directly referenced in the docstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah the underscore is because of a refactor, but yeah don't have to mention how it's implemented.

Comment thread bot/utils/async_utils.py Outdated

async def gather(self, return_exceptions: bool = False) -> list[Any]:
"""Waits for all submitted coroutines to finish execution."""
if self._running_tasks:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this check is redundant

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I thought an empty asyncio.gather raised an error, I guess not

Comment thread bot/utils/async_utils.py
await self._event.wait()


class AsyncExecutor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a public interface for cancelling the tasks? Might be needed if the bot is shutting down.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

"""Return the datetime of the earliest message cached, or now if the cache is empty."""
return self.bot.cached_messages[0].created_at if self.bot.cached_messages else arrow.utcnow().datetime

def _use_cache(self, most_recent_limit: datetime | None) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Inconsistent terminology in this file. There's earliest/latest, lower/upper, first/second, and least/most recent.

Comment thread bot/exts/moderation/clean.py
Comment thread bot/exts/moderation/clean.py
Comment thread bot/exts/moderation/clean.py Outdated
continue

messages_to_delete.append(message)
message_ids.append(message.id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: message IDs can be derived from messages_to_delete. I think it'd be cleaner to not maintain parallel lists, and that it wouldn't be a significant change in performance.

Comment thread bot/exts/moderation/clean.py
Comment thread bot/exts/moderation/clean.py
@mbaruh
mbaruh requested a review from MarkKoz July 5, 2026 20:02
Comment thread bot/utils/async_utils.py
self._running_tasks.clear()
return result

def cancel_all(self) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wait for the tasks to be cancelled or is it fine to fire and forget?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have anything to do if we wait? Tasks that haven't started yet are cleared, and tasks that are already ongoing get cancelled. But if the task e.g. made an API call to Discord, we have no choice but let it play out. Is there something beyond cancelling it that we can realistically do? In the case of the clean cog, I think it's ok even if we let tasks that already started finish.


def cog_unload(self) -> None:
"""Stop any ongoing clean."""
self.cleaning = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not cancel the tasks in here? Is setting cleaning to False guaranteed to cancel the tasks if any exist?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's a running _clean_messages coro, it will stop the cleaning process, hit the check in line 489 and cancel the tasks. I can also cancel it here for good measure, but that function is the central point for handling this flag. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't have the tasks here, because I've found holding in the cog tasks specific to a command call awkward

Comment thread bot/exts/moderation/clean.py Outdated
if not self.cleaning:
return None
if old_messages:
log.trace("Some of the found messages are older than 14d, and will deleted individually.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "will deleted" -> "will be deleted"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 2 - normal Normal Priority t: enhancement Changes or improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants