- Added `JobType`, `SessionTarget`, `Schedule`, `DeliveryConfig`, `CronJob`, `CronRun`, and `CronJobPatch` types in `src/cron/types.rs` for cron job configuration and management. - Introduced `CronAddTool`, `CronListTool`, `CronRemoveTool`, `CronRunTool`, `CronRunsTool`, and `CronUpdateTool` in `src/tools` for adding, listing, removing, running, and updating cron jobs. - Updated the `run` function in `src/daemon/mod.rs` to conditionally start the scheduler based on the cron configuration. - Modified command-line argument parsing in `src/lib.rs` and `src/main.rs` to support new cron job commands. - Enhanced the onboarding wizard in `src/onboard/wizard.rs` to include cron configuration. - Added tests for cron job tools to ensure functionality and error handling.
114 lines
3.1 KiB
Rust
114 lines
3.1 KiB
Rust
use super::traits::{Tool, ToolResult};
|
|
use crate::config::Config;
|
|
use crate::cron;
|
|
use async_trait::async_trait;
|
|
use serde_json::json;
|
|
use std::sync::Arc;
|
|
|
|
pub struct CronRemoveTool {
|
|
config: Arc<Config>,
|
|
}
|
|
|
|
impl CronRemoveTool {
|
|
pub fn new(config: Arc<Config>) -> Self {
|
|
Self { config }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Tool for CronRemoveTool {
|
|
fn name(&self) -> &str {
|
|
"cron_remove"
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Remove a cron job by id"
|
|
}
|
|
|
|
fn parameters_schema(&self) -> serde_json::Value {
|
|
json!({
|
|
"type": "object",
|
|
"properties": {
|
|
"job_id": { "type": "string" }
|
|
},
|
|
"required": ["job_id"]
|
|
})
|
|
}
|
|
|
|
async fn execute(&self, args: serde_json::Value) -> anyhow::Result<ToolResult> {
|
|
if !self.config.cron.enabled {
|
|
return Ok(ToolResult {
|
|
success: false,
|
|
output: String::new(),
|
|
error: Some("cron is disabled by config (cron.enabled=false)".to_string()),
|
|
});
|
|
}
|
|
|
|
let job_id = match args.get("job_id").and_then(serde_json::Value::as_str) {
|
|
Some(v) if !v.trim().is_empty() => v,
|
|
_ => {
|
|
return Ok(ToolResult {
|
|
success: false,
|
|
output: String::new(),
|
|
error: Some("Missing 'job_id' parameter".to_string()),
|
|
});
|
|
}
|
|
};
|
|
|
|
match cron::remove_job(&self.config, job_id) {
|
|
Ok(()) => Ok(ToolResult {
|
|
success: true,
|
|
output: format!("Removed cron job {job_id}"),
|
|
error: None,
|
|
}),
|
|
Err(e) => Ok(ToolResult {
|
|
success: false,
|
|
output: String::new(),
|
|
error: Some(e.to_string()),
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::config::Config;
|
|
use tempfile::TempDir;
|
|
|
|
fn test_config(tmp: &TempDir) -> Arc<Config> {
|
|
let config = Config {
|
|
workspace_dir: tmp.path().join("workspace"),
|
|
config_path: tmp.path().join("config.toml"),
|
|
..Config::default()
|
|
};
|
|
std::fs::create_dir_all(&config.workspace_dir).unwrap();
|
|
Arc::new(config)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn removes_existing_job() {
|
|
let tmp = TempDir::new().unwrap();
|
|
let cfg = test_config(&tmp);
|
|
let job = cron::add_job(&cfg, "*/5 * * * *", "echo ok").unwrap();
|
|
let tool = CronRemoveTool::new(cfg.clone());
|
|
|
|
let result = tool.execute(json!({"job_id": job.id})).await.unwrap();
|
|
assert!(result.success);
|
|
assert!(cron::list_jobs(&cfg).unwrap().is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn errors_when_job_id_missing() {
|
|
let tmp = TempDir::new().unwrap();
|
|
let cfg = test_config(&tmp);
|
|
let tool = CronRemoveTool::new(cfg);
|
|
|
|
let result = tool.execute(json!({})).await.unwrap();
|
|
assert!(!result.success);
|
|
assert!(result
|
|
.error
|
|
.unwrap_or_default()
|
|
.contains("Missing 'job_id'"));
|
|
}
|
|
}
|