use crate::config::OxiosConfig;
use crate::cron::{CronJob, CronJobUpdate, CronScheduler};
use crate::event_bus::{EventBus, KernelEvent};
use crate::git_layer::{GitLayer, LogEntry};
use crate::resource_monitor::{ResourceMonitor, ResourceSnapshot};
use crate::scheduler::{AgentScheduler, ScheduledTask, SchedulerStats};
use std::sync::Arc;
use std::time::{Duration, Instant};
pub struct InfraApi {
pub(crate) git_layer: Arc<GitLayer>,
pub(crate) scheduler: Arc<AgentScheduler>,
pub(crate) cron_scheduler: Arc<CronScheduler>,
pub(crate) resource_monitor: Arc<ResourceMonitor>,
pub(crate) event_bus: EventBus,
pub(crate) config: OxiosConfig,
pub(crate) start_time: Instant,
}
impl InfraApi {
pub fn new(
git_layer: Arc<GitLayer>,
scheduler: Arc<AgentScheduler>,
cron_scheduler: Arc<CronScheduler>,
resource_monitor: Arc<ResourceMonitor>,
event_bus: EventBus,
config: OxiosConfig,
start_time: Instant,
) -> Self {
Self {
git_layer,
scheduler,
cron_scheduler,
resource_monitor,
event_bus,
config,
start_time,
}
}
pub fn git(&self) -> &GitLayer {
&self.git_layer
}
pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
self.git_layer.log(max)
}
pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
self.git_layer.tag(name, message)
}
pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
self.git_layer.restore_file(path, hash)
}
pub fn git_verify(&self) -> anyhow::Result<bool> {
self.git_layer.verify()
}
pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
self.git_layer.list_tags()
}
pub fn scheduler_stats(&self) -> SchedulerStats {
self.scheduler.stats()
}
pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
self.scheduler.queued_tasks()
}
pub fn running_tasks(&self) -> Vec<ScheduledTask> {
self.scheduler.running_tasks()
}
pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
self.cron_scheduler.add_job(job).await
}
pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
self.cron_scheduler.get_job(id)
}
pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
self.cron_scheduler.update_job(id, update).await
}
pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
self.cron_scheduler.remove_job(id).await
}
pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
self.cron_scheduler.trigger_job(id)
}
pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
self.cron_scheduler
.mark_job_completed(id, success, summary)
.await
}
pub fn list_crons(&self) -> Vec<CronJob> {
self.cron_scheduler.list_jobs()
}
pub fn resource_snapshot(&self) -> ResourceSnapshot {
self.resource_monitor.snapshot()
}
pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
self.resource_monitor.history(last_n)
}
pub fn is_overloaded(&self) -> bool {
self.resource_monitor.is_overloaded()
}
pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
self.event_bus.subscribe()
}
pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
self.event_bus
.publish(event)
.map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
}
pub fn config(&self) -> &OxiosConfig {
&self.config
}
pub fn uptime(&self) -> Duration {
self.start_time.elapsed()
}
}