Skip to content

Commit

Permalink
feat(agent): add a debug agent (#28)
Browse files Browse the repository at this point in the history
* feat: rework branch with every changes

Signed-off-by: ESPIE <[email protected]>

* chore: fmt

Signed-off-by: ESPIE <[email protected]>

---------

Signed-off-by: ESPIE <[email protected]>
  • Loading branch information
remi-espie authored Apr 24, 2024
1 parent f176aa8 commit e3bd31d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ clap = { version = "4.5.4", features = ["derive"] }
rand = "0.8.5"
serde = { version = "1.0.197", features = ["derive"] }
toml = "0.8.12"

[features]
debug-agent = []
56 changes: 56 additions & 0 deletions src/agent/src/agents/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use super::AgentOutput;
use crate::agents::Agent;
use crate::{workload, AgentResult};
use std::fs::create_dir_all;
use std::time::SystemTime;

pub struct DebugAgent {
workload_config: workload::config::Config,
}

impl From<workload::config::Config> for DebugAgent {
fn from(workload_config: workload::config::Config) -> Self {
Self { workload_config }
}
}

impl Agent for DebugAgent {
fn prepare(&self) -> AgentResult<AgentOutput> {
let dir = format!("/tmp/{}", self.workload_config.workload_name);

println!("Function directory: {}", dir);

create_dir_all(&dir).expect("Unable to create directory");

std::fs::write(
format!("{}/debug.txt", &dir),
format!(
"Debug agent for {} - written at {:?}",
self.workload_config.workload_name,
SystemTime::now(),
),
)
.expect("Unable to write debug.txt file");

Ok(AgentOutput {
exit_code: 0,
stdout: "Build successfully!".into(),
stderr: String::default(),
})
}

fn run(&self) -> AgentResult<AgentOutput> {
let dir = format!("/tmp/{}", self.workload_config.workload_name);

let content = std::fs::read_to_string(format!("{}/debug.txt", &dir))
.expect("Unable to read debug.txt file");

std::fs::remove_dir_all(dir).expect("Unable to remove directory");

Ok(AgentOutput {
exit_code: 0,
stdout: content,
stderr: String::default(),
})
}
}
6 changes: 6 additions & 0 deletions src/agent/src/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::AgentResult;
use serde::Deserialize;

#[cfg(feature = "debug-agent")]
pub mod debug;
pub mod rust;

#[derive(Debug, Clone)]
Expand All @@ -19,12 +21,16 @@ pub trait Agent {
#[serde(rename_all = "kebab-case")]
pub enum Language {
Rust,
#[cfg(feature = "debug-agent")]
Debug,
}

impl std::fmt::Display for Language {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Language::Rust => write!(f, "rust"),
#[cfg(feature = "debug-agent")]
Language::Debug => write!(f, "debug"),
}
}
}
5 changes: 5 additions & 0 deletions src/agent/src/workload/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::{
AgentResult,
};

#[cfg(feature = "debug-agent")]
use crate::agents::debug;

/// Runner for a workload.
/// Will execute the workload based on the inner agent (language).
pub struct Runner {
Expand All @@ -15,6 +18,8 @@ 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())),
#[cfg(feature = "debug-agent")]
Language::Debug => Box::new(debug::DebugAgent::from(config.clone())),
};

Runner { config, agent }
Expand Down

0 comments on commit e3bd31d

Please sign in to comment.