Skip to main content

ora_backend/
schedules.rs

1//! Schedules and related types.
2
3use std::time::{Duration, SystemTime};
4
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8use crate::{
9    common::{Label, LabelFilter, TimeRange},
10    jobs::{JobDefinition, JobTypeId},
11};
12
13/// A schedule ID.
14#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
15#[repr(transparent)]
16pub struct ScheduleId(pub Uuid);
17
18impl std::fmt::Display for ScheduleId {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(f, "{}", self.0)
21    }
22}
23
24impl From<Uuid> for ScheduleId {
25    fn from(value: Uuid) -> Self {
26        Self(value)
27    }
28}
29
30impl From<ScheduleId> for Uuid {
31    fn from(value: ScheduleId) -> Self {
32        value.0
33    }
34}
35
36/// Definition of a schedule.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ScheduleDefinition {
39    /// The scheduling policy for the schedule.
40    pub scheduling: SchedulingPolicy,
41    /// The job template to be used when creating new jobs.
42    pub job_template: JobDefinition,
43    /// The labels of the schedule.
44    pub labels: Vec<Label>,
45    /// The time range for the schedule.
46    pub time_range: TimeRange,
47}
48
49/// The scheduling policy of a schedule.
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub enum SchedulingPolicy {
52    /// Repeat at fixed intervals.
53    FixedInterval {
54        /// The interval between executions.
55        interval: Duration,
56        /// Whether to immediately create
57        /// a job upon schedule creation.
58        immediate: bool,
59        /// Policy for handling missed executions.
60        missed: MissedTimePolicy,
61    },
62    /// Repeat according to a cron expression.
63    Cron {
64        /// The cron expression defining the schedule.
65        expression: String,
66        /// Whether to immediately create
67        /// a job upon schedule creation.
68        immediate: bool,
69        /// Policy for handling missed executions.
70        missed: MissedTimePolicy,
71    },
72}
73
74/// Policy for handling missed execution times.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
76pub enum MissedTimePolicy {
77    /// Skip missed execution times.
78    Skip,
79    /// Create jobs for missed execution times.
80    Create,
81}
82
83/// Schedule details.
84pub struct ScheduleDetails {
85    /// The unique ID of the schedule.
86    pub id: ScheduleId,
87    /// The schedule definition.
88    pub schedule: ScheduleDefinition,
89    /// The timestamp when the schedule was created.
90    pub created_at: SystemTime,
91    /// The status of the schedule.
92    pub status: ScheduleStatus,
93    /// The timestamp when the schedule was stopped (if any).
94    pub stopped_at: Option<SystemTime>,
95}
96
97/// The status of a schedule.
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
99pub enum ScheduleStatus {
100    /// The schedule is active.
101    Active,
102    /// The schedule was stopped.
103    Stopped,
104}
105
106/// Filters for listing schedules.
107#[derive(Debug, Default, Clone, Serialize, Deserialize)]
108pub struct ScheduleFilters {
109    /// Filter by schedule IDs.
110    pub schedule_ids: Option<Vec<ScheduleId>>,
111    /// Filter by job type IDs.
112    pub job_type_ids: Option<Vec<JobTypeId>>,
113    /// Filter by status.
114    pub statuses: Option<Vec<ScheduleStatus>>,
115    /// Filter by creation time.
116    /// The range can be open-ended in either direction.
117    pub created_at: Option<TimeRange>,
118    /// Filter by labels.
119    pub labels: Option<Vec<LabelFilter>>,
120}
121
122/// The ordering options for listing schedules.
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub enum ScheduleOrderBy {
125    /// Order by creation time ascending.
126    CreatedAtAsc,
127    /// Order by creation time descending.
128    CreatedAtDesc,
129}
130
131/// A schedule that was stopped.
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct StoppedSchedule {
134    /// The schedule ID.
135    pub schedule_id: ScheduleId,
136}
137
138/// A schedule that is active but has no active jobs.
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct PendingSchedule {
141    /// The schedule ID.
142    pub schedule_id: ScheduleId,
143    /// The target execution time of the last job
144    /// belonging to this schedule.
145    pub last_target_execution_time: Option<SystemTime>,
146    /// The scheduling policy.
147    pub scheduling: SchedulingPolicy,
148    /// The time range of the schedule.
149    pub time_range: TimeRange,
150    /// The job template.
151    pub job_template: JobDefinition,
152}