use crate::codec::{self, Wire};
use crate::mode::Context;
use crate::protocol::{Accepted, ApiRange, Hello, In, Out, Welcome, negotiate};
use crate::shm::Ring;
use crate::supervisor::{Decision, Restarts};
use std::io::{BufReader, BufWriter};
use std::path::PathBuf;
use std::process::{Child, ChildStdin, Command, Stdio};
use std::sync::Arc;
use std::sync::mpsc::{Receiver, TryRecvError, channel};
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct Spec {
pub name: String,
pub program: PathBuf,
pub args: Vec<String>,
pub env: Vec<(String, String)>,
pub shm: Option<(u32, usize)>,
pub features: Vec<String>,
}
#[derive(Debug, Clone, Default)]
pub struct Grants {
pub granted: Vec<String>,
pub denied: Vec<(String, String)>,
}
pub struct Connection {
child: Child,
tx: BufWriter<ChildStdin>,
rx: Receiver<Out>,
pub hello: Hello,
pub api: u32,
pub ring: Option<Ring>,
}
pub type Notify = Arc<dyn Fn() + Send + Sync>;
pub const HELLO_TIMEOUT: Duration = Duration::from_secs(5);
pub fn connect(
spec: &Spec,
ctx: Context,
restore: Option<Vec<u8>>,
grant: &mut dyn FnMut(&Hello) -> Grants,
notify: Option<Notify>,
) -> Result<Connection, String> {
let mut cmd = Command::new(&spec.program);
cmd.args(&spec.args).envs(spec.env.iter().map(|(k, v)| (k, v))).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::inherit());
let mut child = cmd.spawn().map_err(|e| format!("can't start {}: {e}", spec.program.display()))?;
let mut tx = BufWriter::new(child.stdin.take().ok_or("no stdin")?);
let mut out = BufReader::with_capacity(1 << 20, child.stdout.take().ok_or("no stdout")?);
let (send, rx) = channel();
std::thread::Builder::new()
.name(format!("plugin-{}", spec.name))
.spawn(move || {
let mut buf = Vec::new();
while let Ok(Some(m)) = codec::read::<Out>(&mut out, Wire::MsgPack, &mut buf) {
if send.send(m).is_err() {
break;
}
if let Some(n) = ¬ify {
n();
}
}
drop(send);
if let Some(n) = ¬ify {
n();
}
})
.map_err(|e| e.to_string())?;
let fail = |mut child: Child, why: String| {
let _ = child.kill();
let _ = child.wait();
Err(why)
};
let hello = match rx.recv_timeout(HELLO_TIMEOUT) {
Ok(Out::Hello(h)) => h,
Ok(_) => return fail(child, "plugin spoke before saying hello".into()),
Err(_) => return fail(child, format!("{} sent no hello", spec.name)),
};
let api = match negotiate(ApiRange::CURRENT, hello.api) {
Ok(a) => a,
Err(reason) => {
let _ = codec::write_named(&mut tx, Wire::MsgPack, &Welcome::Rejected { reason: reason.clone() });
return fail(child, reason);
}
};
let g = grant(&hello);
let ring = match (spec.shm, hello.shm) {
(Some((slots, bytes)), true) => Ring::create(&spec.name, slots, bytes).ok(),
_ => None,
};
let accepted = Accepted {
api,
session: format!("{}-{}", spec.name, child.id()),
ctx,
granted: g.granted,
denied: g.denied,
shm: ring.as_ref().map(Ring::spec),
mcp: None,
restore,
features: spec.features.clone(),
};
if let Err(e) = codec::write_named(&mut tx, Wire::MsgPack, &Welcome::Accepted(Box::new(accepted))) {
return fail(child, e.to_string());
}
Ok(Connection { child, tx, rx, hello, api, ring })
}
impl Connection {
pub fn send(&mut self, m: &In) -> std::io::Result<()> {
codec::write_named(&mut self.tx, Wire::MsgPack, m)
}
pub fn stop(mut self, reason: &str, grace: Duration) {
let _ = self.send(&In::Stopping { reason: reason.into() });
let end = Instant::now() + grace;
while Instant::now() < end {
if let Ok(Some(_)) = self.child.try_wait() {
return;
}
std::thread::sleep(Duration::from_millis(10));
}
let _ = self.child.kill();
let _ = self.child.wait();
}
}
impl Drop for Connection {
fn drop(&mut self) {
if let Ok(None) = self.child.try_wait() {
let _ = self.child.kill();
}
let _ = self.child.wait();
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum State {
Running,
Waiting {
until: Instant,
crashes: u32,
},
Disabled {
reason: String,
},
}
pub struct Supervisor {
pub spec: Spec,
ctx: Context,
grant: Box<dyn FnMut(&Hello) -> Grants + Send>,
restarts: Restarts,
conn: Option<Connection>,
state: State,
saved: Option<Vec<u8>>,
crashes: u32,
notify: Option<Notify>,
}
impl Supervisor {
pub fn new(spec: Spec, ctx: Context, max_restarts: u32, grant: impl FnMut(&Hello) -> Grants + Send + 'static) -> Self {
Self::with_restarts(spec, ctx, Restarts::new(max_restarts), grant)
}
pub fn with_restarts(spec: Spec, ctx: Context, restarts: Restarts, grant: impl FnMut(&Hello) -> Grants + Send + 'static) -> Self {
let mut s = Self {
spec,
ctx,
grant: Box::new(grant),
restarts,
conn: None,
state: State::Waiting { until: Instant::now(), crashes: 0 },
saved: None,
crashes: 0,
notify: None,
};
s.start(Instant::now());
s
}
pub fn with_notify(spec: Spec, ctx: Context, restarts: Restarts, notify: Notify, grant: impl FnMut(&Hello) -> Grants + Send + 'static) -> Self {
let mut s = Self {
spec,
ctx,
grant: Box::new(grant),
restarts,
conn: None,
state: State::Waiting { until: Instant::now(), crashes: 0 },
saved: None,
crashes: 0,
notify: Some(notify),
};
s.start(Instant::now());
s
}
pub fn set_ctx(&mut self, ctx: Context) {
self.ctx = ctx;
}
pub fn state(&self) -> &State {
&self.state
}
pub fn connection(&self) -> Option<&Connection> {
self.conn.as_ref()
}
fn start(&mut self, now: Instant) {
match connect(&self.spec, self.ctx, self.saved.clone(), &mut *self.grant, self.notify.clone()) {
Ok(c) => {
self.conn = Some(c);
self.state = State::Running;
}
Err(e) if e.contains("update") => self.state = State::Disabled { reason: e },
Err(_) => self.crashed(now),
}
}
fn crashed(&mut self, now: Instant) {
self.conn = None;
self.crashes += 1;
self.state = match self.restarts.crashed(now) {
Decision::Restart(wait) => State::Waiting { until: now + wait, crashes: self.crashes },
Decision::GiveUp => State::Disabled { reason: format!("{} stopped after {} crashes", self.spec.name, self.crashes) },
};
}
pub fn send(&mut self, m: &In, now: Instant) -> bool {
let ok = self.conn.as_mut().is_some_and(|c| c.send(m).is_ok());
if !ok && self.state == State::Running {
self.crashed(now);
}
ok
}
pub fn poll(&mut self, now: Instant) -> Vec<Out> {
let mut out = Vec::new();
if let State::Waiting { until, .. } = self.state
&& now >= until
{
self.start(now);
}
let mut dead = false;
if let Some(c) = &mut self.conn {
loop {
match c.rx.try_recv() {
Ok(Out::StateSave(b)) => self.saved = Some(b),
Ok(m) => out.push(m),
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => {
dead = true;
break;
}
}
}
}
if dead {
self.crashed(now);
}
out
}
pub fn restart(&mut self, now: Instant) {
if let Some(c) = self.conn.take() {
c.stop("restart", Duration::from_millis(500));
}
self.restarts.reset();
self.crashes = 0;
self.start(now);
}
pub fn stop(&mut self, reason: &str) {
if let Some(c) = self.conn.take() {
c.stop(reason, Duration::from_millis(500));
}
self.state = State::Disabled { reason: reason.into() };
}
pub fn status(&self, now: Instant) -> Option<String> {
match &self.state {
State::Running => None,
State::Waiting { until, .. } => {
Some(format!("{} crashed, restarting in {} s", self.spec.name, until.saturating_duration_since(now).as_secs_f32().ceil() as u64))
}
State::Disabled { reason } => Some(format!("{reason} (restart)")),
}
}
}