Skip to content

Commit

Permalink
perf: use queue for fetching tags
Browse files Browse the repository at this point in the history
Increase resolving performance.
  • Loading branch information
Denis Frenademetz committed May 30, 2022
1 parent 5416ad4 commit 1cc6611
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"cli-progress": "^3.11.1",
"commander": "^9.2.0",
"easy-table": "^1.2.0",
"fastq": "^1.13.0",
"inquirer": "^8.2.4",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
Expand Down
67 changes: 36 additions & 31 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Bar as CliProgressBar, Presets as CliProgressBarPresets } from 'cli-progress';
import EasyTable from 'easy-table';
import { diff as semverDiff, valid as semverValid } from 'semver';
import { orderBy as _orderBy } from 'lodash';
import { promise as queueAsPromise } from 'fastq';

import { CredentialsLoader, CredentialsStore } from './credentials';
import {
Expand Down Expand Up @@ -66,7 +68,7 @@ class ProgressBarWrapper {
private progressInformation: { total: number; current: number } | undefined;

constructor() {
this.progressBar = new CliProgressBar({}, CliProgressBarPresets.shades_classic);
this.progressBar = new CliProgressBar({ clearOnComplete: true }, CliProgressBarPresets.shades_classic);
}

public start(total: number, initialValue: number): void {
Expand Down Expand Up @@ -142,43 +144,46 @@ export async function listOutdated(options: Options): Promise<OutdatedImage[]> {
const outdatedImages: OutdatedImage[] = [];
progressBar.start(filteredImages.length, 0);

try {
for (const image of filteredImages) {
const { latest, wantedMinor, wantedPatch } = await getImageUpdateTags(credentials, image);

if (image.tag) {
const wantedPatchDiff = wantedPatch && semverDiff(image.tag, wantedPatch);
const wantedMinorDiff = wantedMinor && semverDiff(image.tag, wantedMinor);
const latestDiff = latest && semverDiff(image.tag, latest);
if (wantedPatchDiff || wantedMinorDiff || latestDiff) {
outdatedImages.push({
image,
wantedPatchVersion: wantedPatch || 'NA',
wantedMinorVersion: wantedMinor || 'NA',
latestVersion: latest || 'NA'
});
}
} else {
console.warn(`Skipping image '${image.name}' since we cannot determine its tag!`);
const queue = queueAsPromise<DockerImage>(async image => {
const { latest, wantedMinor, wantedPatch } = await getImageUpdateTags(credentials, image);

if (image.tag) {
const wantedPatchDiff = wantedPatch && semverDiff(image.tag, wantedPatch);
const wantedMinorDiff = wantedMinor && semverDiff(image.tag, wantedMinor);
const latestDiff = latest && semverDiff(image.tag, latest);
if (wantedPatchDiff || wantedMinorDiff || latestDiff) {
outdatedImages.push({
image,
wantedPatchVersion: wantedPatch || 'NA',
wantedMinorVersion: wantedMinor || 'NA',
latestVersion: latest || 'NA'
});
}
progressBar.increment(1);
} else {
console.warn(`Skipping image '${image.name}' since we cannot determine its tag!`);
}
progressBar.increment(1);
}, 10);

try {
filteredImages.forEach(image => queue.push(image));
await queue.drained();
} finally {
progressBar.stop();
} catch (err) {
progressBar.stop();
throw err;
}

const table = new EasyTable();

outdatedImages.forEach(({ image, wantedPatchVersion, wantedMinorVersion, latestVersion }) => {
table.cell('Image', image.name);
table.cell('Current', image.tag);
table.cell('Wanted[~]', wantedPatchVersion);
table.cell('Wanted[^]', wantedMinorVersion);
table.cell('Latest', latestVersion);
table.newRow();
});
_orderBy(outdatedImages, 'image', 'asc').forEach(
({ image, wantedPatchVersion, wantedMinorVersion, latestVersion }) => {
table.cell('Image', image.name);
table.cell('Current', image.tag);
table.cell('Wanted[~]', wantedPatchVersion);
table.cell('Wanted[^]', wantedMinorVersion);
table.cell('Latest', latestVersion);
table.newRow();
}
);

console.log(table.toString());

Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ external-editor@^3.0.3:
iconv-lite "^0.4.24"
tmp "^0.0.33"

fastq@^1.13.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
integrity sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==
dependencies:
reusify "^1.0.4"

figures@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
Expand Down Expand Up @@ -1254,6 +1261,11 @@ restore-cursor@^3.1.0:
onetime "^5.1.0"
signal-exit "^3.0.2"

reusify@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==

rimraf@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
Expand Down

0 comments on commit 1cc6611

Please sign in to comment.