restflow_core/services/
task.rs

1use crate::{
2    AppCore,
3    models::{Node, Task, TaskStatus},
4};
5use anyhow::{Context, Result};
6use serde_json::Value;
7use std::sync::Arc;
8
9pub async fn get_task_status(core: &Arc<AppCore>, task_id: &str) -> Result<Task> {
10    core.executor
11        .get_task_status(task_id)
12        .await
13        .with_context(|| format!("Failed to get status for task {}", task_id))
14}
15
16pub async fn get_execution_status(core: &Arc<AppCore>, execution_id: &str) -> Result<Vec<Task>> {
17    core.executor
18        .get_execution_status(execution_id)
19        .await
20        .with_context(|| format!("Failed to get execution status for {}", execution_id))
21}
22
23pub async fn list_tasks(
24    core: &Arc<AppCore>,
25    execution_id: Option<String>,
26    status: Option<TaskStatus>,
27    limit: Option<u32>,
28) -> Result<Vec<Task>> {
29    if let Some(exec_id) = execution_id {
30        let mut tasks = core
31            .executor
32            .get_execution_status(&exec_id)
33            .await
34            .with_context(|| format!("Failed to get tasks for execution {}", exec_id))?;
35
36        if let Some(status_filter) = status {
37            tasks.retain(|t| t.status == status_filter);
38        }
39
40        if let Some(limit) = limit {
41            tasks.truncate(limit as usize);
42        }
43
44        Ok(tasks)
45    } else {
46        let mut tasks = core
47            .executor
48            .list_tasks(None, status)
49            .await
50            .context("Failed to list tasks")?;
51
52        if let Some(limit) = limit {
53            tasks.truncate(limit as usize);
54        }
55
56        Ok(tasks)
57    }
58}
59
60pub async fn execute_node(core: &Arc<AppCore>, node: Node, input: Value) -> Result<String> {
61    core.executor
62        .submit_node(node, input)
63        .await
64        .context("Failed to execute node")
65}
66
67const MAX_PAGE_SIZE: usize = 100;
68
69pub async fn list_execution_history(
70    core: &Arc<AppCore>,
71    workflow_id: &str,
72    page: usize,
73    page_size: usize,
74) -> Result<crate::models::ExecutionHistoryPage> {
75    let page = if page == 0 { 1 } else { page };
76    let page_size = page_size.clamp(1, MAX_PAGE_SIZE);
77
78    core.storage
79        .execution_history
80        .list_paginated(workflow_id, page, page_size)
81        .context("Failed to load execution history")
82}