1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use futures::StreamExt;
use wasmer_backend_api::{types::PackageVersionState, WasmerClient};

/// Different conditions that can be "awaited" when publishing a package.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum PublishWait {
    None,
    Container,
    NativeExecutables,
    Bindings,
    All,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaitPackageState {
    pub container: bool,
    pub native_executables: bool,
    pub bindings: bool,
}

impl WaitPackageState {
    pub fn is_any(self) -> bool {
        self.container || self.native_executables || self.bindings
    }

    pub fn new_none() -> Self {
        Self {
            container: false,
            native_executables: false,
            bindings: false,
        }
    }

    pub fn new_all() -> Self {
        Self {
            container: true,
            native_executables: true,
            bindings: true,
        }
    }

    pub fn new_container() -> Self {
        Self {
            container: true,
            native_executables: false,
            bindings: false,
        }
    }

    pub fn new_exe() -> Self {
        Self {
            container: true,
            native_executables: true,
            bindings: false,
        }
    }

    pub fn new_bindings() -> Self {
        Self {
            container: true,
            native_executables: false,
            bindings: true,
        }
    }
}

impl From<PublishWait> for WaitPackageState {
    fn from(value: PublishWait) -> Self {
        match value {
            PublishWait::None => Self::new_none(),
            PublishWait::Container => Self::new_container(),
            PublishWait::NativeExecutables => Self::new_exe(),
            PublishWait::Bindings => Self::new_bindings(),
            PublishWait::All => Self::new_all(),
        }
    }
}

pub async fn wait_package(
    client: &WasmerClient,
    to_wait: PublishWait,
    package_version_id: wasmer_backend_api::types::Id,
    timeout: humantime::Duration,
) -> anyhow::Result<()> {
    if let PublishWait::None = to_wait {
        return Ok(());
    }

    let package_version_id = package_version_id.into_inner();

    let mut stream =
        wasmer_backend_api::subscription::package_version_ready(client, &package_version_id)
            .await?;

    let mut state: WaitPackageState = to_wait.into();

    let deadline: std::time::Instant =
        std::time::Instant::now() + std::time::Duration::from_secs(timeout.as_secs());

    loop {
        if !state.is_any() {
            break;
        }

        if std::time::Instant::now() > deadline {
            return Err(anyhow::anyhow!(
                "Timed out waiting for package version to become ready"
            ));
        }

        let data = match tokio::time::timeout_at(deadline.into(), stream.next()).await {
            Err(_) => {
                return Err(anyhow::anyhow!(
                    "Timed out waiting for package version to become ready"
                ));
            }
            Ok(None) => {
                break;
            }
            Ok(Some(data)) => data,
        };

        if let Some(data) = data.unwrap().data {
            match data.package_version_ready.state {
                PackageVersionState::WebcGenerated => state.container = false,
                PackageVersionState::BindingsGenerated => state.bindings = false,
                PackageVersionState::NativeExesGenerated => state.native_executables = false,
            }
        }
    }

    Ok(())
}