-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move rust and Agent trait into agents module and AgentRunne…
…r into workload module Signed-off-by: Matéo Fernandez <[email protected]> Signed-off-by: Martin Moreira de Jesus <[email protected]>
- Loading branch information
1 parent
30a60b5
commit ba094d6
Showing
6 changed files
with
105 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::AgentResult; | ||
use serde::Deserialize; | ||
|
||
pub mod rust; | ||
|
||
pub trait Agent { | ||
fn prepare(&self) -> AgentResult<()>; | ||
fn run(&self) -> AgentResult<()>; | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub enum Language { | ||
Rust, | ||
} | ||
|
||
impl std::fmt::Display for Language { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Language::Rust => write!(f, "rust"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,13 @@ | ||
mod agent; | ||
mod rust; | ||
|
||
use std::path::PathBuf; | ||
|
||
use agent::{Action, Agent, AgentConfig, AgentResult}; | ||
|
||
pub struct AgentRunner { | ||
config: AgentConfig, | ||
agent: Box<dyn Agent>, | ||
mod agents; | ||
pub mod workload { | ||
pub mod config; | ||
pub mod runner; | ||
} | ||
|
||
impl AgentRunner { | ||
pub fn new(config_path: String) -> Self { | ||
let config = AgentConfig::new_from_file(&PathBuf::from(config_path)).unwrap(); | ||
|
||
let agent: Box<dyn Agent> = match config.language.as_str() { | ||
"rust" => Box::new(rust::RustAgent::new_from_config(config.clone())), | ||
_ => panic!("Unsupported language: {}", config.language), | ||
}; | ||
|
||
AgentRunner { config, agent } | ||
} | ||
|
||
pub fn run(&self) -> AgentResult<()> { | ||
match self.config.action { | ||
Action::Prepare => self.agent.prepare()?, | ||
Action::Run => self.agent.run()?, | ||
Action::PrepareAndRun => { | ||
self.agent.prepare()?; | ||
self.agent.run()?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
#[derive(Debug)] | ||
pub enum AgentError { | ||
OpenConfigFileError(std::io::Error), | ||
ParseConfigError(toml::de::Error), | ||
} | ||
|
||
pub type AgentResult<T> = Result<T, AgentError>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
use agent::AgentRunner; | ||
use agent::workload::{config::Config, runner::Runner}; | ||
use clap::Parser; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Args { | ||
#[clap(short, long, default_value = "/etc/cloudlet/agent/config.toml")] | ||
config: String, | ||
config: PathBuf, | ||
} | ||
|
||
fn main() { | ||
let args = Args::parse(); | ||
|
||
let agent_runner = AgentRunner::new(args.config); | ||
let config = Config::from_file(&args.config).unwrap(); | ||
let runner = Runner::new(config); | ||
|
||
agent_runner.run().unwrap(); | ||
runner.run().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::config::{Action, Config}; | ||
use crate::{ | ||
agents::{rust, Agent, Language}, | ||
AgentResult, | ||
}; | ||
|
||
/// Runner for a workload. | ||
/// Will execute the workload based on the inner agent (language). | ||
pub struct Runner { | ||
config: Config, | ||
agent: Box<dyn Agent>, | ||
} | ||
|
||
impl Runner { | ||
pub fn new(config: Config) -> Self { | ||
let agent: Box<dyn Agent> = match config.language { | ||
Language::Rust => Box::new(rust::RustAgent::from(config.clone())), | ||
}; | ||
|
||
Runner { config, agent } | ||
} | ||
|
||
pub fn run(&self) -> AgentResult<()> { | ||
match self.config.action { | ||
Action::Prepare => self.agent.prepare()?, | ||
Action::Run => self.agent.run()?, | ||
Action::PrepareAndRun => { | ||
self.agent.prepare()?; | ||
self.agent.run()?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |