zart-api 0.2.0

Optional Axum HTTP server for Zart — exposes execution status, cancellation, and event delivery endpoints
Documentation
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Route definitions and handler implementations for the Zart HTTP API.

use axum::{
    Json, Router,
    extract::{Path, Query, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::{get, post},
};
use std::time::Duration;
use zart::error::SchedulerError;
#[cfg(feature = "metrics")]
use zart::metrics::gather_metrics;

use crate::{
    models::{
        ErrorResponse, ExecutionResponse, ListQuery, StartExecutionRequest, StatsResponse,
        WaitQuery,
    },
    state::AppState,
};

/// Maximum wait allowed by the `wait` endpoint (seconds).
const MAX_WAIT_SECS: u64 = 30;

/// Construct the versioned API router with the given application state.
///
/// Routes are nested under `prefix` (e.g., `"/api/v1"`). Health checks and
/// metrics are mounted at the root, outside the prefix.
pub fn api_router(state: AppState, prefix: &str) -> Router {
    let inner = Router::new()
        // Execution management
        .route("/executions", get(list_executions))
        .route("/executions", post(start_execution))
        .route("/executions/{execution_id}", get(get_execution))
        .route("/executions/{execution_id}/cancel", post(cancel_execution))
        .route("/executions/{execution_id}/wait", get(wait_execution))
        // Stats
        .route("/stats", get(get_stats))
        // Event delivery
        .route("/events/{execution_id}/{event_name}", post(offer_event));

    #[allow(unused_mut)]
    let mut app = Router::new()
        .nest(prefix, inner)
        // Health checks are root-level, unaffected by prefix
        .route("/healthz", get(healthz))
        .route("/readyz", get(readyz));

    #[cfg(feature = "metrics")]
    {
        app = app.route("/metrics", get(metrics_handler));
    }

    app.with_state(state)
}

// ── Handlers ──────────────────────────────────────────────────────────────────

/// `GET /healthz` — liveness probe.
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/healthz",
    responses(
        (status = 200, description = "Service is alive"),
    ),
    tag = "health"
))]
async fn healthz() -> impl IntoResponse {
    (StatusCode::OK, "ok")
}

/// `GET /readyz` — readiness probe (checks if the service is ready to accept requests).
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/readyz",
    responses(
        (status = 200, description = "Service is ready"),
        (status = 503, description = "Service not ready"),
    ),
    tag = "health"
))]
async fn readyz(State(state): State<AppState>) -> impl IntoResponse {
    // Check if the durable API is available
    if state.durable.is_ready() {
        (StatusCode::OK, "ok")
    } else {
        (StatusCode::SERVICE_UNAVAILABLE, "not ready")
    }
}

/// `GET /metrics` — Prometheus metrics endpoint.
#[cfg(feature = "metrics")]
async fn metrics_handler() -> impl IntoResponse {
    (
        StatusCode::OK,
        [("Content-Type", "text/plain; version=0.0.4; charset=utf-8")],
        gather_metrics(),
    )
}

/// `GET /executions` — list executions with optional filters.
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/executions",
    params(ListQuery),
    responses(
        (status = 200, description = "List of executions", body = Vec<ExecutionResponse>),
        (status = 500, description = "Internal error",     body = ErrorResponse),
    ),
    tag = "executions"
))]
async fn list_executions(State(state): State<AppState>, Query(q): Query<ListQuery>) -> Response {
    let params = q.into_params();

    match state.durable.list_executions(params).await {
        Ok(records) => {
            let body: Vec<ExecutionResponse> = records.into_iter().map(Into::into).collect();
            (StatusCode::OK, Json(body)).into_response()
        }
        Err(e) => scheduler_error_response(e),
    }
}

