acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Job management admin handlers
//!
//! This module provides HTTP handlers for managing background jobs.
//! These handlers should be protected with admin-only authorization.
//!
//! # Architecture
//!
//! Uses acton-reactive web handler pattern with oneshot channels for
//! request-reply communication between HTTP handlers and the `JobAgent`.
//!
//! See `.claude/acton-reactive-research-20251124-message-patterns.md` for
//! detailed documentation on the message passing patterns.
//!
//! # Example Usage
//!
//! ```rust,ignore
//! use acton_htmx::handlers::job_admin;
//! use axum::Router;
//!
//! let admin_routes = Router::new()
//!     .route("/admin/jobs/list", get(job_admin::list_jobs))
//!     .route("/admin/jobs/stats", get(job_admin::job_stats));
//! ```

use acton_reactive::prelude::AgentHandleInterface;
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

use crate::auth::{user::User, Authenticated};
use crate::jobs::{
    agent::{
        CancelJobRequest, ClearDeadLetterQueueRequest, GetMetricsRequest, RetryAllFailedRequest,
        RetryJobRequest,
    },
    JobId,
};
use crate::state::ActonHtmxState;

/// Response for job list endpoint
#[derive(Debug, Serialize, Deserialize)]
pub struct JobListResponse {
    /// List of jobs
    pub jobs: Vec<JobInfo>,
    /// Total number of jobs matching filters
    pub total: usize,
    /// Success message
    pub message: String,
}

/// Information about a single job
#[derive(Debug, Serialize, Deserialize)]
pub struct JobInfo {
    /// Job ID
    pub id: String,
    /// Job type
    pub job_type: String,
    /// Current status
    pub status: String,
    /// When the job was created
    pub created_at: String,
    /// Job priority
    pub priority: i32,
}

/// Response for job statistics endpoint
#[derive(Debug, Serialize, Deserialize)]
pub struct JobStatsResponse {
    /// Total jobs enqueued
    pub total_enqueued: u64,
    /// Currently running jobs
    pub running: usize,
    /// Pending jobs in queue
    pub pending: usize,
    /// Completed jobs
    pub completed: u64,
    /// Failed jobs
    pub failed: u64,
    /// Jobs in dead letter queue
    pub dead_letter: u64,
    /// Average execution time in milliseconds
    pub avg_execution_ms: f64,
    /// P95 execution time in milliseconds
    pub p95_execution_ms: f64,
    /// P99 execution time in milliseconds
    pub p99_execution_ms: f64,
    /// Success rate as percentage
    pub success_rate: f64,
    /// Message
    pub message: String,
}

