plane_core/messages/
scheduler.rs

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
use super::agent::{DockerExecutableConfig, SpawnRequest};
use crate::{
    nats::{SubscribeSubject, TypedMessage},
    types::{BackendId, ClusterName, DroneId},
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use serde_with::DurationSeconds;
use std::{collections::HashMap, time::Duration};

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ScheduleRequest {
    pub cluster: ClusterName,

    /// The name of the backend. This forms part of the hostname used to
    /// connect to the drone.
    pub backend_id: Option<BackendId>,

    /// The timeout after which the drone is shut down if no connections are made.
    #[serde_as(as = "DurationSeconds")]
    pub max_idle_secs: Duration,

    /// Metadata for the spawn. Typically added to log messages for debugging and observability.
    pub metadata: HashMap<String, String>,

    /// Configuration for docker run (image, creds, env vars etc.)
    pub executable: DockerExecutableConfig,
}

impl ScheduleRequest {
    pub fn schedule(&self, drone_id: &DroneId) -> SpawnRequest {
        let backend_id = self
            .backend_id
            .clone()
            .unwrap_or_else(BackendId::new_random);

        SpawnRequest {
            drone_id: drone_id.clone(),
            backend_id,
            max_idle_secs: self.max_idle_secs.clone(),
            metadata: self.metadata.clone(),
            executable: self.executable.clone(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ScheduleResponse {
    Scheduled {
        drone: DroneId,
        backend_id: BackendId,
    },
    NoDroneAvailable,
}

impl TypedMessage for ScheduleRequest {
    type Response = ScheduleResponse;

    fn subject(&self) -> String {
        format!("cluster.{}.schedule", self.cluster.subject_name())
    }
}

impl ScheduleRequest {
    pub fn subscribe_subject() -> SubscribeSubject<Self> {
        SubscribeSubject::new("cluster.*.schedule".into())
    }
}