Skip to content

Commit

Permalink
Catch network error (#74)
Browse files Browse the repository at this point in the history
- I saw some S3 uploading failures, probably due to large file size.
- This is a stopgap solution. The plan is to retry and log more
information about the error and the offending files first.
- Will probably need to implement multipart uploading in the future.
Issue: #75
  • Loading branch information
tuliren authored Dec 15, 2022
1 parent 34ba15b commit 0c2379a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
19 changes: 19 additions & 0 deletions action/__tests__/plugins/staticHosting/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,23 @@ describe('uploadFileWithSignedUrl', function () {
});
});
});

describe('when there is network error', () => {
beforeEach(() => {
mockFetch
.mockRejectedValueOnce('Network error')
.mockResolvedValue({ ok: true, status: 204, statusText: 'No content' } as Response);
});

it('retries', async () => {
await uploadFileWithSignedUrl('https://localhost', {}, 'object-key', 'package.json');
expect(mockFetch).toHaveBeenCalledTimes(2);

const warnings = mockCore.error.mock.calls;
expect(warnings.length).toEqual(1);
warnings.forEach((warning) => {
expect(warning[0]).toContain('Network error');
});
});
});
});
36 changes: 21 additions & 15 deletions action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28776,25 +28776,31 @@ const uploadFileWithSignedUrl = (signedUrl, fields, objectKey, localFilePath, dr
form.append('key', objectKey);
form.append('Content-Type', mime_types.lookup(localFilePath) || 'application/octet-stream');
form.append('file', external_fs_default().readFileSync(localFilePath));
// TODO: replace the following code with a retry library
let retry = 0;
const maxRetry = 6;
while (retry <= maxRetry) {
const { ok, status, statusText } = yield node_ponyfill_default()(signedUrl, {
method: 'POST',
body: form
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);
if (ok) {
break;
}
else if (status === 503) {
const waitingMillis = Math.pow(2, retry) * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
yield new Promise((r) => setTimeout(r, waitingMillis));
try {
const { ok, status, statusText } = yield node_ponyfill_default()(signedUrl, {
method: 'POST',
body: form
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);
if (ok) {
break;
}
else if (status === 503) {
const waitingMillis = Math.pow(2, retry) * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
yield new Promise((r) => setTimeout(r, waitingMillis));
}
else {
core.error(`-- Failed to upload ${localFilePath}: ${status} - ${statusText}`);
}
}
else {
throw new Error(statusText);
catch (e) {
core.error(`-- Failed to upload ${localFilePath}: ${e}`);
}
++retry;
}
Expand Down
2 changes: 1 addition & 1 deletion action/dist/index.js.map

Large diffs are not rendered by default.

33 changes: 19 additions & 14 deletions action/src/plugins/staticHosting/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,29 @@ export const uploadFileWithSignedUrl = async (
form.append('Content-Type', mime.lookup(localFilePath) || 'application/octet-stream');
form.append('file', fs.readFileSync(localFilePath));

// TODO: replace the following code with a retry library
let retry = 0;
const maxRetry = 6;
while (retry <= maxRetry) {
const { ok, status, statusText } = await fetch(signedUrl, {
method: 'POST',
body: form as any
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);
try {
const { ok, status, statusText } = await fetch(signedUrl, {
method: 'POST',
body: form as any
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);

if (ok) {
break;
} else if (status === 503) {
const waitingMillis = 2 ** retry * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
await new Promise((r) => setTimeout(r, waitingMillis));
} else {
throw new Error(statusText);
if (ok) {
break;
} else if (status === 503) {
const waitingMillis = 2 ** retry * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
await new Promise((r) => setTimeout(r, waitingMillis));
} else {
core.error(`-- Failed to upload ${localFilePath}: ${status} - ${statusText}`);
}
} catch (e) {
core.error(`-- Failed to upload ${localFilePath}: ${e}`);
}
++retry;
}
Expand Down

0 comments on commit 0c2379a

Please sign in to comment.