I can't seem to manage to implement the async function into the Drop. This is a sample of what I've tried already, it gives me an error of not finding the event loop.
use anyhow::Result;
use bollard::container::RemoveContainerOptions;
use bollard::Docker;
struct MyStruct;
impl MyStruct {
pub async fn do_something(&self) -> Result<()> {
let docker = Docker::connect_with_unix_defaults()?;
docker
.remove_container(
"container-name",
Some(RemoveContainerOptions {
force: true,
..Default::default()
}),
)
.await?;
Ok(())
}
}
impl Drop for MyStruct {
fn drop(&mut self) {
tokio::task::spawn_blocking(move || {
let my_struct = MyStruct;
let handle = tokio::runtime::Handle::try_current().unwrap();
handle.block_on(async {
my_struct.do_something().await.unwrap();
});
});
}
}