Nestipy provides lifecycle hooks similar to NestJS. These hooks let you run logic during startup, after bootstrap, and during shutdown. Hooks can be implemented by providers, controllers, or modules.
Available Hooks
OnInitwithon_startup()runs when an instance is created.OnModuleInitwithon_module_init()runs after all providers and controllers in a module are created.OnApplicationBootstrapwithon_application_bootstrap()runs once after the entire app is bootstrapped.OnModuleDestroywithon_module_destroy()runs during shutdown beforeOnDestroy.OnDestroywithon_shutdown()runs during shutdown for cleanup.OnApplicationShutdownwithon_application_shutdown()runs at the end of the shutdown process.
Order of Execution
Notes:
OnInitruns per instance, immediately after it is created.OnModuleInitruns after a module and its imports are resolved and instances are created.OnApplicationBootstrapruns once for all providers, controllers, and modules.- Shutdown hooks run in the order shown above.
Request Lifecycle (HTTP)
This shows the runtime request pipeline for HTTP routes.
Notes:
- Request-scoped dependencies are cached using
contextvarsand cleared at the end of each request. - Exception filters can transform errors into structured HTTP responses.
Example
python
from nestipy.common import Injectable
from nestipy.core import OnInit, OnModuleInit, OnApplicationBootstrap
from nestipy.core import OnModuleDestroy, OnDestroy, OnApplicationShutdown
@Injectable()
class ExampleService(
OnInit,
OnModuleInit,
OnApplicationBootstrap,
OnModuleDestroy,
OnDestroy,
OnApplicationShutdown,
):
async def on_startup(self):
print("OnInit")
async def on_module_init(self):
print("OnModuleInit")
async def on_application_bootstrap(self):
print("OnApplicationBootstrap")
async def on_module_destroy(self):
print("OnModuleDestroy")
async def on_shutdown(self):
print("OnDestroy")
async def on_application_shutdown(self):
print("OnApplicationShutdown")Module Hooks
If your module extends NestipyModule, you can override configure(), on_startup(), and on_shutdown() in addition to the standard hooks above.
