repoint post_turn_counts + dedup web_ui stats onto todo_server::dial (#2635 inc 1)
This commit is contained in:
parent
e8d101d663
commit
e11e8294a3
3 changed files with 46 additions and 69 deletions
|
|
@ -35,6 +35,32 @@ fn socket_path() -> Option<PathBuf> {
|
|||
.map(PathBuf::from)
|
||||
}
|
||||
|
||||
/// Dial this same in-agent socket from elsewhere IN THIS PROCESS — the
|
||||
/// harness's own `web_ui` endpoints (`api_todos`, `/api/stats` reminder
|
||||
/// rollup) and `Surface::post_turn_counts` all need read access to the
|
||||
/// `Todos`/`Reminders` stores this module owns behind an `Arc` on a
|
||||
/// different spawned task, and a loopback dial is simpler than threading
|
||||
/// those `Arc`s through every caller. One-shot, best-effort (no retry,
|
||||
/// unlike the broker client): a connect failure means the socket server
|
||||
/// task itself isn't up, which a retry within one call wouldn't fix.
|
||||
pub(crate) async fn dial(req: &Request) -> Option<Response> {
|
||||
let path = socket_path()?;
|
||||
if !path.exists() {
|
||||
return None;
|
||||
}
|
||||
tokio::time::timeout(std::time::Duration::from_secs(3), async move {
|
||||
let mut stream = UnixStream::connect(&path).await.ok()?;
|
||||
let mut line = serde_json::to_string(req).ok()?;
|
||||
line.push('\n');
|
||||
stream.write_all(line.as_bytes()).await.ok()?;
|
||||
let mut lines = BufReader::new(stream).lines();
|
||||
let resp_line = lines.next_line().await.ok()??;
|
||||
serde_json::from_str(&resp_line).ok()
|
||||
})
|
||||
.await
|
||||
.ok()?
|
||||
}
|
||||
|
||||
/// Run the in-agent socket server: bind + accept loop, one request/response
|
||||
/// line per connection. A no-op (returns `Ok`) when `HIVE_AGENT_SOCKET` is
|
||||
/// unset, so a standalone harness without producers just skips it.
|
||||
|
|
|
|||
Loading…
Reference in a new issue