/// `POST /executions` — start a new durable execution.
///
/// Idempotent: if `executionId` already exists, returns the existing record with 200.
#[cfg_attr(feature = "openapi", utoipa::path(
    post,
    path = "/executions",
    request_body = StartExecutionRequest,
    responses(
        (status = 201, description = "Execution started",                          body = ExecutionResponse),
        (status = 200, description = "Idempotent replay — execution already exists", body = ExecutionResponse),
        (status = 500, description = "Internal error",                             body = ErrorResponse),
    ),
    tag = "executions"
))]
async fn start_execution(
    State(state): State<AppState>,
    Json(req): Json<StartExecutionRequest>,
) -> Response {
    let execution_id = req
        .execution_id
        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

    match state
        .durable
        .start(&execution_id, &req.task_name, req.payload)
        .await
    {
        Ok(_) => match state.durable.status(&execution_id).await {
            Ok(record) => {
                let body: ExecutionResponse = record.into();
                (StatusCode::CREATED, Json(body)).into_response()
            }
            Err(e) => scheduler_error_response(e),
        },
        Err(e) => scheduler_error_response(e),
    }
}

/// `GET /executions/:execution_id` — get execution status and step progress.
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/executions/{execution_id}",
    params(
        ("execution_id" = String, Path, description = "Execution identifier"),
    ),
    responses(
        (status = 200, description = "Execution found",   body = ExecutionResponse),
        (status = 404, description = "Not found",         body = ErrorResponse),
        (status = 500, description = "Internal error",    body = ErrorResponse),
    ),
    tag = "executions"
))]
async fn get_execution(
    State(state): State<AppState>,
    Path(execution_id): Path<String>,
) -> Response {
    match state.durable.status(&execution_id).await {
        Ok(record) => {
            let body: ExecutionResponse = record.into();
            (StatusCode::OK, Json(body)).into_response()
        }
        Err(SchedulerError::ExecutionNotFound(_)) => not_found(&execution_id),
        Err(e) => scheduler_error_response(e),
    }
}

/// `POST /executions/:execution_id/cancel` — cancel a running execution.
#[cfg_attr(feature = "openapi", utoipa::path(
    post,
    path = "/executions/{execution_id}/cancel",
    params(
        ("execution_id" = String, Path, description = "Execution identifier"),
    ),
    responses(
        (status = 204, description = "Cancelled"),
        (status = 404, description = "Not found",      body = ErrorResponse),
        (status = 500, description = "Internal error", body = ErrorResponse),
    ),
    tag = "executions"
))]
async fn cancel_execution(
    State(state): State<AppState>,
    Path(execution_id): Path<String>,
) -> Response {
    match state.durable.cancel(&execution_id).await {
        Ok(true) => StatusCode::NO_CONTENT.into_response(),
        Ok(false) => not_found(&execution_id),
        Err(e) => scheduler_error_response(e),
    }
}

/// `GET /executions/:execution_id/wait` — long-poll until completion.
///
/// Accepts an optional `timeout_secs` query parameter (max 30, default 30).
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/executions/{execution_id}/wait",
    params(
        ("execution_id" = String, Path, description = "Execution identifier"),
        WaitQuery,
    ),
    responses(
        (status = 200, description = "Execution completed",  body = ExecutionResponse),
        (status = 404, description = "Not found",            body = ErrorResponse),
        (status = 504, description = "Wait timed out",       body = ErrorResponse),
        (status = 500, description = "Internal error",       body = ErrorResponse),
    ),
    tag = "executions"
))]
async fn wait_execution(
    State(state): State<AppState>,
    Path(execution_id): Path<String>,
    Query(q): Query<WaitQuery>,
) -> Response {
    let secs = q.timeout_secs.unwrap_or(MAX_WAIT_SECS).min(MAX_WAIT_SECS);
    let timeout = Duration::from_secs(secs);

    match state.durable.wait(&execution_id, timeout, None).await {
        Ok(record) => {
            let body: ExecutionResponse = record.into();
            (StatusCode::OK, Json(body)).into_response()
        }
        Err(SchedulerError::ExecutionNotFound(_)) => not_found(&execution_id),
        Err(SchedulerError::WaitTimedOut(_)) => (
            StatusCode::GATEWAY_TIMEOUT,
            Json(ErrorResponse {
                error: format!("execution '{execution_id}' did not complete within {secs}s"),
            }),
        )
            .into_response(),
        Err(e) => scheduler_error_response(e),
    }
}

