summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs
index 7c62521..8c7f5d8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -124,7 +124,10 @@ fn update_images(now: &chrono::DateTime<chrono::Utc>, db: &dyn Db, reg: &dyn Reg
Ok((fetched_tags, None))
}
- None => Err(format!("image not found: {}", image_name)),
+ None => {
+ db.update_last_checked(image_id, now);
+ Err(format!("image not found: {}", image_name))
+ }
}
} else {
println!("Valid in cache");
@@ -302,7 +305,7 @@ fn is_yaml(path: &std::path::Path) -> bool {
return false;
}
-fn run_tool(db: &dyn Db, reg: &dyn Registries, repos: &Vec<Repository>, mut infile_paths: Vec<std::path::PathBuf>, overwrite: bool) {
+fn run_tool(db: &dyn Db, reg: &dyn Registries, repos: &Vec<Repository>, mut infile_paths: Vec<std::path::PathBuf>, overwrite: bool) -> chrono::DateTime<chrono::Utc> {
for repo in repos {
if repo.dest.exists() {
let mut gitcmd = std::process::Command::new("git");
@@ -492,6 +495,30 @@ fn run_tool(db: &dyn Db, reg: &dyn Registries, repos: &Vec<Repository>, mut infi
}
metrics::get().git_operations.with_label_values(&["push", "success"]).inc();
}
+
+ let now = chrono::offset::Utc::now();
+ let mut min_expiry = now + chrono::Duration::hours(24);
+ for file in &file_inputs {
+ for image_range in &file.images {
+ let img = DockerRef::parse(&file.content, image_range);
+ let registry = img.registry.as_ref()
+ .map(|x| &file.content[x.clone()])
+ .unwrap_or("registry.hub.docker.com");
+ let image_name = &file.content[img.image.clone()];
+
+ if let Some((_, last_checked)) = db.get_image(registry, image_name) {
+ let cache_ttl = reg.get_cache_ttl(registry);
+ let expiry = last_checked + cache_ttl;
+ if expiry < min_expiry {
+ min_expiry = expiry;
+ }
+ }
+ }
+ }
+ if min_expiry < now {
+ min_expiry = now;
+ }
+ return min_expiry;
}
fn main() {
@@ -874,15 +901,17 @@ fn main() {
metrics::get().state.set(metrics::STATE_ACTIVE);
let run_start = std::time::Instant::now();
- run_tool(&sqlite_db, &registry, &repos, infile_paths.clone(), overwrite);
+ let next_wakeup = run_tool(&sqlite_db, &registry, &repos, infile_paths.clone(), overwrite);
let run_duration = run_start.elapsed().as_secs_f64();
metrics::get().run_duration.set(run_duration);
+ metrics::get().next_wakeup.set(next_wakeup.timestamp() as f64);
metrics::get().state.set(metrics::STATE_IDLE);
if !continuous { break; }
- println!("Waiting for next run");
- std::thread::sleep(std::time::Duration::from_hours(24));
+ let sleep_duration = next_wakeup - chrono::offset::Utc::now();
+ println!("Waiting {} seconds for next run", sleep_duration.num_seconds());
+ std::thread::sleep(sleep_duration.to_std().unwrap());
}
}