forked from NationTech/harmony
51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use std::sync::{Arc, RwLock};
|
|
|
|
use log::info;
|
|
|
|
use super::{
|
|
interpret::{Interpret, InterpretError, Outcome},
|
|
inventory::Inventory,
|
|
score::Score,
|
|
topology::HAClusterTopology,
|
|
};
|
|
|
|
type ScoreVec = Vec<Box<dyn Score>>;
|
|
|
|
pub struct Maestro {
|
|
inventory: Inventory,
|
|
topology: HAClusterTopology,
|
|
scores: Arc<RwLock<ScoreVec>>,
|
|
}
|
|
|
|
impl Maestro {
|
|
pub fn new(inventory: Inventory, topology: HAClusterTopology) -> Self {
|
|
Self {
|
|
inventory,
|
|
topology,
|
|
scores: Arc::new(RwLock::new(Vec::new())),
|
|
}
|
|
}
|
|
|
|
pub fn start(&mut self) {
|
|
info!("Starting Maestro");
|
|
}
|
|
|
|
pub fn register_all(&mut self, mut scores: ScoreVec) {
|
|
let mut score_mut = self.scores.write().expect("Should acquire lock");
|
|
score_mut.append(&mut scores);
|
|
}
|
|
|
|
pub async fn interpret(&self, score: Box<dyn Score>) -> Result<Outcome, InterpretError> {
|
|
info!("Running score {score:?}");
|
|
let interpret = score.create_interpret();
|
|
info!("Launching interpret {interpret:?}");
|
|
let result = interpret.execute(&self.inventory, &self.topology).await;
|
|
info!("Got result {result:?}");
|
|
result
|
|
}
|
|
|
|
pub fn scores(&self) -> Arc<RwLock<ScoreVec>> {
|
|
self.scores.clone()
|
|
}
|
|
}
|