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
//! Messages for the job agent.

use crate::jobs::{JobId, JobStatus};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{oneshot, Mutex};

/// Response channel type for web handler pattern.
///
/// Wraps `oneshot::Sender` in `Arc<Mutex<Option<T>>>` to satisfy
/// `Clone + Debug` requirements for acton-reactive messages.
pub type ResponseChannel<T> = Arc<Mutex<Option<oneshot::Sender<T>>>>;

/// Enqueue a new job for processing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnqueueJob {
    /// Unique job identifier.
    pub id: JobId,
    /// Job type name.
    pub job_type: String,
    /// Serialized job payload.
    pub payload: Vec<u8>,
    /// Job priority (higher = more important).
    pub priority: i32,
    /// Maximum number of retry attempts.
    pub max_retries: u32,
    /// Job execution timeout.
    pub timeout: Duration,
}

/// Response to job enqueue request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobEnqueued {
    /// The enqueued job ID.
    pub id: JobId,
}

/// Get the status of a job (agent-to-agent pattern).
///
/// **Deprecated**: Use [`GetJobStatusRequest`] for web handlers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetJobStatus {
    /// Job ID to query.
    pub id: JobId,
}

/// Response containing job status (agent-to-agent pattern).
///
/// **Deprecated**: Use [`GetJobStatusRequest`] for web handlers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobStatusResponse {
    /// Job ID.
    pub id: JobId,
    /// Current status (None if job not found).
    pub status: Option<JobStatus>,
}

/// Request job metrics (agent-to-agent pattern).
///
/// **Deprecated**: Use [`GetMetricsRequest`] for web handlers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetMetrics;

/// Job processing metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct JobMetrics {
    /// Total jobs enqueued.
    pub jobs_enqueued: u64,
    /// Total jobs dequeued.
    pub jobs_dequeued: u64,
    /// Total jobs completed successfully.
    pub jobs_completed: u64,
    /// Total jobs failed.
    pub jobs_failed: u64,
    /// Total jobs rejected (queue full).
    pub jobs_rejected: u64,
    /// Total jobs in dead letter queue.
    pub jobs_in_dlq: u64,
    /// Current queue size.
    pub current_queue_size: usize,
    /// Current number of running jobs.
    pub current_running: usize,
    /// Total execution time in milliseconds.
    pub total_execution_time_ms: u64,
    /// Average execution time in milliseconds.
    pub avg_execution_time_ms: u64,
    /// Minimum execution time in milliseconds.
    pub min_execution_time_ms: u64,
    /// Maximum execution time in milliseconds.
    pub max_execution_time_ms: u64,
    /// P50 (median) execution time in milliseconds.
    pub p50_execution_time_ms: u64,
    /// P95 execution time in milliseconds.
    pub p95_execution_time_ms: u64,
    /// P99 execution time in milliseconds.
    pub p99_execution_time_ms: u64,
}

impl JobMetrics {
    /// Update metrics with a completed job execution time.
    ///
    /// This updates percentile calculations using a simple streaming algorithm.
    /// For production use, consider using a histogram library like `hdrhistogram`.
    pub const fn record_execution_time(&mut self, execution_time_ms: u64) {
        self.total_execution_time_ms = self.total_execution_time_ms.saturating_add(execution_time_ms);

        // Update min/max
        if self.min_execution_time_ms == 0 || execution_time_ms < self.min_execution_time_ms {
            self.min_execution_time_ms = execution_time_ms;
        }
        if execution_time_ms > self.max_execution_time_ms {
            self.max_execution_time_ms = execution_time_ms;
        }

        // Update average
        if self.jobs_completed > 0 {
            self.avg_execution_time_ms = self.total_execution_time_ms / self.jobs_completed;
        }

        // Simple percentile estimation (will be replaced with histogram in production)
        // For now, use max as p99, avg as p50, and interpolate p95
        self.p50_execution_time_ms = self.avg_execution_time_ms;
        self.p95_execution_time_ms = self.avg_execution_time_ms +
            ((self.max_execution_time_ms.saturating_sub(self.avg_execution_time_ms)) * 75 / 100);
        self.p99_execution_time_ms = self.max_execution_time_ms;
    }

    /// Calculate failure rate as percentage (0-100).
    #[must_use]
    #[allow(clippy::cast_precision_loss)] // Acceptable for metrics
    pub fn failure_rate(&self) -> f64 {
        let total = self.jobs_completed + self.jobs_failed;
        if total == 0 {
            0.0
        } else {
            (self.jobs_failed as f64 / total as f64) * 100.0
        }
    }
}

/// Internal message to trigger job processing.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)] // Will be used in Week 5 for job processing loop
pub(super) struct ProcessJobs;

