aboutsummaryrefslogtreecommitdiff
path: root/src/remote/restart_policy.rs
blob: 757efcc52432383dd29c076e4cf5e0a57c6cccb7 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RemoteServerRestartReason {
    EndpointProtocol,
    SurfaceInterest,
    HealthCheck,
    DaemonDetach,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RemoteInstallRunningServerPlan {
    KeepRunning,
    LiveHandoff,
    StopRequired(RemoteServerRestartReason),
}

pub(super) fn remote_server_restart_reason(
    endpoint_protocol_generation: Option<u32>,
    detached_server_daemon: bool,
    require_surface_interest: bool,
    surface_interest: bool,
    health_check: bool,
) -> Option<RemoteServerRestartReason> {
    if endpoint_protocol_generation != Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION)
    {
        return Some(RemoteServerRestartReason::EndpointProtocol);
    }
    if require_surface_interest && !surface_interest {
        return Some(RemoteServerRestartReason::SurfaceInterest);
    }
    if require_surface_interest && !health_check {
        return Some(RemoteServerRestartReason::HealthCheck);
    }
    if !detached_server_daemon {
        return Some(RemoteServerRestartReason::DaemonDetach);
    }
    None
}

pub(super) fn remote_install_running_server_plan(
    endpoint_protocol_generation: Option<u32>,
    detached_server_daemon: bool,
    surface_interest: bool,
    health_check: bool,
    live_handoff: bool,
    live_handoff_enabled: bool,
    require_surface_interest: bool,
) -> RemoteInstallRunningServerPlan {
    let Some(reason) = remote_server_restart_reason(
        endpoint_protocol_generation,
        detached_server_daemon,
        require_surface_interest,
        surface_interest,
        health_check,
    ) else {
        return RemoteInstallRunningServerPlan::KeepRunning;
    };

    if live_handoff_enabled && live_handoff {
        return RemoteInstallRunningServerPlan::LiveHandoff;
    }
    RemoteInstallRunningServerPlan::StopRequired(reason)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pre_floor_server_requires_update() {
        assert_eq!(
            remote_server_restart_reason(None, true, false, false, false),
            Some(RemoteServerRestartReason::EndpointProtocol)
        );
    }

    #[test]
    fn compatible_server_can_keep_running() {
        assert_eq!(
            remote_server_restart_reason(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                true,
                false,
                false,
                false
            ),
            None
        );
    }

    #[test]
    fn saved_endpoint_requires_surface_interest() {
        assert_eq!(
            remote_server_restart_reason(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                true,
                true,
                false,
                false
            ),
            Some(RemoteServerRestartReason::SurfaceInterest)
        );
    }

    #[test]
    fn saved_endpoint_requires_health_checks() {
        assert_eq!(
            remote_server_restart_reason(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                true,
                true,
                true,
                false
            ),
            Some(RemoteServerRestartReason::HealthCheck)
        );
    }

    #[test]
    fn saved_endpoint_accepts_complete_lifecycle_support() {
        assert_eq!(
            remote_server_restart_reason(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                true,
                true,
                true,
                true
            ),
            None
        );
    }

    #[test]
    fn old_daemon_requires_restart() {
        assert_eq!(
            remote_server_restart_reason(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                false,
                false,
                false,
                false
            ),
            Some(RemoteServerRestartReason::DaemonDetach)
        );
    }

    #[test]
    fn saved_endpoint_can_explicitly_live_handoff_for_surface_support() {
        assert_eq!(
            remote_install_running_server_plan(
                Some(crate::protocol::endpoint::ENDPOINT_PROTOCOL_GENERATION),
                true,
                false,
                false,
                true,
                true,
                true
            ),
            RemoteInstallRunningServerPlan::LiveHandoff
        );
    }
}