use serde::{Deserialize, Serialize};
use serde_json::Value;
use chronon_core::{Job, Run, ScheduleKind};
#[derive(Debug, Deserialize, Serialize)]
pub struct UpsertJobRequest {
pub job_name: String,
pub script_name: String,
pub cron_expr: Option<String>,
pub timezone: Option<String>,
#[serde(default)]
pub schedule_kind: ScheduleKindDto,
#[serde(default)]
pub params: Value,
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default = "default_concurrency")]
pub concurrency: i32,
pub timeout_ms: Option<i64>,
#[serde(default)]
pub actor_json: Option<Value>,
#[serde(default)]
pub retry_policy: Option<Value>,
#[serde(default)]
pub misfire_policy: Option<Value>,
}
fn default_true() -> bool {
true
}
fn default_concurrency() -> i32 {
1
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum ScheduleKindDto {
#[default]
Cron,
RunOnce,
Manual,
}
impl From<ScheduleKindDto> for ScheduleKind {
fn from(dto: ScheduleKindDto) -> Self {
match dto {
ScheduleKindDto::Cron => Self::Cron,
ScheduleKindDto::RunOnce => Self::RunOnce,
ScheduleKindDto::Manual => Self::Manual,
}
}
}
impl From<ScheduleKind> for ScheduleKindDto {
fn from(kind: ScheduleKind) -> Self {
match kind {
ScheduleKind::Cron => Self::Cron,
ScheduleKind::RunOnce => Self::RunOnce,
ScheduleKind::Manual => Self::Manual,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct JobResponse {
pub job_id: String,
pub job_name: String,
pub script_name: String,
pub enabled: bool,
pub schedule_kind: ScheduleKindDto,
pub cron_expr: Option<String>,
pub timezone: Option<String>,
pub next_run_at: Option<String>,
pub current_revision: i32,
pub created_at: String,
pub updated_at: String,
}
impl From<Job> for JobResponse {
fn from(job: Job) -> Self {
Self {
job_id: job.job_id,
job_name: job.job_name,
script_name: job.script_name,
enabled: job.enabled,
schedule_kind: job.schedule_kind.into(),
cron_expr: job.cron_expr,
timezone: job.timezone,
next_run_at: job.next_run_at.map(|t| t.to_rfc3339()),
current_revision: job.current_revision,
created_at: job.created_at.to_rfc3339(),
updated_at: job.updated_at.to_rfc3339(),
}
}
}
#[derive(Debug, Deserialize)]
pub struct JobActionRequest {
pub job_id: String,
#[serde(default)]
pub params: Option<Value>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RunResponse {
pub run_id: String,
pub job_id: Option<String>,
pub script_name: String,
pub status: String,
pub scheduled_for: String,
pub started_at: Option<String>,
pub finished_at: Option<String>,
pub duration_ms: Option<i64>,
pub attempt: i32,
}
impl From<Run> for RunResponse {
fn from(run: Run) -> Self {
Self {
run_id: run.run_id,
job_id: run.job_id,
script_name: run.script_name,
status: run.status.to_string(),
scheduled_for: run.scheduled_for.to_rfc3339(),
started_at: run.started_at.map(|t| t.to_rfc3339()),
finished_at: run.finished_at.map(|t| t.to_rfc3339()),
duration_ms: run.duration_ms,
attempt: run.attempt,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ScriptResponse {
pub name: String,
pub signature_json: String,
pub signature_hash: u64,
}
#[derive(Debug, Default, Deserialize)]
pub struct ListJobsQuery {
pub job_name: Option<String>,
pub script_name: Option<String>,
pub enabled: Option<bool>,
pub schedule_kind: Option<String>,
pub offset: Option<usize>,
pub limit: Option<usize>,
}
#[derive(Debug, Default, Deserialize)]
pub struct ListRunsQuery {
pub job_id: Option<String>,
pub status: Option<String>,
pub offset: Option<usize>,
pub limit: Option<usize>,
}