Skip to content

Commit d3c6d84

Browse files
committed
refactor JobDescription into struct
1 parent f1e9b30 commit d3c6d84

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/server/cronjobs.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,36 @@ use std::thread;
77
use std::time::Duration;
88

99
const DAY: Duration = Duration::from_secs(60 * 60 * 24);
10-
// The tuple is composed of:
11-
// - job name
12-
// - interval between executions
13-
// - function to execute at specified intervals
14-
type JobDescription = (&'static str, Duration, fn(Arc<Data>) -> Fallible<()>);
15-
16-
lazy_static! {
17-
static ref JOBS: Vec<JobDescription> = {
18-
let mut jobs = Vec::new();
19-
jobs.push((
20-
"crate list update",
21-
DAY,
22-
update_crates as fn(Arc<Data>) -> Fallible<()>,
23-
));
24-
25-
jobs
26-
};
10+
struct JobDescription {
11+
name: &'static str,
12+
interval: Duration,
13+
exec: fn(Arc<Data>) -> Fallible<()>,
2714
}
2815

16+
static JOBS: &[JobDescription] = &[JobDescription {
17+
name: "crates lists update",
18+
interval: DAY,
19+
exec: update_crates as fn(Arc<Data>) -> Fallible<()>,
20+
}];
21+
2922
pub fn spawn(data: Data) {
3023
let data = Arc::new(data);
31-
for job in JOBS.iter() {
32-
let (name, timer, exec) = job;
24+
for job in JOBS {
3325
// needed to make the borrowck happy
3426
let data = Arc::clone(&data);
3527

3628
thread::spawn(move || loop {
37-
let result = exec(Arc::clone(&data));
29+
let result = (job.exec)(Arc::clone(&data));
3830
if let Err(e) = result {
3931
utils::report_failure(&e);
4032
}
4133

4234
info!(
4335
"the {} thread will be respawned in {}s",
44-
name,
45-
timer.as_secs()
36+
job.name,
37+
job.interval.as_secs()
4638
);
47-
thread::sleep(*timer);
39+
thread::sleep(job.interval);
4840
});
4941
}
5042
}

0 commit comments

Comments
 (0)