use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScheduledInput {
pub scheduled_time: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub context: Option<Value>,
}
impl ScheduledInput {
pub fn context_as<T: serde::de::DeserializeOwned>(&self) -> crate::Result<Option<T>> {
match &self.context {
Some(v) => Ok(Some(serde_json::from_value(v.clone())?)),
None => Ok(None),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScheduleStatus {
Active,
Paused,
}
impl ScheduleStatus {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Active => "ACTIVE",
Self::Paused => "PAUSED",
}
}
pub(crate) fn parse(s: &str) -> Self {
match s {
"PAUSED" => Self::Paused,
_ => Self::Active,
}
}
}
#[derive(Clone, Debug)]
pub struct WorkflowSchedule {
pub schedule_id: String,
pub schedule_name: String,
pub workflow_name: String,
pub schedule: String,
pub status: ScheduleStatus,
pub context: Option<Value>,
pub last_fired_at: Option<DateTime<Utc>>,
pub automatic_backfill: bool,
pub cron_timezone: Option<String>,
pub queue_name: Option<String>,
}
impl WorkflowSchedule {
pub(crate) fn tick_input(&self, instant: DateTime<Utc>) -> ScheduledInput {
ScheduledInput {
scheduled_time: instant,
context: self.context.clone(),
}
}
}
#[derive(Clone, Default)]
pub struct ScheduleOptions {
pub(crate) context: Option<Value>,
pub(crate) automatic_backfill: bool,
pub(crate) cron_timezone: Option<String>,
pub(crate) queue_name: Option<String>,
}
impl ScheduleOptions {
pub fn new() -> Self {
Self::default()
}
pub fn context<T: serde::Serialize>(mut self, ctx: &T) -> Self {
self.context = serde_json::to_value(ctx).ok();
self
}
pub fn automatic_backfill(mut self, on: bool) -> Self {
self.automatic_backfill = on;
self
}
pub fn cron_timezone(mut self, tz: impl Into<String>) -> Self {
self.cron_timezone = Some(tz.into());
self
}
pub fn queue_name(mut self, name: impl Into<String>) -> Self {
self.queue_name = Some(name.into());
self
}
}
#[derive(Clone)]
pub struct ApplySchedule {
pub(crate) schedule_name: String,
pub(crate) workflow_name: String,
pub(crate) schedule: String,
pub(crate) options: ScheduleOptions,
}
impl ApplySchedule {
pub fn new(
schedule_name: impl Into<String>,
workflow_name: impl Into<String>,
cron: impl Into<String>,
) -> Self {
Self {
schedule_name: schedule_name.into(),
workflow_name: workflow_name.into(),
schedule: cron.into(),
options: ScheduleOptions::new(),
}
}
pub fn options(mut self, options: ScheduleOptions) -> Self {
self.options = options;
self
}
}
#[derive(Clone, Default)]
pub struct ScheduleFilter {
pub statuses: Vec<ScheduleStatus>,
pub workflow_names: Vec<String>,
pub name_prefixes: Vec<String>,
}