use chrono::{DateTime, Utc};
use runledger_core::jobs::{JobStage, WorkflowStepEnqueueBuilder};
use runledger_postgres::jobs::{JobEnqueue, JobScheduleUpsert};
use serde_json::Value;
use uuid::Uuid;
use super::{CatalogError, JobCatalog};
#[derive(Debug, Clone)]
pub struct CatalogJobEnqueueInput<'a> {
pub job_type: &'a str,
pub organization_id: Option<Uuid>,
pub payload: &'a Value,
pub priority: Option<i32>,
pub max_attempts: Option<i32>,
pub timeout_seconds: Option<i32>,
pub next_run_at: Option<DateTime<Utc>>,
pub idempotency_key: Option<&'a str>,
pub stage: Option<JobStage>,
}
#[derive(Debug, Clone)]
pub struct CatalogJobScheduleInput<'a> {
pub name: &'a str,
pub job_type: &'a str,
pub organization_id: Option<Uuid>,
pub payload_template: &'a Value,
pub cron_expr: &'a str,
pub is_active: bool,
pub next_fire_at: DateTime<Utc>,
pub max_jitter_seconds: i32,
}
impl JobCatalog {
pub fn job_enqueue<'a>(
&self,
input: &CatalogJobEnqueueInput<'a>,
) -> Result<JobEnqueue<'a>, CatalogError> {
let job_type = self.require_catalog_enabled_job_type(input.job_type)?;
Ok(JobEnqueue {
job_type,
organization_id: input.organization_id,
payload: input.payload,
priority: input.priority,
max_attempts: input.max_attempts,
timeout_seconds: input.timeout_seconds,
next_run_at: input.next_run_at,
idempotency_key: input.idempotency_key,
stage: input.stage,
})
}
pub fn job_schedule<'a>(
&self,
input: &CatalogJobScheduleInput<'a>,
) -> Result<JobScheduleUpsert<'a>, CatalogError> {
let job_type = self.require_catalog_enabled_job_type(input.job_type)?;
Ok(JobScheduleUpsert {
name: input.name,
job_type,
organization_id: input.organization_id,
payload_template: input.payload_template,
cron_expr: input.cron_expr,
is_active: input.is_active,
next_fire_at: input.next_fire_at,
max_jitter_seconds: input.max_jitter_seconds,
})
}
pub fn workflow_step<'a>(
&self,
step_key: &'a str,
job_type_name: &str,
payload: &'a Value,
) -> Result<WorkflowStepEnqueueBuilder<'a>, CatalogError> {
let job_type = self.require_catalog_enabled_job_type(job_type_name)?;
WorkflowStepEnqueueBuilder::try_new(step_key, job_type.as_str(), payload)
.map_err(CatalogError::WorkflowBuild)
}
}