Skip to content

Commit bf0ecdf

Browse files
author
Amir Moualem
authored
Merge pull request #323 from snyk/fix/retry-pull
Fix/retry pull
2 parents 2899c9e + b752a6f commit bf0ecdf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/scanner/images/skopeo.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SpawnPromiseResult } from 'child-process-promise';
1+
import * as sleep from 'sleep-promise';
22

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

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

46-
return processWrapper.exec('skopeo', ...args);
46+
await pullWithRetry(args);
47+
}
48+
49+
async function pullWithRetry(args: Array<processWrapper.IProcessArgument>): Promise<void> {
50+
const MAX_ATTEMPTS = 10;
51+
const RETRY_INTERVAL_SEC = 0.2;
52+
53+
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
54+
try {
55+
await processWrapper.exec('skopeo', ...args);
56+
return;
57+
} catch (err) {
58+
if (attempt + 1 > MAX_ATTEMPTS) {
59+
throw err;
60+
}
61+
await sleep(RETRY_INTERVAL_SEC * 1000);
62+
}
63+
}
4764
}
4865

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

0 commit comments

Comments
 (0)