Basically I want to have a single process handling multiple things at the same time (specifically want to have an http endpoint for monitoring a non-http service), but it seems I'm being caught up in Rocket depending on a rocket-specific tokio runtime and I'm not seeing how to get around this. I've thought about having rocket be the main entry point instead of tokio's standard one and launching other stuff from that runtime, but this seems wrong in general and fragile as I might switch libraries. I've thought about using hyper and warp and I'm somewhat confident I could make them work in this situation but want to use rocket as I'm using it elsewhere in this project. I'm using the 0.5-rc01 version.
From the docs:
Rocket v0.5 uses the tokio runtime. The runtime is started for you if you use #[launch] or #[rocket::main], but you can still launch() a Rocket instance on a custom-built runtime by not using either attribute.
Unfortunately I cannot find any further explanation of what requirements this custom-built runtime has nor any examples not using the launch/main macros.
Here's a simplified version of the code I'm trying to use:
#[rocket::get("/ex")]
async fn test() -> &'static str {
"OK"
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
let config = Config {
port: 8001,
address: std::net::Ipv4Addr::new(127, 0, 0, 1).into(),
..Config::debug_default()
};
rocket::custom(&config)
.mount("/", rocket::routes![test])
.launch()
.await;
});
let start = Instant::now();
let mut interval = interval_at(start, tokio::time::Duration::from_secs(5));
loop {
interval.tick().await;
println!("Other scheduled work");
}
}
When I press CTRL-C to terminate the process, the following is printed:
^CWarning: Received SIGINT. Requesting shutdown.
Received shutdown request. Waiting for pending I/O...
Warning: Server failed to shutdown cooperatively.
>> Server is executing inside of a custom runtime.
>> Rocket's runtime is `#[rocket::main]` or `#[launch]`.
>> Refusing to terminate runaway custom runtime.
Other scheduled work
Other scheduled work
Other scheduled work
I've found this documentation explaining that you need to 'cooperate' in shutdown if you're using a custom runtime but it does not explain what cooperation entails. Also, running things besides rocket in the spawned thread causes CTRL-C to kill the process as I would have normally expected, so I think this is related to rocket specifically. What do I need to do so that I can actually kill the process?
Edit: I gave up and switched to warp, which was a thousand times easier. I would still be curious to know the proper way to do this with rocket and would try again if anybody has suggestions.