返回 CodeWhale
automations.rs
根目录 / crates / app-server / src / runtime_api / automations.rs
1 use axum::Json;
2 use axum::extract::{Path, State};
3 use axum::http::{HeaderMap, StatusCode};
4 use axum::response::Response;
5 use serde::{Deserialize, Serialize};
6
7 use codewhale_engine::automation_manager::{
8 AutomationMissedPolicy, AutomationMissedStatus, MissedRunDecision, resolve_missed_run_shared,
9 };
10
11 use super::{ApiError, RuntimeApiState, automation_mutation, map_automation_err};
12
13 #[derive(Debug, Clone, Serialize, Deserialize)]
14 pub(super) struct MissedPolicyRequest {
15 #[serde(default)]
16 policy: Option<AutomationMissedPolicy>,
17 }
18
19 pub(super) async fn list_missed(
20 State(state): State<RuntimeApiState>,
21 ) -> Result<Json<Vec<AutomationMissedStatus>>, ApiError> {
22 let manager = state.automations.lock().await;
23 let missed = manager
24 .list_missed()
25 .map_err(|e| ApiError::internal(format!("Failed to list missed automations: {e}")))?;
26 Ok(Json(missed))
27 }
28
29 pub(super) async fn get_missed(
30 State(state): State<RuntimeApiState>,
31 Path(id): Path<String>,
32 ) -> Result<Json<AutomationMissedStatus>, ApiError> {
33 let manager = state.automations.lock().await;
34 let status = manager.missed_status(&id).map_err(map_automation_err)?;
35 Ok(Json(status))
36 }
37
38 pub(super) async fn set_missed_policy(
39 State(state): State<RuntimeApiState>,
40 Path(id): Path<String>,
41 headers: HeaderMap,
42 Json(req): Json<MissedPolicyRequest>,
43 ) -> Result<Response, ApiError> {
44 automation_mutation(
45 &state,
46 &headers,
47 &format!("PUT /v1/automations/{id}/missed/policy"),
48 &req,
49 StatusCode::OK,
50 map_automation_err,
51 async {
52 state
53 .automations
54 .lock()
55 .await
56 .set_missed_policy(&id, req.policy)
57 },
58 )
59 .await
60 }
61
62 pub(super) async fn run_missed(
63 State(state): State<RuntimeApiState>,
64 Path(id): Path<String>,
65 headers: HeaderMap,
66 ) -> Result<Response, ApiError> {
67 answer(state, id, headers, MissedRunDecision::RunNow, "run").await
68 }
69
70 pub(super) async fn skip_missed(
71 State(state): State<RuntimeApiState>,
72 Path(id): Path<String>,
73 headers: HeaderMap,
74 ) -> Result<Response, ApiError> {
75 answer(state, id, headers, MissedRunDecision::Skip, "skip").await
76 }
77
78 async fn answer(
79 state: RuntimeApiState,
80 id: String,
81 headers: HeaderMap,
82 decision: MissedRunDecision,
83 action: &str,
84 ) -> Result<Response, ApiError> {
85 automation_mutation(
86 &state,
87 &headers,
88 &format!("POST /v1/automations/{id}/missed/{action}"),
89 &decision,
90 StatusCode::OK,
91 map_automation_err,
92 resolve_missed_run_shared(&state.automations, &id, decision, &state.task_manager),
93 )
94 .await
95 }
96
96 lines RUST