1use chrono::{DateTime, Utc};
2use runledger_core::jobs::{
3 JobEventType, JobFailureKind, JobStage, JobStatus, JobType, JobTypeName,
4};
5use serde_json::Value;
6use sqlx::types::Uuid;
7
8pub const JOB_SCHEDULE_MAX_JITTER_SECONDS: i32 = 86_400;
13
14pub const JOB_LIST_PAGE_LIMIT_MAX: i64 = 1_000;
19
20#[derive(Debug, Clone)]
21pub struct JobDefinitionUpsert<'a> {
22 pub job_type: JobType<'a>,
23 pub version: i32,
24 pub max_attempts: i32,
25 pub default_timeout_seconds: i32,
26 pub default_priority: i32,
27 pub is_enabled: bool,
28}
29
30#[derive(Debug, Clone)]
31pub struct JobDefinitionRecord {
32 pub job_type: JobTypeName,
33 pub version: i32,
34 pub max_attempts: i32,
35 pub default_timeout_seconds: i32,
36 pub default_priority: i32,
37 pub is_enabled: bool,
38 pub created_at: DateTime<Utc>,
39 pub updated_at: DateTime<Utc>,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct JobScheduleJobTypeReference {
45 pub schedule_name: String,
47 pub job_type: JobTypeName,
49}
50
51#[derive(Debug, Clone)]
52pub struct JobDefinitionListFilter<'a> {
53 pub job_type: Option<&'a str>,
56 pub limit: i64,
57 pub offset: i64,
58}
59
60#[derive(Debug, Clone)]
61pub struct JobDefinitionUpdate {
62 pub max_attempts: Option<i32>,
63 pub default_timeout_seconds: Option<i32>,
64 pub default_priority: Option<i32>,
65 pub is_enabled: Option<bool>,
66}
67
68#[derive(Debug, Clone)]
69pub struct JobRuntimeConfigUpsert<'a> {
70 pub job_type: JobType<'a>,
71 pub schema_version: i32,
72 pub config: &'a Value,
73 pub updated_by_user_id: Option<Uuid>,
74}
75
76#[derive(Debug, Clone)]
77pub struct JobRuntimeConfigRecord {
78 pub job_type: JobTypeName,
79 pub schema_version: i32,
80 pub config: Value,
81 pub updated_by_user_id: Option<Uuid>,
82 pub created_at: DateTime<Utc>,
83 pub updated_at: DateTime<Utc>,
84}
85
86#[derive(Debug, Clone)]
87pub struct JobEnqueue<'a> {
88 pub job_type: JobType<'a>,
89 pub organization_id: Option<Uuid>,
90 pub payload: &'a Value,
91 pub priority: Option<i32>,
92 pub max_attempts: Option<i32>,
93 pub timeout_seconds: Option<i32>,
94 pub next_run_at: Option<DateTime<Utc>>,
98 pub idempotency_key: Option<&'a str>,
99 pub stage: Option<JobStage>,
100}
101
102#[derive(Debug, Clone)]
103pub struct JobScheduleRecord {
104 pub id: Uuid,
106 pub name: String,
108 pub job_type: JobTypeName,
110 pub organization_id: Option<Uuid>,
112 pub payload_template: Value,
115 pub cron_expr: String,
117 pub is_active: bool,
122 pub max_jitter_seconds: i32,
125 pub next_fire_at: DateTime<Utc>,
127}
128
129#[derive(Debug, Clone)]
148pub struct JobScheduleUpsert<'a> {
149 pub name: &'a str,
151 pub job_type: JobType<'a>,
153 pub organization_id: Option<Uuid>,
155 pub payload_template: &'a Value,
157 pub cron_expr: &'a str,
160 pub is_active: bool,
162 pub next_fire_at: DateTime<Utc>,
164 pub max_jitter_seconds: i32,
167}
168
169#[derive(Debug, Clone)]
171pub struct JobScheduleCatalogSyncEntry<'a> {
172 pub upsert: JobScheduleUpsert<'a>,
176}
177
178#[non_exhaustive]
180#[derive(Debug, Clone, PartialEq, Eq)]
181pub struct JobScheduleCatalogSyncReport {
182 pub synced_schedule_names: Vec<String>,
184}
185
186#[derive(Debug, Clone)]
187pub struct JobQueueRecord {
188 pub id: Uuid,
189 pub job_type: JobTypeName,
190 pub organization_id: Option<Uuid>,
191 pub payload: Value,
192 pub status: JobStatus,
193 pub priority: i32,
194 pub run_number: i32,
195 pub attempt: i32,
196 pub max_attempts: i32,
197 pub timeout_seconds: i32,
198 pub next_run_at: DateTime<Utc>,
199 pub lease_expires_at: Option<DateTime<Utc>>,
200 pub last_heartbeat_at: Option<DateTime<Utc>>,
201 pub worker_id: Option<String>,
202 pub started_at: Option<DateTime<Utc>>,
203 pub finished_at: Option<DateTime<Utc>>,
204 pub stage: JobStage,
205 pub progress_done: Option<i64>,
206 pub progress_total: Option<i64>,
207 pub progress_pct: Option<f64>,
208 pub checkpoint: Option<Value>,
209 pub output: Option<Value>,
210 pub idempotency_key: Option<String>,
211 pub status_reason: Option<String>,
212 pub last_error_code: Option<String>,
213 pub last_error_message: Option<String>,
214 pub created_at: DateTime<Utc>,
215 pub updated_at: DateTime<Utc>,
216}
217
218#[derive(Debug, Clone)]
219pub struct JobEventRecord {
220 pub id: i64,
221 pub job_id: Uuid,
222 pub run_number: i32,
223 pub attempt: Option<i32>,
224 pub event_type: JobEventType,
225 pub stage: Option<JobStage>,
226 pub progress_done: Option<i64>,
227 pub progress_total: Option<i64>,
228 pub payload: Value,
229 pub occurred_at: DateTime<Utc>,
230}
231
232#[derive(Debug, Clone)]
233pub struct ReapedTerminalLeaseRecord {
234 pub job_id: Uuid,
235 pub job_type: JobTypeName,
236 pub organization_id: Option<Uuid>,
237 pub run_number: i32,
238 pub attempt: i32,
239 pub payload: Value,
240}
241
242#[non_exhaustive]
243#[derive(Debug, Clone)]
244pub struct ReapExpiredLeaseDeferredError {
245 pub job_id: Uuid,
246 pub run_number: i32,
247 pub attempt: i32,
248 pub error_code: String,
249 pub error_message: String,
250 pub sqlstate: Option<String>,
251}
252
253#[derive(Debug, Clone)]
254pub struct ReapExpiredLeasesResult {
255 pub processed: i64,
256 pub terminal_dead_lettered: Vec<ReapedTerminalLeaseRecord>,
257}
258
259#[non_exhaustive]
260#[derive(Debug, Clone)]
261pub struct ReapExpiredLeasesDetailedResult {
262 pub summary: ReapExpiredLeasesResult,
263 pub deferred_row_error_count: usize,
264 pub deferred_row_errors: Vec<ReapExpiredLeaseDeferredError>,
265}
266
267#[derive(Debug, Clone)]
268pub struct JobMetricsRecord {
269 pub job_type: JobTypeName,
270 pub pending_count: i64,
271 pub leased_count: i64,
272 pub stale_leases: i64,
273 pub succeeded_24h: i64,
274 pub retryable_24h: i64,
275 pub terminal_24h: i64,
276 pub panicked_24h: i64,
277 pub timeout_24h: i64,
278 pub dead_lettered_24h: i64,
279 pub p50_duration_ms_24h: Option<f64>,
280 pub p95_duration_ms_24h: Option<f64>,
281}
282
283#[derive(Debug, Clone)]
284pub struct JobLogRecord {
285 pub id: i64,
286 pub job_id: Uuid,
287 pub run_number: i32,
288 pub attempt: Option<i32>,
289 pub level: String,
290 pub message: String,
291 pub payload: Value,
292 pub occurred_at: DateTime<Utc>,
293}
294
295#[derive(Debug, Clone)]
296pub struct JobLogRecordInput {
297 pub job_id: Uuid,
298 pub run_number: i32,
299 pub attempt: Option<i32>,
300 pub level: String,
301 pub message: String,
302 pub payload: Value,
303}
304
305#[derive(Debug, Clone)]
306pub struct JobProgressUpdate<'a> {
307 pub stage: Option<JobStage>,
308 pub progress_done: Option<i64>,
309 pub progress_total: Option<i64>,
310 pub checkpoint: Option<&'a Value>,
311}
312
313#[derive(Debug, Clone)]
314pub struct JobCompletionUpdate<'a> {
315 pub progress_done: Option<i64>,
316 pub progress_total: Option<i64>,
317 pub checkpoint: Option<&'a Value>,
318 pub output: Option<&'a Value>,
319}
320
321#[derive(Debug, Clone)]
322pub struct JobFailureUpdate<'a> {
323 pub kind: JobFailureKind,
324 pub code: &'a str,
325 pub message: &'a str,
326 pub retry_delay_ms: Option<i32>,
327}
328
329#[derive(Debug, Clone)]
330pub struct JobListFilter<'a> {
331 pub organization_id: Option<Uuid>,
332 pub status: Option<JobStatus>,
333 pub job_type: Option<&'a str>,
336 pub limit: i64,
337 pub offset: i64,
338}
339
340#[derive(Debug, Clone)]
341pub struct JobRuntimeConfigListFilter<'a> {
342 pub job_type: Option<&'a str>,
345 pub limit: i64,
346 pub offset: i64,
347}