fix: wait timeout implementation

This commit is contained in:
Ivan Dlugos 2022-03-03 20:18:19 +01:00
parent 7825bea020
commit 6e62761105
3 changed files with 19 additions and 22 deletions

View file

@ -223,7 +223,7 @@ jobs:
{ {
Write-Error "Expected files do not exist" Write-Error "Expected files do not exist"
} }
if(!(Get-Content $fileA) -ceq "Lorem ipsum dolor sit amet")) if(!(Get-Content $fileA) -ceq "Lorem ipsum dolor sit amet")
{ {
Write-Error "File contents of downloaded artifacts are incorrect" Write-Error "File contents of downloaded artifacts are incorrect"
} }

16
dist/index.js vendored
View file

@ -7014,23 +7014,25 @@ function run() {
let runDownload; let runDownload;
// no retry allowed // no retry allowed
if (waitTimeoutStr == '') { if (waitTimeoutStr == '') {
runDownload = (action) => action(); runDownload = (action) => __awaiter(this, void 0, void 0, function* () { return action(); });
} }
else { else {
const waitTimeoutSeconds = parseInt(waitTimeoutStr); const waitTimeoutSeconds = parseInt(waitTimeoutStr);
runDownload = (action) => { runDownload = (action) => __awaiter(this, void 0, void 0, function* () {
const waitUntil = new Date().getSeconds() + waitTimeoutSeconds; const waitUntil = new Date().getSeconds() + waitTimeoutSeconds;
let lastError; let lastError;
do { do {
try { try {
return action(); return yield action();
} }
catch (e) { catch (e) {
lastError = e; lastError = e;
core.info('Waiting for the artifact to become available...');
yield new Promise(f => setTimeout(f, 10000));
} }
} while (new Date().getSeconds() < waitUntil); } while (new Date().getSeconds() < waitUntil);
throw Error('Timeout reached. Latest error: ' + lastError); throw Error('Timeout reached. Latest error: ' + lastError);
}; });
} }
let resolvedPath; let resolvedPath;
// resolve tilde expansions, path.replace only replaces the first occurrence of a pattern // resolve tilde expansions, path.replace only replaces the first occurrence of a pattern
@ -7046,7 +7048,7 @@ function run() {
// download all artifacts // download all artifacts
core.info('No artifact name specified, downloading all artifacts'); core.info('No artifact name specified, downloading all artifacts');
core.info('Creating an extra directory for each artifact that is being downloaded'); core.info('Creating an extra directory for each artifact that is being downloaded');
const downloadResponse = yield runDownload(() => __awaiter(this, void 0, void 0, function* () { return yield artifactClient.downloadAllArtifacts(resolvedPath); })); const downloadResponse = yield runDownload(() => artifactClient.downloadAllArtifacts(resolvedPath));
core.info(`There were ${downloadResponse.length} artifacts downloaded`); core.info(`There were ${downloadResponse.length} artifacts downloaded`);
for (const artifact of downloadResponse) { for (const artifact of downloadResponse) {
core.info(`Artifact ${artifact.artifactName} was downloaded to ${artifact.downloadPath}`); core.info(`Artifact ${artifact.artifactName} was downloaded to ${artifact.downloadPath}`);
@ -7058,9 +7060,7 @@ function run() {
const downloadOptions = { const downloadOptions = {
createArtifactFolder: false createArtifactFolder: false
}; };
const downloadResponse = yield runDownload(() => __awaiter(this, void 0, void 0, function* () { const downloadResponse = yield runDownload(() => artifactClient.downloadArtifact(name, resolvedPath, downloadOptions));
return yield artifactClient.downloadArtifact(name, resolvedPath, downloadOptions);
}));
core.info(`Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}`); core.info(`Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}`);
} }
// output the directory that the artifact(s) was/were downloaded to // output the directory that the artifact(s) was/were downloaded to

View file

@ -10,20 +10,22 @@ async function run(): Promise<void> {
const path = core.getInput(Inputs.Path, {required: false}) const path = core.getInput(Inputs.Path, {required: false})
const waitTimeoutStr = core.getInput(Inputs.WaitTimeout, {required: false}) const waitTimeoutStr = core.getInput(Inputs.WaitTimeout, {required: false})
let runDownload: <T extends unknown>(action: () => T) => T let runDownload: <T extends unknown>(action: () => T) => Promise<T>
// no retry allowed // no retry allowed
if (waitTimeoutStr == '') { if (waitTimeoutStr == '') {
runDownload = <T extends unknown>(action: () => T) => action() runDownload = async <T extends unknown>(action: () => T) => action()
} else { } else {
const waitTimeoutSeconds = parseInt(waitTimeoutStr) const waitTimeoutSeconds = parseInt(waitTimeoutStr)
runDownload = <T extends unknown>(action: () => T) => { runDownload = async <T extends unknown>(action: () => T) => {
const waitUntil = new Date().getSeconds() + waitTimeoutSeconds const waitUntil = new Date().getSeconds() + waitTimeoutSeconds
let lastError let lastError
do { do {
try { try {
return action() return await action()
} catch (e) { } catch (e) {
lastError = e lastError = e
core.info('Waiting for the artifact to become available...')
await new Promise(f => setTimeout(f, 10000))
} }
} while (new Date().getSeconds() < waitUntil) } while (new Date().getSeconds() < waitUntil)
throw Error('Timeout reached. Latest error: ' + lastError) throw Error('Timeout reached. Latest error: ' + lastError)
@ -46,8 +48,8 @@ async function run(): Promise<void> {
core.info( core.info(
'Creating an extra directory for each artifact that is being downloaded' 'Creating an extra directory for each artifact that is being downloaded'
) )
const downloadResponse = await runDownload( const downloadResponse = await runDownload(() =>
async () => await artifactClient.downloadAllArtifacts(resolvedPath) artifactClient.downloadAllArtifacts(resolvedPath)
) )
core.info(`There were ${downloadResponse.length} artifacts downloaded`) core.info(`There were ${downloadResponse.length} artifacts downloaded`)
for (const artifact of downloadResponse) { for (const artifact of downloadResponse) {
@ -61,13 +63,8 @@ async function run(): Promise<void> {
const downloadOptions = { const downloadOptions = {
createArtifactFolder: false createArtifactFolder: false
} }
const downloadResponse = await runDownload( const downloadResponse = await runDownload(() =>
async () => artifactClient.downloadArtifact(name, resolvedPath, downloadOptions)
await artifactClient.downloadArtifact(
name,
resolvedPath,
downloadOptions
)
) )
core.info( core.info(
`Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}` `Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}`