/// `POST /events/:execution_id/:event_name` — deliver an event.
#[cfg_attr(feature = "openapi", utoipa::path(
    post,
    path = "/events/{execution_id}/{event_name}",
    params(
        ("execution_id" = String, Path, description = "Execution identifier"),
        ("event_name"   = String, Path, description = "Event name"),
    ),
    request_body = serde_json::Value,
    responses(
        (status = 202, description = "Event accepted"),
        (status = 404, description = "Not found",      body = ErrorResponse),
        (status = 500, description = "Internal error", body = ErrorResponse),
    ),
    tag = "events"
))]
async fn offer_event(
    State(state): State<AppState>,
    Path((execution_id, event_name)): Path<(String, String)>,
    Json(payload): Json<serde_json::Value>,
) -> Response {
    match state
        .durable
        .offer_event(&execution_id, &event_name, payload)
        .await
    {
        Ok(()) => StatusCode::ACCEPTED.into_response(),
        Err(SchedulerError::ExecutionNotFound(_)) => not_found(&execution_id),
        Err(e) => scheduler_error_response(e),
    }
}

// ── Stats ──────────────────────────────────────────────────────────────────

/// `GET /stats` — aggregate execution counts by status.
#[cfg_attr(feature = "openapi", utoipa::path(
    get,
    path = "/stats",
    responses(
        (status = 200, description = "Execution statistics", body = StatsResponse),
        (status = 500, description = "Internal error",       body = ErrorResponse),
    ),
    tag = "stats"
))]
async fn get_stats(State(state): State<AppState>) -> Response {
    match state.durable.stats().await {
        Ok(stats) => {
            let body: StatsResponse = stats.into();
            (StatusCode::OK, Json(body)).into_response()
        }
        Err(e) => scheduler_error_response(e),
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn not_found(execution_id: &str) -> Response {
    (
        StatusCode::NOT_FOUND,
        Json(ErrorResponse {
            error: format!("execution '{execution_id}' not found"),
        }),
    )
        .into_response()
}

fn scheduler_error_response(e: SchedulerError) -> Response {
    (
        StatusCode::INTERNAL_SERVER_ERROR,
        Json(ErrorResponse {
            error: e.to_string(),
        }),
    )
        .into_response()
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use std::sync::Arc;
    use tower::ServiceExt;
    use zart::{DurableApi, error::SchedulerError};
    use zart::{ExecutionRecord, ExecutionStats, ListExecutionsParams};
    use zart_scheduler::ScheduleResult;

    struct NullApi;

    #[async_trait]
    impl DurableApi for NullApi {
        async fn start(
            &self,
            _: &str,
            _: &str,
            _: serde_json::Value,
        ) -> Result<ScheduleResult, SchedulerError> {
            unimplemented!()
        }
        async fn cancel(&self, _: &str) -> Result<bool, SchedulerError> {
            unimplemented!()
        }
        async fn status(&self, _: &str) -> Result<ExecutionRecord, SchedulerError> {
            unimplemented!()
        }
        async fn wait(
            &self,
            _: &str,
            _: Duration,
            _: Option<Duration>,
        ) -> Result<ExecutionRecord, SchedulerError> {
            unimplemented!()
        }
        async fn offer_event(
            &self,
            _: &str,
            _: &str,
            _: serde_json::Value,
        ) -> Result<(), SchedulerError> {
            unimplemented!()
        }
        async fn list_executions(
            &self,
            _: ListExecutionsParams,
        ) -> Result<Vec<ExecutionRecord>, SchedulerError> {
            unimplemented!()
        }
        async fn stats(&self) -> Result<ExecutionStats, SchedulerError> {
            Ok(ExecutionStats::default())
        }
    }

    fn test_app() -> axum::Router {
        let state = AppState::new(Arc::new(NullApi));
        api_router(state, "/api/v1").layer(tower_http::trace::TraceLayer::new_for_http())
    }

    #[tokio::test]
    async fn healthz_returns_200() {
        let app = test_app();
        let response = app
            .oneshot(
                Request::builder()
                    .uri("/healthz")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn readyz_returns_200() {
        let app = test_app();
        let response = app
            .oneshot(
                Request::builder()
                    .uri("/readyz")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[cfg(feature = "metrics")]
    #[tokio::test]
    async fn metrics_returns_200() {
        let app = test_app();
        let response = app
            .oneshot(
                Request::builder()
                    .uri("/metrics")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }
}