Files
horizon-airdef/crates/horizon-prepare/src/worker.rs
T
rdbtCVS f45e3f1237 Optimize launcher preparation with precompiled, batched exporters
Compile and validate original exporter bundles during release builds, cache development compilation, and reuse initialized JVMs and collision data during preparation. Keep legacy preparers compatible while current releases need only a Java runtime.

Co-Authored-By: Codex <noreply@openai.com>
2026-10-25 23:11:23 +00:00

80 lines
2.5 KiB
Rust

//! A bounded request/reply channel to one initialized exporter JVM.
use crate::*;
use std::io::{BufRead, BufReader, Write};
use std::process::{Child, ChildStdin, Stdio};
use std::sync::mpsc::{self, Receiver};
pub(crate) struct Worker {
child: Child,
input: ChildStdin,
replies: Receiver<std::io::Result<String>>,
}
impl Worker {
pub(crate) fn new(run: &Run, classes: &Path, cp: &str) -> Result<Self, String> {
let log = run.log_file(&run.local.join(format!(
"{}-worker.log",
classes.file_name().unwrap().to_string_lossy()
)))?;
let mut child = java::command(&run.jdk.java)
.args([
"-Xmx2G",
"-cp",
&format!(
"{}{}{}",
classes.display(),
if cfg!(windows) { ";" } else { ":" },
cp
),
"dev.horizonlab.parity.PrepareWorker",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(log)
.spawn()
.map_err(|e| e.to_string())?;
let input = child.stdin.take().unwrap();
let output = child.stdout.take().unwrap();
let (send, replies) = mpsc::channel();
std::thread::spawn(move || {
for line in BufReader::new(output).lines() {
if send.send(line).is_err() {
break;
}
}
});
Ok(Self {
child,
input,
replies,
})
}
pub(crate) fn run(&mut self, main: &str, args: &[String], log: &Path) -> Result<(), String> {
let task = Value::Array(vec![
Value::Str(arg(log)),
Value::Str(main.into()),
Value::Array(args.iter().cloned().map(Value::Str).collect()),
]);
writeln!(self.input, "{}", pyjson::dumps(&task, &pyjson::COMPACT))
.map_err(|e| e.to_string())?;
self.input.flush().map_err(|e| e.to_string())?;
let reply = self
.replies
.recv_timeout(std::time::Duration::from_secs(3600))
.map_err(|e| format!("Exporter JVM stopped or timed out: {e}"))?
.map_err(|e| e.to_string())?;
match pyjson::loads(&reply)?.as_str() {
Some("") => Ok(()),
_ => Err(format!("Exporter {main} failed: {reply}")),
}
}
}
impl Drop for Worker {
fn drop(&mut self) {
let _ = self.child.kill();
let _ = self.child.wait();
}
}