tools/systemd/time: convert the resulting timestamp into an option

we want to use dates for the calendarspec, and with that there are some
impossible combinations that cannot be detected during parsing
(e.g. some datetimes do not exist in some timezones, and the timezone
can change after setting the schedule)

so finding no timestamp is not an error anymore but a valid result

we omit logging in that case (since it is not an error anymore)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-09-04 14:33:27 +02:00 committed by Dietmar Maurer
parent cb73b2d69c
commit 15ec790a40
3 changed files with 18 additions and 10 deletions

View File

@ -57,7 +57,8 @@ pub fn list_sync_jobs(
job.next_run = (|| -> Option<i64> { job.next_run = (|| -> Option<i64> {
let schedule = job.schedule.as_ref()?; let schedule = job.schedule.as_ref()?;
let event = parse_calendar_event(&schedule).ok()?; let event = parse_calendar_event(&schedule).ok()?;
compute_next_event(&event, last, false).ok() // ignore errors
compute_next_event(&event, last, false).unwrap_or_else(|_| None)
})(); })();
} }

View File

@ -301,7 +301,8 @@ async fn schedule_datastore_garbage_collection() {
}; };
let next = match compute_next_event(&event, last, false) { let next = match compute_next_event(&event, last, false) {
Ok(next) => next, Ok(Some(next)) => next,
Ok(None) => continue,
Err(err) => { Err(err) => {
eprintln!("compute_next_event for '{}' failed - {}", event_str, err); eprintln!("compute_next_event for '{}' failed - {}", event_str, err);
continue; continue;
@ -412,7 +413,8 @@ async fn schedule_datastore_prune() {
}; };
let next = match compute_next_event(&event, last, false) { let next = match compute_next_event(&event, last, false) {
Ok(next) => next, Ok(Some(next)) => next,
Ok(None) => continue,
Err(err) => { Err(err) => {
eprintln!("compute_next_event for '{}' failed - {}", event_str, err); eprintln!("compute_next_event for '{}' failed - {}", event_str, err);
continue; continue;
@ -520,7 +522,8 @@ async fn schedule_datastore_sync_jobs() {
}; };
let next = match compute_next_event(&event, last, false) { let next = match compute_next_event(&event, last, false) {
Ok(next) => next, Ok(Some(next)) => next,
Ok(None) => continue,
Err(err) => { Err(err) => {
eprintln!("compute_next_event for '{}' failed - {}", event_str, err); eprintln!("compute_next_event for '{}' failed - {}", event_str, err);
continue; continue;

View File

@ -1,4 +1,4 @@
use anyhow::{bail, Error}; use anyhow::Error;
use bitflags::bitflags; use bitflags::bitflags;
pub use super::parse_time::*; pub use super::parse_time::*;
@ -155,7 +155,7 @@ pub fn compute_next_event(
event: &CalendarEvent, event: &CalendarEvent,
last: i64, last: i64,
utc: bool, utc: bool,
) -> Result<i64, Error> { ) -> Result<Option<i64>, Error> {
let last = last + 1; // at least one second later let last = last + 1; // at least one second later
@ -166,8 +166,9 @@ pub fn compute_next_event(
let mut count = 0; let mut count = 0;
loop { loop {
if count > 1000 { // should not happen // cancel after 1000 loops
bail!("unable to compute next calendar event"); if count > 1000 {
return Ok(None);
} else { } else {
count += 1; count += 1;
} }
@ -235,13 +236,15 @@ pub fn compute_next_event(
} }
let next = t.into_epoch()?; let next = t.into_epoch()?;
return Ok(next) return Ok(Some(next))
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use anyhow::bail;
use super::*; use super::*;
use proxmox::tools::time::*; use proxmox::tools::time::*;
@ -268,7 +271,7 @@ mod test {
}; };
match compute_next_event(&event, last, true) { match compute_next_event(&event, last, true) {
Ok(next) => { Ok(Some(next)) => {
if next == expect { if next == expect {
println!("next {:?} => {}", event, next); println!("next {:?} => {}", event, next);
} else { } else {
@ -276,6 +279,7 @@ mod test {
event, gmtime(next), gmtime(expect)); event, gmtime(next), gmtime(expect));
} }
} }
Ok(None) => bail!("next {:?} failed to find a timestamp", event),
Err(err) => bail!("compute next for '{}' failed - {}", v, err), Err(err) => bail!("compute next for '{}' failed - {}", v, err),
} }