πŸƒ LifespanΒΆ

Raito lets you define async startup/shutdown logic using a @rt.lifespan(router) decorator β€” just like in FastAPI.


ExampleΒΆ

from typing import AsyncGenerator

from aiogram import Bot, Router,
from raito import rt, Raito

router = Router(name="lifespan")

@rt.lifespan(router)
async def lifespan_fn(raito: Raito, bot: Bot):
    bot_info = await bot.get_me()
    rt.log.info("πŸš€ Launching bot : [@%s] %s", bot_info.username, bot_info.full_name)

    rt.debug("Registering commands...")
    await raito.register_commands(bot)

    yield

    rt.log.info("πŸ‘‹ Bye!")

How it worksΒΆ

  • Code before yield runs on startup

  • Code after yield runs on shutdown

  • Lifespans are tracked per bot.id and executed in reverse order on exit