/// Internal message to cleanup expired jobs.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)] // Will be used in Week 5 for cleanup scheduling
pub(super) struct CleanupExpiredJobs;

// ============================================================================
// Web Handler Pattern Messages (HTTP handler to agent communication)
// ============================================================================

/// Request job metrics (web handler pattern).
///
/// Used by HTTP handlers to query job statistics. Uses oneshot channel
/// for response to avoid blocking the handler.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::GetMetricsRequest;
/// use std::time::Duration;
///
/// async fn handler(State(state): State<ActonHtmxState>) -> Result<Response> {
///     let (request, rx) = GetMetricsRequest::new();
///     state.job_agent().send(request).await;
///
///     let timeout = Duration::from_millis(100);
///     let metrics = tokio::time::timeout(timeout, rx).await??;
///
///     Ok(Json(metrics).into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetMetricsRequest {
    /// Response channel for metrics.
    pub response_tx: ResponseChannel<JobMetrics>,
}

impl GetMetricsRequest {
    /// Create a new metrics request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new() -> (Self, oneshot::Receiver<JobMetrics>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Request job status (web handler pattern).
///
/// Used by HTTP handlers to query the status of a specific job.
/// Uses oneshot channel for response to avoid blocking the handler.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::GetJobStatusRequest;
/// use std::time::Duration;
///
/// async fn handler(
///     State(state): State<ActonHtmxState>,
///     Path(job_id): Path<JobId>,
/// ) -> Result<Response> {
///     let (request, rx) = GetJobStatusRequest::new(job_id);
///     state.job_agent().send(request).await;
///
///     let timeout = Duration::from_millis(100);
///     let status = tokio::time::timeout(timeout, rx).await??;
///
///     Ok(Json(status).into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetJobStatusRequest {
    /// Job ID to query.
    pub id: JobId,
    /// Response channel for status.
    pub response_tx: ResponseChannel<Option<JobStatus>>,
}

impl GetJobStatusRequest {
    /// Create a new job status request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new(id: JobId) -> (Self, oneshot::Receiver<Option<JobStatus>>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            id,
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Retry a failed job (web handler pattern).
///
/// Re-queues a job from the dead letter queue back into the main queue
/// for another execution attempt.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::RetryJobRequest;
///
/// async fn handler(
///     State(state): State<ActonHtmxState>,
///     Path(job_id): Path<JobId>,
/// ) -> Result<Response> {
///     let (request, rx) = RetryJobRequest::new(job_id);
///     state.job_agent().send(request).await;
///
///     let success = tokio::time::timeout(Duration::from_millis(100), rx).await??;
///     Ok(if success {
///         StatusCode::OK
///     } else {
///         StatusCode::NOT_FOUND
///     }.into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct RetryJobRequest {
    /// Job ID to retry.
    pub id: JobId,
    /// Response channel indicating success.
    pub response_tx: ResponseChannel<bool>,
}

impl RetryJobRequest {
    /// Create a new retry job request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new(id: JobId) -> (Self, oneshot::Receiver<bool>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            id,
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Retry all failed jobs (web handler pattern).
///
/// Re-queues all jobs from the dead letter queue back into the main queue.
/// Returns the number of jobs successfully retried.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::RetryAllFailedRequest;
///
/// async fn handler(State(state): State<ActonHtmxState>) -> Result<Response> {
///     let (request, rx) = RetryAllFailedRequest::new();
///     state.job_agent().send(request).await;
///
///     let count = tokio::time::timeout(Duration::from_millis(500), rx).await??;
///     Ok(Json(json!({ "retried": count })).into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct RetryAllFailedRequest {
    /// Response channel with count of retried jobs.
    pub response_tx: ResponseChannel<usize>,
}

impl RetryAllFailedRequest {
    /// Create a new retry all failed request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new() -> (Self, oneshot::Receiver<usize>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Cancel a running or pending job (web handler pattern).
///
/// 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.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::CancelJobRequest;
///
/// async fn handler(
///     State(state): State<ActonHtmxState>,
///     Path(job_id): Path<JobId>,
/// ) -> Result<Response> {
///     let (request, rx) = CancelJobRequest::new(job_id);
///     state.job_agent().send(request).await;
///
///     let success = tokio::time::timeout(Duration::from_millis(100), rx).await??;
///     Ok(if success {
///         StatusCode::OK
///     } else {
///         StatusCode::NOT_FOUND
///     }.into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct CancelJobRequest {
    /// Job ID to cancel.
    pub id: JobId,
    /// Response channel indicating success.
    pub response_tx: ResponseChannel<bool>,
}

