From 1c1b54df8683df319e2ef7899a2ffe30157e2428 Mon Sep 17 00:00:00 2001 From: Sebastiano Tocci Date: Sat, 3 Jun 2023 05:07:29 +0200 Subject: [PATCH] Crono expression parser fix (#1682) --- examples/server_container.toml | 4 ++++ server/core/src/interval.rs | 26 +++++++++++++++++++++----- server/core/src/lib.rs | 9 ++++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/examples/server_container.toml b/examples/server_container.toml index e8c58e48b..a7f746179 100644 --- a/examples/server_container.toml +++ b/examples/server_container.toml @@ -102,6 +102,10 @@ origin = "https://idm.example.com:8443" # schedule = "00 22 * * *" # four times a day at 3 minutes past the hour, every 6th hours # schedule = "03 */6 * * *" +# We also support non standard cron syntax, with the following format: +# sec min hour day of month month day of week year +# (it's very similar to the standard cron syntax, it just allows to specify the seconds +# at the beginning and the year at the end) # Number of backups to keep (default 7) # versions = 7 # diff --git a/server/core/src/interval.rs b/server/core/src/interval.rs index 73cb90e34..e41e3b027 100644 --- a/server/core/src/interval.rs +++ b/server/core/src/interval.rs @@ -56,17 +56,33 @@ impl IntervalActor { #[allow(clippy::result_unit_err)] pub fn start_online_backup( server: &'static QueryServerReadV1, - cfg: &OnlineBackup, + online_backup_config: &OnlineBackup, mut rx: broadcast::Receiver, ) -> Result, ()> { - let outpath = cfg.path.to_owned(); - let versions = cfg.versions; - + let outpath = online_backup_config.path.to_owned(); + let versions = online_backup_config.versions; + let crono_expr = online_backup_config.schedule.as_str().to_string(); + let mut crono_expr_values = crono_expr.split_ascii_whitespace().collect::>(); + let chrono_expr_uses_standard_syntax = crono_expr_values.len() == 5; + if chrono_expr_uses_standard_syntax { + // we add a 0 element at the beginning to simulate the standard crono syntax which always runs + // commands at seconds 00 + crono_expr_values.insert(0, "0"); + crono_expr_values.push("*"); + } + let crono_expr_schedule = crono_expr_values.join(" "); + if chrono_expr_uses_standard_syntax { + info!( + "Provided online backup schedule is: {}, now being transformed to: {}", + crono_expr, crono_expr_schedule + ); + } // Cron expression handling - let cron_expr = Schedule::from_str(cfg.schedule.as_str()).map_err(|e| { + let cron_expr = Schedule::from_str(crono_expr_schedule.as_str()).map_err(|e| { error!("Online backup schedule parse error: {}", e); error!("valid formats are:"); error!("sec min hour day of month month day of week year"); + error!("min hour day of month month day of week"); error!("@hourly | @daily | @weekly"); })?; diff --git a/server/core/src/lib.rs b/server/core/src/lib.rs index a2c2075ec..41db0dee2 100644 --- a/server/core/src/lib.rs +++ b/server/core/src/lib.rs @@ -739,9 +739,12 @@ pub async fn create_server_core( let interval_handle = IntervalActor::start(server_write_ref, broadcast_tx.subscribe()); // Setup timed events associated to the read thread let maybe_backup_handle = match &config.online_backup { - Some(cfg) => { - let handle = - IntervalActor::start_online_backup(server_read_ref, cfg, broadcast_tx.subscribe())?; + Some(online_backup_config) => { + let handle = IntervalActor::start_online_backup( + server_read_ref, + online_backup_config, + broadcast_tx.subscribe(), + )?; Some(handle) } None => {