Replies: 1 comment 7 replies
-
Is there a reason you're starting a blocking thread as opposed to a tokio task? If you did the latter, the problem would be simplified. Something like the following, in pseudocode: impl Fairing for SomeFairing {
async fn on_shutdown(&self) -> _ {
self.rx.await;
}
}
#[launch]
fn rocket() -> _ {
let (tx, rx) = tokio::sync::oneshot::channel();
let rocket = rocket::build()
.mount("/", routes![...])
.attach(SomeFairing { rx });
let shutdown = rocket.shutdown();
tokio::task::spawn(async move {
loop {
select! {
_ = shutdown => break,
msg = channel => do(msg),
}
}
cleanup().await;
tx.send(());
});
rocket
} If you must use a regular thread, you can do almost the same thing, but keep your cancellation channel around and use it to send the shutdown message instead of using the |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I need a worker thread along
rocket
which should shutdown gracefully when rocket shuts down.But the thing does not compile because
Fairing::on_shutdown
is taking&self
instead of&mut self
.I come up with this
This is a cross post, and the question is basically here:
https://users.rust-lang.org/t/how-to-have-joinhandle-inside-rocket-fairing-for-shutdown/105748/2
Thanks a lot for the help.
Beta Was this translation helpful? Give feedback.
All reactions