scheduled tasks

This commit is contained in:
pavel 2026-02-10 19:32:33 +01:00
commit 937827d08b
17 changed files with 1445 additions and 136 deletions

View file

@ -1,23 +1,28 @@
use axum::{
Json, Router,
extract::{Path, State},
http::{Method, StatusCode},
http::StatusCode,
routing::{get, post},
};
use chrono::Utc;
use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryOrder, Set};
use sea_orm::{
ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryOrder, QuerySelect, Set,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};
use tower_http::cors::CorsLayer;
use uuid::Uuid;
use crate::agent::Agent;
use crate::entities::task::{self, Entity as Task};
use crate::entities::task_run::{self, Entity as TaskRun};
use crate::scheduler::Scheduler;
use migration::{Migrator, MigratorTrait};
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
pub scheduler: Arc<Scheduler>,
pub zen_api_key: Option<String>,
pub tavily_api_key: Option<String>,
}
@ -25,18 +30,42 @@ pub struct AppState {
#[derive(Deserialize)]
pub struct CreateTaskRequest {
pub goal: String,
pub cron: Option<String>,
}
#[derive(Deserialize)]
pub struct UpdateTaskRequest {
pub goal: String,
pub cron: Option<String>,
}
#[derive(Serialize)]
pub struct TaskResponse {
pub id: Uuid,
pub goal: String,
pub cron: Option<String>,
pub created_at: chrono::DateTime<chrono::FixedOffset>,
pub runs: Vec<TaskRunResponse>,
}
#[derive(Serialize)]
pub struct TaskRunResponse {
pub id: Uuid,
pub status: String,
pub logs: String,
pub answer: Option<String>,
pub created_at: chrono::DateTime<chrono::FixedOffset>,
}
#[derive(Serialize)]
pub struct RecentRunResponse {
pub id: Uuid,
pub task_id: Uuid,
pub goal: String,
pub status: String,
pub created_at: chrono::DateTime<chrono::FixedOffset>,
}
pub async fn start(db_url: &str) -> Result<(), Box<dyn std::error::Error>> {
let db = Database::connect(db_url).await?;
Migrator::up(&db, None).await?;
@ -44,20 +73,31 @@ pub async fn start(db_url: &str) -> Result<(), Box<dyn std::error::Error>> {
let zen_api_key = std::env::var("ZEN_API_KEY").ok();
let tavily_api_key = std::env::var("TAVILY_API_KEY").ok();
let scheduler =
Arc::new(Scheduler::new(db.clone(), zen_api_key.clone(), tavily_api_key.clone()).await?);
// Load existing scheduled tasks
let existing_tasks = Task::find().all(&db).await?;
for task in existing_tasks {
if let Some(cron) = task.cron {
let _ = scheduler.add_task_job(task.id, &cron).await;
}
}
let state = Arc::new(AppState {
db,
scheduler,
zen_api_key,
tavily_api_key,
});
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods([Method::GET, Method::POST])
.allow_headers(Any);
let cors = CorsLayer::permissive();
let app = Router::new()
.route("/tasks", post(create_task).get(list_tasks))
.route("/tasks/:id", get(get_task))
.route("/tasks/:id", get(get_task).put(update_task))
.route("/tasks/:id/runs", post(rerun_task))
.route("/runs/recent", get(get_recent_runs))
.layer(cors)
.with_state(state);
@ -72,6 +112,7 @@ async fn list_tasks(
State(state): State<Arc<AppState>>,
) -> Result<Json<Vec<TaskResponse>>, (StatusCode, String)> {
let tasks = Task::find()
.find_with_related(TaskRun)
.order_by_desc(task::Column::CreatedAt)
.all(&state.db)
.await
@ -79,13 +120,21 @@ async fn list_tasks(
let response = tasks
.into_iter()
.map(|t| TaskResponse {
.map(|(t, runs)| TaskResponse {
id: t.id,
goal: t.goal,
status: t.status,
logs: t.logs,
answer: t.answer,
cron: t.cron,
created_at: t.created_at,
runs: runs
.into_iter()
.map(|r| TaskRunResponse {
id: r.id,
status: r.status,
logs: r.logs,
answer: r.answer,
created_at: r.created_at,
})
.collect(),
})
.collect();
@ -98,13 +147,11 @@ async fn create_task(
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
let task_id = Uuid::new_v4();
// Initial save
// Initial task save
let new_task = task::ActiveModel {
id: Set(task_id),
goal: Set(payload.goal.clone()),
status: Set("running".to_string()),
logs: Set(String::new()),
answer: Set(None),
cron: Set(payload.cron.clone()),
created_at: Set(Utc::now().into()),
};
@ -113,14 +160,54 @@ async fn create_task(
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// Run agent in the background or synchronously for now as requested
// "it can be created and executed on the agent with an api. the resulting logs after execution will get returned by the endpoint"
// This implies we wait for it to finish.
if let Some(cron) = &payload.cron {
let _ = state.scheduler.add_task_job(task_id, cron).await;
} else {
let _ = state.scheduler.remove_task_job(task_id).await;
}
execute_agent_run(state, task_id, payload.goal).await
}
async fn rerun_task(
State(state): State<Arc<AppState>>,
Path(id): Path<Uuid>,
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
let task = Task::find_by_id(id)
.one(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.ok_or((StatusCode::NOT_FOUND, "Task not found".to_string()))?;
execute_agent_run(state, task.id, task.goal).await
}
async fn execute_agent_run(
state: Arc<AppState>,
task_id: Uuid,
goal: String,
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
let run_id = Uuid::new_v4();
// Initial run save
let new_run = task_run::ActiveModel {
id: Set(run_id),
task_id: Set(task_id),
status: Set("running".to_string()),
logs: Set(String::new()),
answer: Set(None),
created_at: Set(Utc::now().into()),
};
new_run
.insert(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut agent = Agent::new(
state.zen_api_key.clone(),
state.tavily_api_key.clone(),
payload.goal.clone(),
goal.clone(),
);
let (logs, answer) = match agent.run().await {
@ -129,51 +216,111 @@ async fn create_task(
};
// Update with final logs and status
let mut task: task::ActiveModel = Task::find_by_id(task_id)
let mut run: task_run::ActiveModel = TaskRun::find_by_id(run_id)
.one(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.ok_or((
StatusCode::NOT_FOUND,
"Task not found after insert".to_string(),
"Run not found after insert".to_string(),
))?
.into();
task.logs = Set(logs.clone());
task.answer = Set(answer.clone());
task.status = Set("completed".to_string());
run.logs = Set(logs.clone());
run.answer = Set(answer.clone());
run.status = Set("completed".to_string());
let updated_task = task
.update(&state.db)
run.update(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(TaskResponse {
id: updated_task.id,
goal: updated_task.goal,
status: updated_task.status,
logs: updated_task.logs,
answer: updated_task.answer,
created_at: updated_task.created_at,
}))
get_task(Path(task_id), State(state)).await
}
async fn update_task(
State(state): State<Arc<AppState>>,
Path(id): Path<Uuid>,
Json(payload): Json<UpdateTaskRequest>,
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
let mut task: task::ActiveModel = Task::find_by_id(id)
.one(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.ok_or((StatusCode::NOT_FOUND, "Task not found".to_string()))?
.into();
task.goal = Set(payload.goal.clone());
task.cron = Set(payload.cron.clone());
task.update(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
if let Some(cron) = &payload.cron {
let _ = state.scheduler.add_task_job(id, cron).await;
} else {
let _ = state.scheduler.remove_task_job(id).await;
}
get_task(Path(id), State(state)).await
}
async fn get_task(
Path(id): Path<Uuid>,
State(state): State<Arc<AppState>>,
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
let task = Task::find_by_id(id)
.one(&state.db)
let results = Task::find_by_id(id)
.find_with_related(TaskRun)
.all(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let (task, runs) = results
.into_iter()
.next()
.ok_or((StatusCode::NOT_FOUND, "Task not found".to_string()))?;
Ok(Json(TaskResponse {
id: task.id,
goal: task.goal,
status: task.status,
logs: task.logs,
answer: task.answer,
cron: task.cron,
created_at: task.created_at,
runs: runs
.into_iter()
.map(|r| TaskRunResponse {
id: r.id,
status: r.status,
logs: r.logs,
answer: r.answer,
created_at: r.created_at,
})
.collect(),
}))
}
async fn get_recent_runs(
State(state): State<Arc<AppState>>,
) -> Result<Json<Vec<RecentRunResponse>>, (StatusCode, String)> {
let results = TaskRun::find()
.find_also_related(Task)
.order_by_desc(task_run::Column::CreatedAt)
.limit(50)
.all(&state.db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let response = results
.into_iter()
.filter_map(|(run, task_opt)| {
task_opt.map(|task| RecentRunResponse {
id: run.id,
task_id: run.task_id,
goal: task.goal,
status: run.status,
created_at: run.created_at,
})
})
.collect();
Ok(Json(response))
}