can find due after ts and Task w schedule
parent
89185f5016
commit
4ca5ed4d7c
|
|
@ -503,20 +503,20 @@ impl Delta {
|
||||||
struct Task(serde_yaml::Value);
|
struct Task(serde_yaml::Value);
|
||||||
|
|
||||||
impl Task {
|
impl Task {
|
||||||
pub fn next_due(&self) -> Option<u64> {
|
pub fn next_due(&self, after: u64) -> Option<u64> {
|
||||||
match self.schedule() {
|
match self.schedule() {
|
||||||
Some(schedule) => self.parse_schedule_next(schedule),
|
Some(schedule) => self.parse_schedule_next(schedule, after),
|
||||||
None => Some(1),
|
None => Some(1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_schedule_next(&self, schedule: String) -> Option<u64> {
|
fn parse_schedule_next(&self, schedule: String, after: u64) -> Option<u64> {
|
||||||
let mut schedule = schedule;
|
let mut schedule = schedule;
|
||||||
|
|
||||||
if regex::Regex::new(r"^[0-9]+h$").unwrap().is_match(&schedule) {
|
if regex::Regex::new(r"^[0-9]+h$").unwrap().is_match(&schedule) {
|
||||||
let hours = &schedule[..schedule.len() - 1];
|
let hours = &schedule[..schedule.len() - 1];
|
||||||
match hours.parse::<u64>() {
|
match hours.parse::<u64>() {
|
||||||
Ok(hours) => return Some(Delta::now_time() + hours * 60 * 60),
|
Ok(hours) => return Some(after + hours * 60 * 60),
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -528,7 +528,7 @@ impl Task {
|
||||||
schedule += "T00";
|
schedule += "T00";
|
||||||
}
|
}
|
||||||
|
|
||||||
if regex::Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}$")
|
if regex::Regex::new(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}$")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.is_match(&schedule)
|
.is_match(&schedule)
|
||||||
{
|
{
|
||||||
|
|
@ -545,6 +545,20 @@ impl Task {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if regex::Regex::new(r"^([^ ]+ ){4}[^ ]+$")
|
||||||
|
.unwrap()
|
||||||
|
.is_match(&schedule)
|
||||||
|
{
|
||||||
|
let after = chrono::DateTime::from_timestamp(after as i64, 0).unwrap();
|
||||||
|
if let Ok(next) = cron_parser::parse(&schedule, &after) {
|
||||||
|
let seconds = next.format("%s").to_string();
|
||||||
|
match seconds.parse::<u64>() {
|
||||||
|
Ok(n) => return Some(n),
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -570,7 +584,7 @@ mod test_task {
|
||||||
fn test_unscheduled() {
|
fn test_unscheduled() {
|
||||||
let task = Task(serde_yaml::Value::String("hello world".to_string()));
|
let task = Task(serde_yaml::Value::String("hello world".to_string()));
|
||||||
assert_eq!(None, task.schedule());
|
assert_eq!(None, task.schedule());
|
||||||
assert_eq!(Some(1 as u64), task.next_due());
|
assert_eq!(Some(1 as u64), task.next_due(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -579,7 +593,7 @@ mod test_task {
|
||||||
m.insert("schedule".into(), "2006-01-02".into());
|
m.insert("schedule".into(), "2006-01-02".into());
|
||||||
let task = Task(serde_yaml::Value::Mapping(m));
|
let task = Task(serde_yaml::Value::Mapping(m));
|
||||||
assert_eq!(Some("2006-01-02".to_string()), task.schedule());
|
assert_eq!(Some("2006-01-02".to_string()), task.schedule());
|
||||||
assert_eq!(Some(1136160000 as u64), task.next_due());
|
assert_eq!(Some(1136160000 as u64), task.next_due(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -588,7 +602,7 @@ mod test_task {
|
||||||
m.insert("schedule".into(), "2036-01-02".into());
|
m.insert("schedule".into(), "2036-01-02".into());
|
||||||
let task = Task(serde_yaml::Value::Mapping(m));
|
let task = Task(serde_yaml::Value::Mapping(m));
|
||||||
assert_eq!(Some("2036-01-02".to_string()), task.schedule());
|
assert_eq!(Some("2036-01-02".to_string()), task.schedule());
|
||||||
assert_eq!(Some(2082844800 as u64), task.next_due());
|
assert_eq!(Some(2082844800 as u64), task.next_due(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -597,7 +611,7 @@ mod test_task {
|
||||||
m.insert("schedule".into(), "2036-01-02T16".into());
|
m.insert("schedule".into(), "2036-01-02T16".into());
|
||||||
let task = Task(serde_yaml::Value::Mapping(m));
|
let task = Task(serde_yaml::Value::Mapping(m));
|
||||||
assert_eq!(Some("2036-01-02T16".to_string()), task.schedule());
|
assert_eq!(Some("2036-01-02T16".to_string()), task.schedule());
|
||||||
assert_eq!(Some(2082902400 as u64), task.next_due());
|
assert_eq!(Some(2082902400 as u64), task.next_due(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -606,7 +620,7 @@ mod test_task {
|
||||||
m.insert("schedule".into(), "1h".into());
|
m.insert("schedule".into(), "1h".into());
|
||||||
let task = Task(serde_yaml::Value::Mapping(m));
|
let task = Task(serde_yaml::Value::Mapping(m));
|
||||||
assert_eq!(Some("1h".to_string()), task.schedule());
|
assert_eq!(Some("1h".to_string()), task.schedule());
|
||||||
assert_eq!(Some(Delta::now_time() + 60 * 60 as u64), task.next_due());
|
assert_eq!(Some(3700), task.next_due(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -615,7 +629,7 @@ mod test_task {
|
||||||
m.insert("schedule".into(), "* * * * *".into());
|
m.insert("schedule".into(), "* * * * *".into());
|
||||||
let task = Task(serde_yaml::Value::Mapping(m));
|
let task = Task(serde_yaml::Value::Mapping(m));
|
||||||
assert_eq!(Some("* * * * *".to_string()), task.schedule());
|
assert_eq!(Some("* * * * *".to_string()), task.schedule());
|
||||||
assert_eq!(Some((Delta::now_time()) + 60 % 60 as u64), task.next_due());
|
assert_eq!(Some(120 as u64), task.next_due(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue