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>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | #[serde(tag = "op", rename_all = "snake_case")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | pub enum Request { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | Acquire(Acquire), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | Started { pid: i32 }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | Usage { rss: Bytes }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | Done, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | Status, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | Kill { lease: LeaseId }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | Extend { lease: LeaseId, by: Dur }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | Watch { lease: LeaseId }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | Overdue { session: String }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | #[derive(Clone, Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | pub struct LeaseInfo { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | pub id: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | pub cmd: Vec<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | pub cwd: PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | pub session: Option<String>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | pub need: Need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | pub heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | pub running: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | /// seconds since queued or started, whichever it's doing |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | pub elapsed: u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | pub rss: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | pub overdue: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | pub waiting: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | pub position: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | pub struct Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | pub leases: Vec<LeaseInfo>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | pub ram_free: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | pub ram_budget: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | pub cores_free: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | pub cores: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | #[derive(Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | #[serde(tag = "ev", rename_all = "snake_case")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | pub enum Event { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | lease: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | position: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | reason: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | Granted { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | lease: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | cores: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 78 | Kill, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 79 | Status(Status), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 80 | Overdue { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 81 | leases: Vec<LeaseInfo>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 82 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 83 | Fired { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 84 | overdue: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | Ok, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 88 | msg: String, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 90 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 91 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 92 | pub fn send<T: Serialize>(stream: &mut UnixStream, msg: &T) -> io::Result<()> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 93 | let mut line = serde_json::to_vec(msg)?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 94 | line.push(b'\n'); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 95 | stream.write_all(&line) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 96 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 97 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 98 | pub struct Reader(BufReader<UnixStream>); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 99 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 100 | impl Reader { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 101 | pub fn new(stream: &UnixStream) -> io::Result<Self> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 102 | Ok(Self(BufReader::new(stream.try_clone()?))) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 103 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 104 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 105 | /// `Ok(None)` on eof |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 106 | pub fn recv<T: DeserializeOwned>(&mut self) -> io::Result<Option<T>> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 107 | let mut line = String::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 108 | if self.0.read_line(&mut line)? == 0 { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 109 | return Ok(None); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 110 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 111 | serde_json::from_str(&line) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 112 | .map(Some) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 113 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 114 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 115 | } |