1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use chronon_core::{Job, Run, ScheduleKind};
7
8#[derive(Debug, Deserialize, Serialize)]
12pub struct UpsertJobRequest {
13 pub job_name: String,
15 pub script_name: String,
17 pub cron_expr: Option<String>,
19 pub timezone: Option<String>,
21 #[serde(default)]
23 pub schedule_kind: ScheduleKindDto,
24 #[serde(default)]
26 pub params: Value,
27 #[serde(default = "default_true")]
29 pub enabled: bool,
30 #[serde(default = "default_concurrency")]
32 pub concurrency: i32,
33 pub timeout_ms: Option<i64>,
35 #[serde(default)]
37 pub actor_json: Option<Value>,
38 #[serde(default)]
40 pub retry_policy: Option<Value>,
41 #[serde(default)]
43 pub misfire_policy: Option<Value>,
44}
45
46fn default_true() -> bool {
47 true
48}
49
50fn default_concurrency() -> i32 {
51 1
52}
53
54#[derive(Debug, Default, Deserialize, Serialize, Clone, Copy)]
56#[serde(rename_all = "snake_case")]
57pub enum ScheduleKindDto {
58 #[default]
60 Cron,
61 RunOnce,
63 Manual,
65}
66
67impl From<ScheduleKindDto> for ScheduleKind {
68 fn from(dto: ScheduleKindDto) -> Self {
69 match dto {
70 ScheduleKindDto::Cron => Self::Cron,
71 ScheduleKindDto::RunOnce => Self::RunOnce,
72 ScheduleKindDto::Manual => Self::Manual,
73 }
74 }
75}
76
77impl From<ScheduleKind> for ScheduleKindDto {
78 fn from(kind: ScheduleKind) -> Self {
79 match kind {
80 ScheduleKind::Cron => Self::Cron,
81 ScheduleKind::RunOnce => Self::RunOnce,
82 ScheduleKind::Manual => Self::Manual,
83 }
84 }
85}
86
87#[derive(Debug, Serialize, Deserialize)]
89pub struct JobResponse {
90 pub job_id: String,
92 pub job_name: String,
94 pub script_name: String,
96 pub enabled: bool,
98 pub schedule_kind: ScheduleKindDto,
100 pub cron_expr: Option<String>,
102 pub timezone: Option<String>,
104 pub next_run_at: Option<String>,
106 pub current_revision: i32,
108 pub created_at: String,
110 pub updated_at: String,
112}
113
114impl From<Job> for JobResponse {
115 fn from(job: Job) -> Self {
116 Self {
117 job_id: job.job_id,
118 job_name: job.job_name,
119 script_name: job.script_name,
120 enabled: job.enabled,
121 schedule_kind: job.schedule_kind.into(),
122 cron_expr: job.cron_expr,
123 timezone: job.timezone,
124 next_run_at: job.next_run_at.map(|t| t.to_rfc3339()),
125 current_revision: job.current_revision,
126 created_at: job.created_at.to_rfc3339(),
127 updated_at: job.updated_at.to_rfc3339(),
128 }
129 }
130}
131
132#[derive(Debug, Deserialize)]
134pub struct JobActionRequest {
135 pub job_id: String,
137 #[serde(default)]
139 pub params: Option<Value>,
140}
141
142#[derive(Debug, Serialize, Deserialize)]
144pub struct RunResponse {
145 pub run_id: String,
147 pub job_id: Option<String>,
149 pub script_name: String,
151 pub status: String,
153 pub scheduled_for: String,
155 pub started_at: Option<String>,
157 pub finished_at: Option<String>,
159 pub duration_ms: Option<i64>,
161 pub attempt: i32,
163}
164
165impl From<Run> for RunResponse {
166 fn from(run: Run) -> Self {
167 Self {
168 run_id: run.run_id,
169 job_id: run.job_id,
170 script_name: run.script_name,
171 status: run.status.to_string(),
172 scheduled_for: run.scheduled_for.to_rfc3339(),
173 started_at: run.started_at.map(|t| t.to_rfc3339()),
174 finished_at: run.finished_at.map(|t| t.to_rfc3339()),
175 duration_ms: run.duration_ms,
176 attempt: run.attempt,
177 }
178 }
179}
180
181#[derive(Debug, Serialize, Deserialize)]
183pub struct ScriptResponse {
184 pub name: String,
186 pub signature_json: String,
188 pub signature_hash: u64,
190}
191
192#[derive(Debug, Default, Deserialize)]
194pub struct ListJobsQuery {
195 pub job_name: Option<String>,
197 pub script_name: Option<String>,
199 pub enabled: Option<bool>,
201 pub schedule_kind: Option<String>,
203 pub offset: Option<usize>,
205 pub limit: Option<usize>,
207}
208
209#[derive(Debug, Default, Deserialize)]
211pub struct ListRunsQuery {
212 pub job_id: Option<String>,
214 pub status: Option<String>,
216 pub offset: Option<usize>,
218 pub limit: Option<usize>,
220}