/// List all jobs
///
/// Returns a list of jobs from the queue and their current status.
/// Requires admin role.
///
/// # Errors
///
/// Returns [`StatusCode::FORBIDDEN`] if the authenticated user does not have the "admin" role.
///
/// # Example
///
/// ```bash
/// GET /admin/jobs/list
/// ```
///
/// Response:
/// ```json
/// {
///   "jobs": [],
///   "total": 0,
///   "message": "Jobs retrieved successfully"
/// }
/// ```
pub async fn list_jobs(
    State(_state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            "Non-admin attempted to list jobs"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // For now, we return empty list as we don't have a message to list all jobs
    // This would require adding a new message type to the JobAgent
    // In Phase 3, we can add ListJobs message to get actual job data

    let response = JobListResponse {
        jobs: vec![],
        total: 0,
        message: "Job listing functionality will be enhanced in Phase 3".to_string(),
    };

    tracing::info!(
        admin_id = admin.id,
        "Admin retrieved job list"
    );

    Ok((StatusCode::OK, Json(response)).into_response())
}

/// Get job statistics
///
/// Returns comprehensive statistics about the job queue and execution metrics.
/// Requires admin role.
///
/// Uses acton-reactive web handler pattern with oneshot channel for
/// communication with `JobAgent`. Includes 100ms timeout to prevent handler
/// from hanging if the agent is slow or stopped.
///
/// # Example
///
/// ```bash
/// GET /admin/jobs/stats
/// ```
///
/// Response:
/// ```json
/// {
///   "total_enqueued": 150,
///   "running": 2,
///   "pending": 5,
///   "completed": 140,
///   "failed": 3,
///   "dead_letter": 0,
///   "avg_execution_ms": 125.5,
///   "p95_execution_ms": 450.0,
///   "p99_execution_ms": 890.0,
///   "success_rate": 97.9,
///   "message": "Statistics retrieved successfully"
/// }
/// ```
///
/// # Errors
///
/// Returns:
/// - `403 FORBIDDEN` if user is not an admin
/// - `408 REQUEST_TIMEOUT` if agent doesn't respond within 100ms
/// - `500 INTERNAL_SERVER_ERROR` if agent response channel fails
#[allow(clippy::cast_precision_loss)] // Acceptable for metrics
pub async fn job_stats(
    State(state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            "Non-admin attempted to view job statistics"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // Create request with response channel (web handler pattern)
    let (request, rx) = GetMetricsRequest::new();

    // Send message to JobAgent (fire-and-forget from handler perspective)
    state.job_agent().send(request).await;

    // Await response with 100ms timeout
    let timeout = Duration::from_millis(100);
    let metrics = tokio::time::timeout(timeout, rx)
        .await
        .map_err(|_| {
            tracing::error!("Job metrics retrieval timeout");
            StatusCode::REQUEST_TIMEOUT
        })?
        .map_err(|_| {
            tracing::error!("Job metrics channel error");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    // Calculate success rate from metrics
    let total_processed = metrics.jobs_completed + metrics.jobs_failed;
    let success_rate = if total_processed > 0 {
        (metrics.jobs_completed as f64 / total_processed as f64) * 100.0
    } else {
        100.0
    };

    // Build response from real metrics
    let response = JobStatsResponse {
        total_enqueued: metrics.jobs_enqueued,
        running: metrics.current_running,
        pending: metrics.current_queue_size,
        completed: metrics.jobs_completed,
        failed: metrics.jobs_failed,
        dead_letter: metrics.jobs_in_dlq,
        avg_execution_ms: metrics.avg_execution_time_ms as f64,
        p95_execution_ms: metrics.p95_execution_time_ms as f64,
        p99_execution_ms: metrics.p99_execution_time_ms as f64,
        success_rate,
        message: "Statistics retrieved successfully".to_string(),
    };

    tracing::info!(
        admin_id = admin.id,
        jobs_enqueued = metrics.jobs_enqueued,
        jobs_completed = metrics.jobs_completed,
        jobs_failed = metrics.jobs_failed,
        "Admin retrieved job statistics"
    );

    Ok((StatusCode::OK, Json(response)).into_response())
}

/// Retry a failed job by ID
///
/// Re-queues a job from the dead letter queue back into the main queue
/// for another execution attempt. Requires admin role.
///
/// # Example
///
/// ```bash
/// POST /admin/jobs/{job_id}/retry
/// ```
///
/// Response:
/// ```json
/// {
///   "success": true,
///   "message": "Job queued for retry"
/// }
/// ```
///
/// # Errors
///
/// Returns:
/// - `403 FORBIDDEN` if user is not an admin
/// - `404 NOT_FOUND` if job is not in dead letter queue
/// - `408 REQUEST_TIMEOUT` if agent doesn't respond within 100ms
/// - `500 INTERNAL_SERVER_ERROR` if agent response channel fails
pub async fn retry_job(
    State(state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
    Path(job_id): Path<JobId>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            %job_id,
            "Non-admin attempted to retry job"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // Create request with response channel
    let (request, rx) = RetryJobRequest::new(job_id);

    // Send message to JobAgent
    state.job_agent().send(request).await;

    // Await response with 100ms timeout
    let timeout = Duration::from_millis(100);
    let success = tokio::time::timeout(timeout, rx)
        .await
        .map_err(|_| {
            tracing::error!(%job_id, "Job retry timeout");
            StatusCode::REQUEST_TIMEOUT
        })?
        .map_err(|_| {
            tracing::error!(%job_id, "Job retry channel error");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    if success {
        tracing::info!(
            admin_id = admin.id,
            %job_id,
            "Job queued for retry"
        );

        Ok((
            StatusCode::OK,
            Json(serde_json::json!({
                "success": true,
                "message": "Job queued for retry"
            })),
        )
            .into_response())
    } else {
        tracing::warn!(
            admin_id = admin.id,
            %job_id,
            "Job not found in dead letter queue"
        );
        Err(StatusCode::NOT_FOUND)
    }
}

/// Retry all failed jobs
///
/// Re-queues all jobs from the dead letter queue back into the main queue.
/// Requires admin role.
///
/// # Example
///
/// ```bash
/// POST /admin/jobs/retry-all
/// ```
///
/// Response:
/// ```json
/// {
///   "retried": 5,
///   "message": "5 jobs queued for retry"
/// }
/// ```
///
/// # Errors
///
/// Returns:
/// - `403 FORBIDDEN` if user is not an admin
/// - `408 REQUEST_TIMEOUT` if agent doesn't respond within 500ms
/// - `500 INTERNAL_SERVER_ERROR` if agent response channel fails
pub async fn retry_all_jobs(
    State(state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            "Non-admin attempted to retry all jobs"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // Create request with response channel
    let (request, rx) = RetryAllFailedRequest::new();

    // Send message to JobAgent
    state.job_agent().send(request).await;

    // Await response with 500ms timeout (may need to requeue many jobs)
    let timeout = Duration::from_millis(500);
    let retried = tokio::time::timeout(timeout, rx)
        .await
        .map_err(|_| {
            tracing::error!("Retry all jobs timeout");
            StatusCode::REQUEST_TIMEOUT
        })?
        .map_err(|_| {
            tracing::error!("Retry all jobs channel error");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    tracing::info!(
        admin_id = admin.id,
        retried,
        "All failed jobs queued for retry"
    );

    Ok((
        StatusCode::OK,
        Json(serde_json::json!({
            "retried": retried,
            "message": format!("{retried} jobs queued for retry")
        })),
    )
        .into_response())
}

/// Cancel a running or pending job
///
/// Attempts to cancel a job. If the job is pending, it's removed from the queue.
/// If it's currently running, a cancellation signal is sent.
/// Requires admin role.
///
/// # Example
///
/// ```bash
/// POST /admin/jobs/{job_id}/cancel
/// ```
///
/// Response:
/// ```json
/// {
///   "success": true,
///   "message": "Job cancellation requested"
/// }
/// ```
///
/// # Errors
///
/// Returns:
/// - `403 FORBIDDEN` if user is not an admin
/// - `404 NOT_FOUND` if job is not found
/// - `408 REQUEST_TIMEOUT` if agent doesn't respond within 100ms
/// - `500 INTERNAL_SERVER_ERROR` if agent response channel fails
pub async fn cancel_job(
    State(state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
    Path(job_id): Path<JobId>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            %job_id,
            "Non-admin attempted to cancel job"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // Create request with response channel
    let (request, rx) = CancelJobRequest::new(job_id);

    // Send message to JobAgent
    state.job_agent().send(request).await;

    // Await response with 100ms timeout
    let timeout = Duration::from_millis(100);
    let success = tokio::time::timeout(timeout, rx)
        .await
        .map_err(|_| {
            tracing::error!(%job_id, "Job cancel timeout");
            StatusCode::REQUEST_TIMEOUT
        })?
        .map_err(|_| {
            tracing::error!(%job_id, "Job cancel channel error");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    if success {
        tracing::info!(
            admin_id = admin.id,
            %job_id,
            "Job cancellation requested"
        );

        Ok((
            StatusCode::OK,
            Json(serde_json::json!({
                "success": true,
                "message": "Job cancellation requested"
            })),
        )
            .into_response())
    } else {
        tracing::warn!(
            admin_id = admin.id,
            %job_id,
            "Job not found"
        );
        Err(StatusCode::NOT_FOUND)
    }
}

/// Clear the dead letter queue
///
/// Permanently removes all jobs from the dead letter queue.
/// This operation cannot be undone. Requires admin role.
///
/// # Example
///
/// ```bash
/// POST /admin/jobs/dead-letter/clear
/// ```
///
/// Response:
/// ```json
/// {
///   "cleared": 3,
///   "message": "3 jobs removed from dead letter queue"
/// }
/// ```
///
/// # Errors
///
/// Returns:
/// - `403 FORBIDDEN` if user is not an admin
/// - `408 REQUEST_TIMEOUT` if agent doesn't respond within 100ms
/// - `500 INTERNAL_SERVER_ERROR` if agent response channel fails
pub async fn clear_dead_letter_queue(
    State(state): State<ActonHtmxState>,
    Authenticated(admin): Authenticated<User>,
) -> Result<Response, StatusCode> {
    // Verify admin role
    if !admin.roles.contains(&"admin".to_string()) {
        tracing::warn!(
            admin_id = admin.id,
            "Non-admin attempted to clear dead letter queue"
        );
        return Err(StatusCode::FORBIDDEN);
    }

    // Create request with response channel
    let (request, rx) = ClearDeadLetterQueueRequest::new();

    // Send message to JobAgent
    state.job_agent().send(request).await;

    // Await response with 100ms timeout
    let timeout = Duration::from_millis(100);
    let cleared = tokio::time::timeout(timeout, rx)
        .await
        .map_err(|_| {
            tracing::error!("Clear dead letter queue timeout");
            StatusCode::REQUEST_TIMEOUT
        })?
        .map_err(|_| {
            tracing::error!("Clear dead letter queue channel error");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    tracing::info!(
        admin_id = admin.id,
        cleared,
        "Dead letter queue cleared"
    );

    Ok((
        StatusCode::OK,
        Json(serde_json::json!({
            "cleared": cleared,
            "message": format!("{cleared} jobs removed from dead letter queue")
        })),
    )
        .into_response())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_job_info_serialization() {
        let job = JobInfo {
            id: "job-123".to_string(),
            job_type: "WelcomeEmail".to_string(),
            status: "pending".to_string(),
            created_at: "2025-11-22T10:00:00Z".to_string(),
            priority: 10,
        };

        let json = serde_json::to_string(&job).unwrap();
        assert!(json.contains("job-123"));
        assert!(json.contains("WelcomeEmail"));
    }

    #[test]
    fn test_job_stats_response_serialization() {
        let stats = JobStatsResponse {
            total_enqueued: 100,
            running: 2,
            pending: 5,
            completed: 90,
            failed: 3,
            dead_letter: 0,
            avg_execution_ms: 125.5,
            p95_execution_ms: 450.0,
            p99_execution_ms: 890.0,
            success_rate: 96.8,
            message: "Success".to_string(),
        };

        let json = serde_json::to_string(&stats).unwrap();
        assert!(json.contains("\"total_enqueued\":100"));
        assert!(json.contains("\"running\":2"));
        assert!(json.contains("\"success_rate\":96.8"));
    }
}