6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | io::{self, BufRead, BufReader, Write}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | os::unix::net::UnixStream, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | path::PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | use serde::{Deserialize, Serialize, de::DeserializeOwned}; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | use crate::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | sched::{Need, Reason}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | units::{Bytes, Dur}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | pub type LeaseId = u64; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | #[derive(Clone, Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | pub struct Acquire { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | pub need: Need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | pub cwd: PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | pub cmd: Vec<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | pub session: Option<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | pub deadline: Option<Dur>, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 23 | #[serde(default)] |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 24 | pub exact: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | #[serde(tag = "op", rename_all = "snake_case")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | pub enum Request { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 30 | /// `None` withdraws it |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 31 | Expect { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 32 | session: String, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 33 | need: Option<Need>, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 34 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | Acquire(Acquire), |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 36 | Started { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 37 | pid: i32, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 38 | }, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 39 | Usage { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 40 | rss: Bytes, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 41 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | Done, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | Status, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 44 | Kill { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 45 | lease: LeaseId, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 46 | }, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 47 | Extend { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 48 | lease: LeaseId, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 49 | by: Dur, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 50 | }, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 51 | Watch { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 52 | lease: LeaseId, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 53 | }, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 54 | Overdue { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 55 | session: String, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 56 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | #[derive(Clone, Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | pub struct LeaseInfo { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | pub id: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | pub cmd: Vec<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | pub cwd: PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | pub session: Option<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | pub need: Need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | pub heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | pub running: bool, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 68 | #[serde(default)] |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 69 | pub granted: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | /// seconds since queued or started, whichever it's doing |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | pub elapsed: u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | pub rss: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | pub overdue: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | pub waiting: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | pub position: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 78 | #[derive(Clone, Debug, Serialize, Deserialize)] |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 79 | pub struct IntentInfo { |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 80 | pub session: String, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 81 | pub need: Need, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 82 | pub expires_in: u64, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 83 | } |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 84 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | pub struct Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | pub leases: Vec<LeaseInfo>, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 88 | pub intents: Vec<IntentInfo>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | pub ram_free: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 90 | pub ram_budget: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 91 | pub cores_free: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 92 | pub cores: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 93 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 94 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 95 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 96 | #[serde(tag = "ev", rename_all = "snake_case")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 97 | pub enum Event { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 98 | Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 99 | lease: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 100 | position: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 101 | reason: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 102 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 103 | Granted { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 104 | lease: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 105 | cores: u32, |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 106 | /// grows with any bonus cores, since more jobs means more ram |
ecc1cf8df0f5 | atagen | 2026-09-27 13:36 UTC | 107 | ram: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 108 | heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 109 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 110 | Kill, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 111 | Status(Status), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 112 | Overdue { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 113 | leases: Vec<LeaseInfo>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 114 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 115 | Fired { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 116 | overdue: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 117 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 118 | Ok, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 119 | Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 120 | msg: String, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 121 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 122 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 123 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 124 | pub fn send<T: Serialize>(stream: &mut UnixStream, msg: &T) -> io::Result<()> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 125 | let mut line = serde_json::to_vec(msg)?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 126 | line.push(b'\n'); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 127 | stream.write_all(&line) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 128 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 129 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 130 | pub struct Reader(BufReader<UnixStream>); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 131 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 132 | impl Reader { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 133 | pub fn new(stream: &UnixStream) -> io::Result<Self> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 134 | Ok(Self(BufReader::new(stream.try_clone()?))) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 135 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 136 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 137 | /// `Ok(None)` on eof |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 138 | pub fn recv<T: DeserializeOwned>(&mut self) -> io::Result<Option<T>> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 139 | let mut line = String::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 140 | if self.0.read_line(&mut line)? == 0 { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 141 | return Ok(None); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 142 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 143 | serde_json::from_str(&line) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 144 | .map(Some) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 145 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 146 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 147 | } |