Create jobkit-daemon crate with generic daemon infrastructure: - event_log: JSONL append with size-based rotation - socket: Unix domain socket RPC client and server with signal handling - status: JSON status file read/write Migrate daemon.rs to use the library: - Worker pool setup via Daemon::new() - Socket loop + signal handling via Daemon::run() - RPC handlers as registered closures - Logging, status writing, send_rpc all delegate to library Migrate tui.rs to use socket::send_rpc() instead of inline UnixStream. daemon.rs: 1952 → 1806 lines (-146), old status_socket_loop removed. tui.rs: socket boilerplate removed. Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
29 lines
863 B
Rust
29 lines
863 B
Rust
// Status file management
|
|
//
|
|
// Writes a JSON status snapshot to data_dir/daemon-status.json.
|
|
// Applications provide their own status struct (must impl Serialize).
|
|
|
|
use std::fs;
|
|
use std::path::Path;
|
|
|
|
fn status_path(data_dir: &Path) -> std::path::PathBuf {
|
|
data_dir.join("daemon-status.json")
|
|
}
|
|
|
|
/// Write a status snapshot to the status file.
|
|
pub fn write<S: serde::Serialize>(data_dir: &Path, status: &S) {
|
|
if let Ok(json) = serde_json::to_string_pretty(status) {
|
|
let _ = fs::write(status_path(data_dir), json);
|
|
}
|
|
}
|
|
|
|
/// Read the status file as a string.
|
|
pub fn read(data_dir: &Path) -> Option<String> {
|
|
fs::read_to_string(status_path(data_dir)).ok()
|
|
}
|
|
|
|
/// Read and deserialize the status file.
|
|
pub fn load<S: serde::de::DeserializeOwned>(data_dir: &Path) -> Option<S> {
|
|
let s = read(data_dir)?;
|
|
serde_json::from_str(&s).ok()
|
|
}
|