impl CancelJobRequest {
    /// Create a new cancel job request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new(id: JobId) -> (Self, oneshot::Receiver<bool>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            id,
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Clear the dead letter queue (web handler pattern).
///
/// Permanently removes all jobs from the dead letter queue.
/// This operation cannot be undone.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::ClearDeadLetterQueueRequest;
///
/// async fn handler(State(state): State<ActonHtmxState>) -> Result<Response> {
///     let (request, rx) = ClearDeadLetterQueueRequest::new();
///     state.job_agent().send(request).await;
///
///     let count = tokio::time::timeout(Duration::from_millis(100), rx).await??;
///     Ok(Json(json!({ "cleared": count })).into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct ClearDeadLetterQueueRequest {
    /// Response channel with count of cleared jobs.
    pub response_tx: ResponseChannel<usize>,
}

impl ClearDeadLetterQueueRequest {
    /// Create a new clear dead letter queue request with response channel.
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new() -> (Self, oneshot::Receiver<usize>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}

/// Job history page response containing records and pagination info.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobHistoryPage {
    /// Job history records for this page.
    pub jobs: Vec<super::history::JobHistoryRecord>,
    /// Current page number (1-indexed).
    pub page: usize,
    /// Number of records per page.
    pub page_size: usize,
    /// Total number of matching records across all pages.
    pub total_count: usize,
    /// Whether there is a previous page.
    pub has_prev: bool,
    /// Whether there is a next page.
    pub has_next: bool,
}

impl JobHistoryPage {
    /// Create a new job history page from records and pagination info.
    #[must_use]
    pub const fn new(
        jobs: Vec<super::history::JobHistoryRecord>,
        page: usize,
        page_size: usize,
        total_count: usize,
    ) -> Self {
        let has_prev = page > 1;
        let total_pages = total_count.div_ceil(page_size);
        let has_next = page < total_pages;

        Self {
            jobs,
            page,
            page_size,
            total_count,
            has_prev,
            has_next,
        }
    }

    /// Get the previous page number.
    #[must_use]
    pub fn prev_page(&self) -> usize {
        self.page.saturating_sub(1).max(1)
    }

    /// Get the next page number.
    #[must_use]
    pub const fn next_page(&self) -> usize {
        self.page + 1
    }

    /// Get the starting record number for this page (1-indexed).
    #[must_use]
    pub fn page_start(&self) -> usize {
        if self.jobs.is_empty() {
            0
        } else {
            (self.page - 1) * self.page_size + 1
        }
    }

    /// Get the ending record number for this page (1-indexed).
    #[must_use]
    pub fn page_end(&self) -> usize {
        if self.jobs.is_empty() {
            0
        } else {
            self.page_start() + self.jobs.len() - 1
        }
    }
}

/// Request job history with pagination and search (web handler pattern).
///
/// Retrieves completed job history with optional search filtering
/// and pagination support.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::jobs::agent::messages::GetJobHistoryRequest;
///
/// async fn handler(
///     State(state): State<ActonHtmxState>,
///     Query(params): Query<HistoryParams>,
/// ) -> Result<Response> {
///     let (request, rx) = GetJobHistoryRequest::new(
///         params.page.unwrap_or(1),
///         params.page_size.unwrap_or(20),
///         params.search,
///     );
///     state.job_agent().send(request).await;
///
///     let timeout = Duration::from_millis(200);
///     let history = tokio::time::timeout(timeout, rx).await??;
///
///     Ok(Json(history).into_response())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetJobHistoryRequest {
    /// Page number (1-indexed).
    pub page: usize,
    /// Number of records per page.
    pub page_size: usize,
    /// Optional search query to filter results.
    pub search_query: Option<String>,
    /// Response channel for history page.
    pub response_tx: ResponseChannel<JobHistoryPage>,
}

impl GetJobHistoryRequest {
    /// Create a new job history request with response channel.
    ///
    /// # Arguments
    ///
    /// * `page` - Page number (1-indexed)
    /// * `page_size` - Number of records per page
    /// * `search_query` - Optional search string to filter records
    ///
    /// Returns a tuple of (request, receiver) where the request should be
    /// sent to the agent and the receiver awaited for the response.
    #[must_use]
    pub fn new(
        page: usize,
        page_size: usize,
        search_query: Option<String>,
    ) -> (Self, oneshot::Receiver<JobHistoryPage>) {
        let (tx, rx) = oneshot::channel();
        let request = Self {
            page: page.max(1), // Ensure page is at least 1
            page_size: page_size.clamp(1, 100), // Clamp between 1-100
            search_query,
            response_tx: Arc::new(Mutex::new(Some(tx))),
        };
        (request, rx)
    }
}