Skip to content
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

Allow closing the bot during StartingEvent #1746

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davfsa
Copy link
Member

@davfsa davfsa commented Nov 3, 2023

Summary

Allows the following use-case:

bot = hikari.GatewayBot("TOKEN")

@bot.listen()
async def connect_to_db(event: hikari.StartingEvent) -> None:
    try:
        database = Database.connect()
    except ConnectionFailureException:
        print("Failed to connect to database")
        await bot.close()

bot.run()

This would be the gist of the change as things currently stand, but I am not entirely happy with it. Ideally, we would want any interrupt or call to bot.close() (which should really resolve to the same thing internally to streamline the shutdown process, imo) at any point in time to immediately start the shutdown sequence.

When I get some time, I will play around with implementing the signal handling directly into the loop and (maybe) an event being set stops the loop and makes it continue to the shutdown phase rather than having to keep checking for the the event being set.

Any feedback on this is welcome!


Note for myself (probably wont be changed):
I also dislike how its a bit counter intuitive to check for None other than the event being set, but until CPython 3.11 we will not be able to change that since asyncio.Event tries to get the loop in __init__.

Checklist

  • I have run nox and all the pipelines have passed.
  • I have made unittests according to the code I have added/modified/deleted.

@davfsa davfsa added the enhancement New feature or request label Nov 3, 2023
@davfsa davfsa self-assigned this Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant