diff --git a/hooks/src/commands/reap.ts b/hooks/src/commands/reap.ts index 07fef3b..a3bfb76 100644 --- a/hooks/src/commands/reap.ts +++ b/hooks/src/commands/reap.ts @@ -39,51 +39,55 @@ async function main(argv): Promise { const images = await smembersAsync("current.images"); console.log(` there are ${images.length} total images to evaluate`); for (const image of images) { - const expireAt = await hgetAsync(image, "expires"); + try { + const expireAt = await hgetAsync(image, "expires"); - if (+expireAt > now) { - const minutesLeft = (+expireAt - now) / 1000 / 60; - console.log(`not expiring ${image} for another ~${Math.round(minutesLeft)} minute(s)`); - continue; - } - - const imageAndTag = image.split(":"); - const headers = { - "Accept": "application/vnd.docker.distribution.manifest.v2+json", - }; - - // Get the manifest from the tag - const getOptions = { - method: "HEAD", - uri: `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${imageAndTag[1]}`, - headers, - resolveWithFullResponse: true, - simple: false, - } - const getResponse = await rp(getOptions); - - if (getResponse.statusCode == 404) { + if (+expireAt > now) { + const minutesLeft = (+expireAt - now) / 1000 / 60; + console.log(`not expiring ${image} for another ~${Math.round(minutesLeft)} minute(s)`); + continue; + } + + const imageAndTag = image.split(":"); + const headers = { + "Accept": "application/vnd.docker.distribution.manifest.v2+json", + }; + + // Get the manifest from the tag + const getOptions = { + method: "HEAD", + uri: `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${imageAndTag[1]}`, + headers, + resolveWithFullResponse: true, + simple: false, + } + const getResponse = await rp(getOptions); + + if (getResponse.statusCode == 404) { + await sremAsync("current.images", image); + await delAsync(image); + continue; + } + + const deleteURI = `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${getResponse.headers.etag.replace(/"/g,"")}`; + + // Remove from the registry + const options = { + method: "DELETE", + uri: deleteURI, + headers, + resolveWithFullResponse: true, + simple: false, + } + + await rp(options); + + console.log(`expiring ${image}`); await sremAsync("current.images", image); await delAsync(image); - continue; + } catch(err) { + console.log(`failed to evaluate image ${image}:`, err); } - - const deleteURI = `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${getResponse.headers.etag.replace(/"/g,"")}`; - - // Remove from the registry - const options = { - method: "DELETE", - uri: deleteURI, - headers, - resolveWithFullResponse: true, - simple: false, - } - - await rp(options); - - console.log(`expiring ${image}`); - await sremAsync("current.images", image); - await delAsync(image); } }, start: true,