[{"data":1,"prerenderedAt":482},["ShallowReactive",2],{"$ftU-UDNrd4MgxT96v4H0ULXYJ3wiAKye2JOvhl6R-OLg":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":468,"editUrl":476,"prev":477,"next":480},"fundamentals\u002Flifecycle-events","Lifecycle events","Hook into application startup and shutdown with lifecycle traits.","---\ntitle: Lifecycle events\ndescription: Hook into application startup and shutdown with lifecycle traits.\n---\n\n# Lifecycle events\n\nFerrite exposes four lifecycle hooks. Each is an `#[async_trait]` with default no-op implementations, so you only override what you need.\n\n## The hooks\n\n| Hook | Runs when | Typical use |\n| --- | --- | --- |\n| `OnModuleInit` | A module's providers are instantiated | Open clients, warm caches, validate config |\n| `OnApplicationBootstrap` | The full graph is assembled, right before the server starts accepting | Start background servers (gRPC, message consumers) |\n| `OnModuleDestroy` | Application shutdown begins | Close resources, flush buffers |\n| `OnApplicationShutdown` | Shutdown after modules are destroyed | Final teardown |\n\n## Usage\n\n```rust\nuse ferrite_framework::{OnApplicationBootstrap, OnModuleInit, async_trait, injectable};\n\n#[injectable]\npub struct MetricsService;\n\nimpl MetricsService {\n    #[inject]\n    pub fn new() -> Self {\n        Self\n    }\n}\n\n#[async_trait]\nimpl OnModuleInit for MetricsService {\n    async fn on_module_init(&self) {\n        \u002F\u002F e.g. pre-create a connection pool\n    }\n}\n\n#[async_trait]\nimpl OnApplicationBootstrap for MetricsService {\n    async fn on_application_bootstrap(&self) {\n        \u002F\u002F e.g. start the metrics exporter\n    }\n}\n```\n\n## Hooking a module\n\nEcosystem modules implement these hooks internally. For example, `ferrite-grpc`'s `GrpcModuleImpl` implements `OnApplicationBootstrap` so the gRPC server starts when the app is ready:\n\n```rust\n#[module(imports = [GrpcModuleImpl])]\npub struct AppModule;\n\n\u002F\u002F GrpcModuleImpl::on_application_bootstrap() spawns:\n\u002F\u002F   grpc.start(\"0.0.0.0:50051\")\n```\n\nThe same pattern applies to `NatsModuleImpl`, `KafkaModuleImpl`, `QueueModuleImpl`, `CronModuleImpl`, and `I18nModuleImpl`.\n\n## Shutdown ordering\n\nShutdown happens in the reverse order of startup: application hooks first, then module destroy hooks. Keep expensive cleanup in `OnModuleDestroy` (pools, queues) and one-time teardown in `OnApplicationShutdown`.\n\n## Threading & async\n\nAll hooks are async — spawning backend loops inside `OnApplicationBootstrap` with `tokio::spawn` is the documented pattern for gRPC, NATS, and Kafka servers. Errors inside hooks propagate through bootstrap as usual (you'll see them via the app's error\u002Fpanic path).\n\n## Next steps\n\n- [Bootstrap](\u002Fdocs\u002Foverview\u002Fbootstrap).\n- [Testing](\u002Fdocs\u002Ffundamentals\u002Ftesting).",{"frontmatter":9,"meta":10,"nodes":34},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31],{"id":17,"depth":13,"text":18},"the-hooks","The hooks",{"id":20,"depth":13,"text":21},"usage","Usage",{"id":23,"depth":13,"text":24},"hooking-a-module","Hooking a module",{"id":26,"depth":13,"text":27},"shutdown-ordering","Shutdown ordering",{"id":29,"depth":13,"text":30},"threading-async","Threading & async",{"id":32,"depth":13,"text":33},"next-steps","Next steps",[35,39,48,51,126,128,348,350,364,407,427,429,437,439,449,451],[36,37,5],"h1",{"id":38},"lifecycle-events",[40,41,42,43,47],"p",{},"Ferrite exposes four lifecycle hooks. Each is an ",[44,45,46],"code",{},"#[async_trait]"," with default no-op implementations, so you only override what you need.",[49,50,18],"h2",{"id":17},[52,53,54,70],"table",{},[55,56,57],"thead",{},[58,59,60,64,67],"tr",{},[61,62,63],"th",{},"Hook",[61,65,66],{},"Runs when",[61,68,69],{},"Typical use",[71,72,73,87,100,113],"tbody",{},[58,74,75,81,84],{},[76,77,78],"td",{},[44,79,80],{},"OnModuleInit",[76,82,83],{},"A module's providers are instantiated",[76,85,86],{},"Open clients, warm caches, validate config",[58,88,89,94,97],{},[76,90,91],{},[44,92,93],{},"OnApplicationBootstrap",[76,95,96],{},"The full graph is assembled, right before the server starts accepting",[76,98,99],{},"Start background servers (gRPC, message consumers)",[58,101,102,107,110],{},[76,103,104],{},[44,105,106],{},"OnModuleDestroy",[76,108,109],{},"Application shutdown begins",[76,111,112],{},"Close resources, flush buffers",[58,114,115,120,123],{},[76,116,117],{},[44,118,119],{},"OnApplicationShutdown",[76,121,122],{},"Shutdown after modules are destroyed",[76,124,125],{},"Final teardown",[49,127,21],{"id":20},[129,130,133],"pre",{"language":131,"class":132},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[44,134,136,166,167,166,169,166,174,166,188,166,190,166,200,166,205,166,228,166,233,166,238,166,243,166,245,166,249,166,263,166,285,166,291,166,295,166,299,166,301,166,305,166,318,166,335,166,340,166,344],{"class":135},"language-rust",[137,138,141,145,149,152,156,158,161,163],"span",{"class":139,"style":140},"line","display: inline",[137,142,144],{"style":143},"color:#D73A49;--shiki-dark:#F97583","use",[137,146,148],{"style":147},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[137,150,151],{"style":143},"::",[137,153,155],{"style":154},"color:#24292E;--shiki-dark:#E1E4E8","{",[137,157,93],{"style":147},[137,159,160],{"style":154},", ",[137,162,80],{"style":147},[137,164,165],{"style":154},", async_trait, injectable};","\n",[137,168],{"class":139,"style":140},[137,170,171],{"class":139,"style":140},[137,172,173],{"style":154},"#[injectable]",[137,175,176,179,182,185],{"class":139,"style":140},[137,177,178],{"style":143},"pub",[137,180,181],{"style":143}," struct",[137,183,184],{"style":147}," MetricsService",[137,186,187],{"style":154},";",[137,189],{"class":139,"style":140},[137,191,192,195,197],{"class":139,"style":140},[137,193,194],{"style":143},"impl",[137,196,184],{"style":147},[137,198,199],{"style":154}," {",[137,201,202],{"class":139,"style":140},[137,203,204],{"style":154},"    #[inject]",[137,206,207,210,213,216,219,222,226],{"class":139,"style":140},[137,208,209],{"style":143},"    pub",[137,211,212],{"style":143}," fn",[137,214,215],{"style":147}," new",[137,217,218],{"style":154},"() ",[137,220,221],{"style":143},"->",[137,223,225],{"style":224},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[137,227,199],{"style":154},[137,229,230],{"class":139,"style":140},[137,231,232],{"style":224},"        Self",[137,234,235],{"class":139,"style":140},[137,236,237],{"style":154},"    }",[137,239,240],{"class":139,"style":140},[137,241,242],{"style":154},"}",[137,244],{"class":139,"style":140},[137,246,247],{"class":139,"style":140},[137,248,46],{"style":154},[137,250,251,253,256,259,261],{"class":139,"style":140},[137,252,194],{"style":143},[137,254,255],{"style":147}," OnModuleInit",[137,257,258],{"style":143}," for",[137,260,184],{"style":147},[137,262,199],{"style":154},[137,264,265,268,270,273,276,279,282],{"class":139,"style":140},[137,266,267],{"style":143},"    async",[137,269,212],{"style":143},[137,271,272],{"style":147}," on_module_init",[137,274,275],{"style":154},"(",[137,277,278],{"style":143},"&",[137,280,281],{"style":224},"self",[137,283,284],{"style":154},") {",[137,286,287],{"class":139,"style":140},[137,288,290],{"style":289},"color:#6A737D;--shiki-dark:#6A737D","        \u002F\u002F e.g. pre-create a connection pool",[137,292,293],{"class":139,"style":140},[137,294,237],{"style":154},[137,296,297],{"class":139,"style":140},[137,298,242],{"style":154},[137,300],{"class":139,"style":140},[137,302,303],{"class":139,"style":140},[137,304,46],{"style":154},[137,306,307,309,312,314,316],{"class":139,"style":140},[137,308,194],{"style":143},[137,310,311],{"style":147}," OnApplicationBootstrap",[137,313,258],{"style":143},[137,315,184],{"style":147},[137,317,199],{"style":154},[137,319,320,322,324,327,329,331,333],{"class":139,"style":140},[137,321,267],{"style":143},[137,323,212],{"style":143},[137,325,326],{"style":147}," on_application_bootstrap",[137,328,275],{"style":154},[137,330,278],{"style":143},[137,332,281],{"style":224},[137,334,284],{"style":154},[137,336,337],{"class":139,"style":140},[137,338,339],{"style":289},"        \u002F\u002F e.g. start the metrics exporter",[137,341,342],{"class":139,"style":140},[137,343,237],{"style":154},[137,345,346],{"class":139,"style":140},[137,347,242],{"style":154},[49,349,24],{"id":23},[40,351,352,353,356,357,360,361,363],{},"Ecosystem modules implement these hooks internally. For example, ",[44,354,355],{},"ferrite-grpc","'s ",[44,358,359],{},"GrpcModuleImpl"," implements ",[44,362,93],{}," so the gRPC server starts when the app is ready:",[129,365,366],{"language":131,"class":132},[44,367,368,166,384,166,395,166,397,166,402],{"class":135},[137,369,370,373,376,379,381],{"class":139,"style":140},[137,371,372],{"style":154},"#[module(imports ",[137,374,375],{"style":143},"=",[137,377,378],{"style":154}," [",[137,380,359],{"style":147},[137,382,383],{"style":154},"])]",[137,385,386,388,390,393],{"class":139,"style":140},[137,387,178],{"style":143},[137,389,181],{"style":143},[137,391,392],{"style":147}," AppModule",[137,394,187],{"style":154},[137,396],{"class":139,"style":140},[137,398,399],{"class":139,"style":140},[137,400,401],{"style":289},"\u002F\u002F GrpcModuleImpl::on_application_bootstrap() spawns:",[137,403,404],{"class":139,"style":140},[137,405,406],{"style":289},"\u002F\u002F   grpc.start(\"0.0.0.0:50051\")",[40,408,409,410,160,413,160,416,160,419,422,423,426],{},"The same pattern applies to ",[44,411,412],{},"NatsModuleImpl",[44,414,415],{},"KafkaModuleImpl",[44,417,418],{},"QueueModuleImpl",[44,420,421],{},"CronModuleImpl",", and ",[44,424,425],{},"I18nModuleImpl",".",[49,428,27],{"id":26},[40,430,431,432,434,435,426],{},"Shutdown happens in the reverse order of startup: application hooks first, then module destroy hooks. Keep expensive cleanup in ",[44,433,106],{}," (pools, queues) and one-time teardown in ",[44,436,119],{},[49,438,30],{"id":29},[40,440,441,442,444,445,448],{},"All hooks are async — spawning backend loops inside ",[44,443,93],{}," with ",[44,446,447],{},"tokio::spawn"," is the documented pattern for gRPC, NATS, and Kafka servers. Errors inside hooks propagate through bootstrap as usual (you'll see them via the app's error\u002Fpanic path).",[49,450,33],{"id":32},[452,453,454,462],"ul",{},[455,456,457,426],"li",{},[458,459,461],"a",{"href":460},"\u002Fdocs\u002Foverview\u002Fbootstrap","Bootstrap",[455,463,464,426],{},[458,465,467],{"href":466},"\u002Fdocs\u002Ffundamentals\u002Ftesting","Testing",{"title":12,"searchDepth":13,"depth":14,"links":469},[470,471,472,473,474,475],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ffundamentals\u002Flifecycle-events.md",{"title":478,"slug":479},"Dependency injection","fundamentals\u002Fdependency-injection",{"title":467,"slug":481},"fundamentals\u002Ftesting",1789565440171]