use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServicePaths {
home: PathBuf,
}
impl ServicePaths {
#[must_use]
pub fn new(home: impl Into<PathBuf>) -> ServicePaths {
ServicePaths { home: home.into() }
}
#[must_use]
pub fn home(&self) -> &Path {
&self.home
}
#[must_use]
pub fn lock(&self) -> PathBuf {
self.home.join("service.lock")
}
#[must_use]
pub fn replace_lock(&self) -> PathBuf {
self.home.join("service.replace.lock")
}
#[must_use]
pub fn info(&self) -> PathBuf {
self.home.join("service.json")
}
#[must_use]
pub fn socket(&self) -> PathBuf {
self.home.join("service.sock")
}
#[must_use]
pub fn log(&self) -> PathBuf {
self.home.join("service.log")
}
#[must_use]
pub fn read_info(&self) -> Option<ServiceInfo> {
let text = std::fs::read_to_string(self.info()).ok()?;
serde_json::from_str(&text).ok()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceInfo {
pub pid: u32,
pub started: String,
pub version: String,
pub protocol: u32,
pub endpoint: String,
pub epoch: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub build: Option<String>,
}
pub const BUILD_ENV: &str = "ISOTHRUM_SERVICE_BUILD";
#[must_use]
pub fn build_of(path: &Path) -> Option<String> {
let meta = std::fs::metadata(path).ok().filter(std::fs::Metadata::is_file)?;
let modified = meta.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?;
Some(format!("{}-{}", meta.len(), modified.as_nanos()))
}