diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2026-02-07 20:40:44 +0100 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2026-02-07 20:40:44 +0100 |
| commit | f634dab8509ce012c99dcba26392000424e49c82 (patch) | |
| tree | cb46317e4c6d5556d15a5c21116faaf724b13bd4 /src/main.rs | |
| parent | e487a19e6096ee76a131c2a69b81844891e134b0 (diff) | |
Start to peel apart the update loop
I also moved over to passing borrows of the time. This should have been
a separate commit, but i forgot.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index ed3c9d5..680bc9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,32 +52,52 @@ struct FileInput { images: Vec<Range<usize>>, } -fn update_images(db: &dyn Db, reg: &dyn Registry, files: &[FileInput], outcomes: &mut Vec<Result<Vec<FilePatch>, String>>) { +fn update_images(now: &chrono::DateTime<chrono::Utc>, db: &dyn Db, reg: &dyn Registry, files: &[FileInput], outcomes: &mut Vec<Result<Vec<FilePatch>, String>>) { for file in files { let mut patches = vec![]; let mut error: Option<String> = None; + let mut imgs: Vec<DockerRef> = vec![]; + let mut image_ids: Vec<i64> = vec![]; + let mut last_checkeds: Vec<chrono::DateTime<chrono::Utc>> = vec![]; + for image in &file.images { - println!("Checking image {}", &file.content[image.clone()]); let img = DockerRef::parse(&file.content, image); - let registry = img.registry.as_ref().map(|x| &file.content[x.clone()]).unwrap_or("registry.hub.docker.com"); - let mut tag = img.tag.as_ref().map(|x| file.content[x.clone()].to_string()); + 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()]; - metrics::get().images_checked.with_label_values(&[registry]).inc(); - - let now = chrono::offset::Utc::now(); - let cache_max_age = chrono::Duration::hours(24); - let (image_id, last_checked) = match db.get_image(registry, image_name) { Some(x) => x, None => { - let id = db.insert_image(registry, image_name, now); - (id, now) + let epoch = chrono::DateTime::<chrono::Utc>::UNIX_EPOCH; + let id = db.insert_image(registry, image_name, &epoch); + (id, epoch) }, }; - let cache_stale = now - last_checked > cache_max_age; + imgs.push(img); + image_ids.push(image_id); + last_checkeds.push(last_checked); + } + + let cache_max_age = chrono::Duration::hours(24); + + for i in 0..imgs.len() { + let img = &imgs[i]; + let registry = img.registry.as_ref() + .map(|x| &file.content[x.clone()]) + .unwrap_or("registry.hub.docker.com"); + let mut tag = img.tag.as_ref().map(|x| file.content[x.clone()].to_string()); + let image_name = &file.content[img.image.clone()]; + let image_id = image_ids[i]; + let last_checked = last_checkeds[i]; + + println!("Checking image {}", &file.content[file.images[i].clone()]); + metrics::get().images_checked.with_label_values(&[registry]).inc(); + + let cache_stale = *now - last_checked > cache_max_age; let cached_tags: Vec<String> = db.get_tags_sorted(image_id); @@ -143,7 +163,7 @@ fn update_images(db: &dyn Db, reg: &dyn Registry, files: &[FileInput], outcomes: if let Some(new_tag) = new_tag { tag = Some(new_tag.clone()); patches.push(FilePatch { - position: img.tag.unwrap(), + position: img.tag.clone().unwrap(), content: new_tag, }); metrics::get().images_updated.with_label_values(&[registry]).inc(); @@ -393,8 +413,9 @@ fn run_tool(db: &dyn Db, reg: &dyn Registry, repos: &Vec<Repository>, mut infile file_inputs.push(FileInput { path, content, images }); } + let now = chrono::offset::Utc::now(); let mut outcomes: Vec<Result<Vec<FilePatch>, String>> = Vec::with_capacity(file_inputs.len()); - update_images(db, reg, &file_inputs, &mut outcomes); + update_images(&now, db, reg, &file_inputs, &mut outcomes); for i in 0..file_inputs.len() { let file = &file_inputs[i]; |
