Skip to content

Commit

Permalink
Merge pull request #323 from snyk/fix/retry-pull
Browse files Browse the repository at this point in the history
Fix/retry pull
  • Loading branch information
Amir Moualem authored Feb 11, 2020
2 parents 2899c9e + b752a6f commit bf0ecdf
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/scanner/images/skopeo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpawnPromiseResult } from 'child-process-promise';
import * as sleep from 'sleep-promise';

import * as processWrapper from '../../common/process';
import * as config from '../../common/config';
Expand Down Expand Up @@ -33,7 +33,7 @@ function prefixRespository(target: string, type: SkopeoRepositoryType): string {
export async function pull(
image: string,
destination: string,
): Promise<SpawnPromiseResult> {
): Promise<void> {
const creds = await credentials.getSourceCredentials(image);
const credentialsParameters = getCredentialParameters(creds);

Expand All @@ -43,7 +43,24 @@ export async function pull(
args.push({body: prefixRespository(image, SkopeoRepositoryType.ImageRegistry), sanitise: false});
args.push({body: prefixRespository(destination, SkopeoRepositoryType.DockerArchive), sanitise: false});

return processWrapper.exec('skopeo', ...args);
await pullWithRetry(args);
}

async function pullWithRetry(args: Array<processWrapper.IProcessArgument>): Promise<void> {
const MAX_ATTEMPTS = 10;
const RETRY_INTERVAL_SEC = 0.2;

for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try {
await processWrapper.exec('skopeo', ...args);
return;
} catch (err) {
if (attempt + 1 > MAX_ATTEMPTS) {
throw err;
}
await sleep(RETRY_INTERVAL_SEC * 1000);
}
}
}

export function getCredentialParameters(credentials: string | undefined): Array<processWrapper.IProcessArgument> {
Expand Down

0 comments on commit bf0ecdf

Please sign in to comment.