Skip to main content

runledger_postgres/jobs/types/
admin.rs

1use chrono::{DateTime, Utc};
2use runledger_core::jobs::{JobStatus, JobTypeName};
3use serde_json::Value;
4use sqlx::types::Uuid;
5
6/// Maximum page size accepted by public job and workflow list APIs.
7///
8/// This bounds accidental unbounded reads from admin/TUI surfaces while still
9/// allowing operators to inspect a large page when needed.
10pub const JOB_LIST_PAGE_LIMIT_MAX: i64 = 1_000;
11
12/// Authorization scope for canceling a job.
13///
14/// Unlike legacy cancellation APIs that use `None` as an admin wildcard, this
15/// type distinguishes an exact global job from an explicit admin operation.
16#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17pub enum JobCancellationScope {
18    /// Match only a job whose `organization_id` is `NULL`.
19    Global,
20    /// Match only a job owned by this exact organization.
21    Organization(Uuid),
22    /// Match the job regardless of its organization.
23    Admin,
24}
25
26#[derive(Clone, Debug)]
27pub struct JobMetricsRecord {
28    pub job_type: JobTypeName,
29    pub pending_count: i64,
30    pub leased_count: i64,
31    pub stale_leases: i64,
32    pub succeeded_24h: i64,
33    pub retryable_24h: i64,
34    pub terminal_24h: i64,
35    pub panicked_24h: i64,
36    pub timeout_24h: i64,
37    pub dead_lettered_24h: i64,
38    pub p50_duration_ms_24h: Option<f64>,
39    pub p95_duration_ms_24h: Option<f64>,
40}
41
42/// Continuation-specific operational signals for one job type.
43///
44/// Kept separate from [`JobMetricsRecord`] so adding continuation visibility
45/// does not break downstream code that constructs the established metrics DTO.
46#[derive(Clone, Debug)]
47#[non_exhaustive]
48pub struct JobContinuationMetricsRecord {
49    pub job_type: JobTypeName,
50    /// Successful handler continuations recorded during the last 24 hours.
51    pub continued_24h: i64,
52    /// Pending or leased jobs whose current run was created by continuation.
53    pub active_continued_count: i64,
54    /// Highest current run number among those continuation-created runs.
55    pub max_active_run_number: i32,
56}
57
58#[derive(Clone, Debug)]
59pub struct JobLogRecord {
60    pub id: i64,
61    pub job_id: Uuid,
62    pub run_number: i32,
63    pub attempt: Option<i32>,
64    pub level: String,
65    pub message: String,
66    pub payload: Value,
67    pub occurred_at: DateTime<Utc>,
68}
69
70#[derive(Clone, Debug)]
71pub struct JobLogRecordInput {
72    pub job_id: Uuid,
73    pub run_number: i32,
74    pub attempt: Option<i32>,
75    pub level: String,
76    pub message: String,
77    pub payload: Value,
78}
79
80#[derive(Clone, Debug)]
81pub struct JobListFilter<'a> {
82    pub organization_id: Option<Uuid>,
83    pub status: Option<JobStatus>,
84    /// Admin list query input used for `ILIKE` substring matching, not a canonical persisted
85    /// identifier boundary.
86    pub job_type: Option<&'a str>,
87    pub limit: i64,
88    pub offset: i64,
89}