Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agent): add a debug agent #28

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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