wait_on/
task.rs

1use anyhow::{bail, Result};
2use tokio::select;
3use tokio::time::sleep;
4
5use crate::resource::Resource;
6use crate::{WaitOptions, Waitable};
7
8pub struct WaitOnTask {
9    resource: Resource,
10    options: WaitOptions,
11}
12
13impl WaitOnTask {
14    pub fn new(resource: Resource, options: WaitOptions) -> Self {
15        Self { resource, options }
16    }
17
18    pub async fn run(self) -> Result<()> {
19        select! {
20            _ = self.watch() => Ok(()),
21            _ = self.deadline() => bail!("Timeout reached"),
22        }
23    }
24
25    async fn watch(&self) -> Result<()> {
26        let resource = self.resource.clone();
27        let options = self.options.clone();
28
29        tokio::spawn(async move { resource.wait(&options).await }).await??;
30
31        Ok(())
32    }
33
34    async fn deadline(&self) -> Result<()> {
35        sleep(self.options.timeout).await;
36        bail!("Timeout reached");
37    }
38}