-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.rs
160 lines (129 loc) · 5.5 KB
/
scheduler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::io;
use std::io::prelude::*;
use std::collections::{HashSet, HashMap};
use std::str::FromStr;
use time;
use regex::Regex;
use error::Error;
use error::Error::ErrCronFormat;
pub type SchedulerResult<'a> = Result<Scheduler<'a>, Error>;
#[derive(Debug)]
pub struct Scheduler<'a> {
seconds: &'a str,
minutes: &'a str,
hours: &'a str,
days: &'a str,
months: &'a str,
weekdays: &'a str,
timeFiledsLength: usize,
timePoints: HashMap<&'a str, HashSet<u32>>,
re: Regex,
}
impl<'a> Scheduler<'a> {
pub fn new(intervals: &'a str) -> SchedulerResult {
let reRes = Regex::new(r"^\s*((\*(/\d+)?)|[0-9-,/]+)(\s+((\*(/\d+)?)|[0-9-,/]+)){4,5}\s*$");
match reRes {
Ok(re) => {
if !re.is_match(intervals) {
return Err(ErrCronFormat(format!("invalid format: {}", intervals)));
}
let timeFileds: Vec<&str> = intervals.split_whitespace().collect();
let timeFiledsLength = timeFileds.len();
if timeFiledsLength != 5 && timeFiledsLength != 6 {
return Err(ErrCronFormat(format!("length of itervals should be 5 or 6, \
but got {}",
timeFiledsLength)));
}
let mut sec = "";
let mut startIndex: usize = 0;
if timeFiledsLength == 6 {
sec = timeFileds[0].clone();
startIndex = 1;
}
let mut sch = Scheduler {
seconds: sec,
minutes: timeFileds[startIndex],
hours: timeFileds[startIndex + 1],
days: timeFileds[startIndex + 2],
months: timeFileds[startIndex + 3],
weekdays: timeFileds[startIndex + 4],
timeFiledsLength: timeFiledsLength,
timePoints: HashMap::new(),
re: re,
};
try!(sch.parse_time_fields().map_err(|e| ErrCronFormat(e.to_string())));
Ok(sch)
}
Err(e) => Err(ErrCronFormat(e.to_string())),
}
}
pub fn parse_time_fields(&mut self) -> Result<(), Error> {
if self.seconds != "" {
self.timePoints.insert("seconds", try!(parse_intervals_field(self.seconds, 0, 59)));
} else {
self.timePoints.insert("seconds", [0].iter().cloned().collect::<HashSet<u32>>());
}
self.timePoints.insert("minutes", try!(parse_intervals_field(self.minutes, 0, 59)));
self.timePoints.insert("hours", try!(parse_intervals_field(self.hours, 0, 23)));
self.timePoints.insert("days", try!(parse_intervals_field(self.days, 1, 31)));
self.timePoints.insert("months", try!(parse_intervals_field(self.months, 1, 12)));
self.timePoints.insert("weekdays", try!(parse_intervals_field(self.weekdays, 0, 6)));
Ok(())
}
pub fn is_time_up(&self, t: &time::Tm) -> bool {
let (second, minute, hour, day, month, weekday) = (t.tm_sec as u32,
t.tm_min as u32,
t.tm_hour as u32,
t.tm_mday as u32,
t.tm_mon as u32,
t.tm_wday as u32);
let isSecond = self.timePoints.get("seconds").unwrap().contains(&second);
let isLeft = self.timePoints.get("minutes").unwrap().contains(&minute) &&
self.timePoints.get("hours").unwrap().contains(&hour) &&
self.timePoints.get("days").unwrap().contains(&day) &&
self.timePoints.get("months").unwrap().contains(&month) &&
self.timePoints.get("weekdays").unwrap().contains(&weekday);
if self.timeFiledsLength == 5 {
isLeft
} else {
isSecond && isLeft
}
}
}
fn parse_intervals_field(inter: &str, min: u32, max: u32) -> Result<HashSet<u32>, Error> {
let mut points = HashSet::new();
let parts: Vec<&str> = inter.split(",").collect();
for part in parts {
let x: Vec<&str> = part.split("/").collect();
let y: Vec<&str> = x[0].split("-").collect();
let mut _min = min;
let mut _max = max;
let mut step = 1u32;
let (xLen, yLen) = (x.len(), y.len());
if xLen == 1 && yLen == 1 {
if y[0] != "*" {
_min = try!(y[0].parse::<u32>());
_max = _min;
}
} else if xLen == 1 && yLen == 2 {
_min = try!(y[0].parse::<u32>());
_max = try!(y[1].parse::<u32>());
} else if xLen == 2 && yLen == 1 && x[0] == "*" {
step = try!(x[1].parse::<u32>());
} else {
return Err(ErrCronFormat(String::from(part)));
}
for i in (_min.._max + 1).filter(|x| x % step == 0).collect::<Vec<u32>>() {
points.insert(i);
}
}
Ok(points)
}
#[test]
fn test_parse_intervals() {
assert!(Scheduler::new("*/2 1-8,11 * * *").is_ok());
assert!(Scheduler::new("0 */2 1-8,11 * * *").is_ok());
assert!(Scheduler::new("*/2 1-4,16,11,17 * * *").is_ok());
assert!(Scheduler::new("05 */2 1-8,11 * * * *").is_err());
assert!(Scheduler::new("05 */ 1-8,11 * * *").is_err());
}