Repository navigation
Expand file tree
/
Copy pathexecutor.rs
More file actions
101 lines (87 loc) · 3.04 KB
/
Copy pathexecutor.rs
File metadata and controls
101 lines (87 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Task executor for running DAG tasks.
use anyhow::{Context, Result, bail};
use cortex_agents::task::{Task, TaskStatus};
use std::time::{Duration, Instant};
use crate::styled_output::print_info;
use super::types::TaskExecutionResult;
/// Task executor that runs the actual task commands.
pub struct TaskExecutor {
timeout: Duration,
verbose: bool,
}
impl TaskExecutor {
pub fn new(timeout_secs: u64, verbose: bool) -> Self {
Self {
timeout: Duration::from_secs(timeout_secs),
verbose,
}
}
/// Execute a single task.
pub async fn execute(&self, task: &Task) -> TaskExecutionResult {
let start = Instant::now();
let task_id = task.id.expect("Task must have an ID");
// Get command from metadata if available
let command = task
.metadata
.get("command")
.and_then(|v| v.as_str())
.map(String::from);
if self.verbose {
if let Some(ref cmd) = command {
print_info(&format!("Executing task '{}': {}", task.name, cmd));
} else {
print_info(&format!(
"Executing task '{}': {}",
task.name, task.description
));
}
}
// If there's a command, execute it
let (status, output, error) = if let Some(cmd) = command {
match self.run_command(&cmd).await {
Ok(output) => (TaskStatus::Completed, Some(output), None),
Err(e) => (TaskStatus::Failed, None, Some(e.to_string())),
}
} else {
// Simulated task execution (no command)
// In a real system, this would delegate to an agent or external executor
(
TaskStatus::Completed,
Some(format!("Task '{}' completed (no command)", task.name)),
None,
)
};
TaskExecutionResult {
task_id,
task_name: task.name.clone(),
status,
duration: start.elapsed(),
output,
error,
}
}
/// Run a shell command with timeout.
async fn run_command(&self, cmd: &str) -> Result<String> {
let timeout_duration = self.timeout;
let result = tokio::time::timeout(timeout_duration, async {
let output = tokio::process::Command::new("sh")
.arg("-c")
.arg(cmd)
.output()
.await
.context("Failed to execute command")?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
Ok(stdout)
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
bail!("Command failed: {}", stderr);
}
})
.await;
match result {
Ok(inner_result) => inner_result,
Err(_) => bail!("Task timed out after {:?}", timeout_duration),
}
}
}