2024-11-24 21:08:37 +01:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
use crate::agent::models::Commands;
|
|
|
|
|
|
|
|
use super::{agent_state::AgentState, queue::AgentQueue};
|
2024-11-24 00:53:43 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AgentRefresh {
|
2024-11-24 17:12:15 +01:00
|
|
|
process_host: String,
|
2024-11-24 21:08:37 +01:00
|
|
|
queue: AgentQueue,
|
2024-11-24 00:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AgentRefresh {
|
2024-11-24 17:12:15 +01:00
|
|
|
pub fn new(state: impl Into<AgentState>) -> Self {
|
|
|
|
let state: AgentState = state.into();
|
2024-11-24 00:53:43 +01:00
|
|
|
Self {
|
2024-11-24 17:12:15 +01:00
|
|
|
process_host: state.discovery.process_host.clone(),
|
2024-11-24 21:08:37 +01:00
|
|
|
queue: state.queue.clone(),
|
2024-11-24 00:53:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl notmad::Component for AgentRefresh {
|
2024-11-24 17:12:15 +01:00
|
|
|
fn name(&self) -> Option<String> {
|
|
|
|
Some("agent_refresh".into())
|
|
|
|
}
|
|
|
|
|
2024-11-24 00:53:43 +01:00
|
|
|
async fn run(
|
|
|
|
&self,
|
|
|
|
cancellation_token: tokio_util::sync::CancellationToken,
|
|
|
|
) -> Result<(), notmad::MadError> {
|
2025-01-05 20:50:49 +01:00
|
|
|
// let cancel =
|
|
|
|
// nodrift::schedule_drifter(std::time::Duration::from_secs(60 * 10), self.clone());
|
|
|
|
let cancel = nodrift::schedule_drifter(std::time::Duration::from_secs(5), self.clone());
|
2024-11-24 00:53:43 +01:00
|
|
|
tokio::select! {
|
|
|
|
_ = cancel.cancelled() => {},
|
|
|
|
_ = cancellation_token.cancelled() => {
|
|
|
|
tracing::debug!("cancelling agent refresh");
|
|
|
|
cancel.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl nodrift::Drifter for AgentRefresh {
|
|
|
|
async fn execute(&self, _token: tokio_util::sync::CancellationToken) -> anyhow::Result<()> {
|
2024-11-24 17:12:15 +01:00
|
|
|
tracing::info!(process_host = self.process_host, "refreshing agent");
|
2024-11-24 00:53:43 +01:00
|
|
|
|
2024-11-24 21:08:37 +01:00
|
|
|
self.queue
|
|
|
|
.publish(Commands::ScheduleTask {
|
|
|
|
task: "update".into(),
|
|
|
|
properties: BTreeMap::default(),
|
|
|
|
})
|
|
|
|
.await?;
|
2024-11-24 00:53:43 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|