Getting a notification when Rocket is shutting down? #2800
-
Hi! I need to save certain transient data to disk on shutdown. By "transient data" I mean:
Does Rocket support such functionality? (if not, I can open a ticket). I can see that Basically all I need would be among the terms of: fn my_shutdown_routine( my_data: &rocket::State<MyData> ) {
// Alright. Rocket is going down. Time to write everything to disk.
}
#[launch]
fn rocket() -> _ {
rocket::build().manage(my_data).shutdown(my_shutdown_routine);
/* ... */
} Ideally Did I miss this functionality in the docs? Cheers and thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think this is what you're after: https://api.rocket.rs/v0.5/rocket/fairing/struct.AdHoc#method.on_shutdown with example use here: |
Beta Was this translation helpful? Give feedback.
-
Thank you! That's exactly what I was looking for and it worked! The final code looks like this: rocket::build()
.attach(rocket::fairing::AdHoc::on_shutdown("Bye!", |r| {
Box::pin(async move {
let r = r.state::<MyData>().unwrap();
println!("Rocket is on its way back!");
})
}))
.manage(my_data)
.mount( /* ... */ ) |
Beta Was this translation helpful? Give feedback.
I think this is what you're after: https://api.rocket.rs/v0.5/rocket/fairing/struct.AdHoc#method.on_shutdown with example use here:
https://rocket.rs/guide/v0.5/fairings/#ad-hoc-fairings