mirror of
https://github.com/actions/cache.git
synced 2025-04-22 03:56:45 +00:00
Add retries to upload chunk
This commit is contained in:
parent
825f95f8f2
commit
61f374ae8c
1 changed files with 34 additions and 6 deletions
|
@ -1,7 +1,7 @@
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { BearerCredentialHandler } from "typed-rest-client/Handlers";
|
import { BearerCredentialHandler } from "typed-rest-client/Handlers";
|
||||||
import { HttpClient } from "typed-rest-client/HttpClient";
|
import { HttpClient, HttpCodes } from "typed-rest-client/HttpClient";
|
||||||
import { IHttpClientResponse } from "typed-rest-client/Interfaces";
|
import { IHttpClientResponse } from "typed-rest-client/Interfaces";
|
||||||
import {
|
import {
|
||||||
IRequestOptions,
|
IRequestOptions,
|
||||||
|
@ -19,6 +19,16 @@ import * as utils from "./utils/actionUtils";
|
||||||
function isSuccessStatusCode(statusCode: number): boolean {
|
function isSuccessStatusCode(statusCode: number): boolean {
|
||||||
return statusCode >= 200 && statusCode < 300;
|
return statusCode >= 200 && statusCode < 300;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isRetryableStatusCode(statusCode: number): boolean {
|
||||||
|
const retryableStatusCodes = [
|
||||||
|
HttpCodes.BadGateway,
|
||||||
|
HttpCodes.ServiceUnavailable,
|
||||||
|
HttpCodes.GatewayTimeout
|
||||||
|
];
|
||||||
|
return retryableStatusCodes.includes(statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
function getCacheApiUrl(): string {
|
function getCacheApiUrl(): string {
|
||||||
// Ideally we just use ACTIONS_CACHE_URL
|
// Ideally we just use ACTIONS_CACHE_URL
|
||||||
const baseUrl: string = (
|
const baseUrl: string = (
|
||||||
|
@ -152,11 +162,29 @@ async function uploadChunk(
|
||||||
"Content-Range": getContentRange(start, end)
|
"Content-Range": getContentRange(start, end)
|
||||||
};
|
};
|
||||||
|
|
||||||
return await restClient.uploadStream<void>(
|
const uploadChunkRequest = async (): Promise<IRestResponse<void>> => {
|
||||||
"PATCH",
|
return await restClient.uploadStream<void>(
|
||||||
resourceUrl,
|
"PATCH",
|
||||||
data,
|
resourceUrl,
|
||||||
requestOptions
|
data,
|
||||||
|
requestOptions
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await uploadChunkRequest();
|
||||||
|
if (isSuccessStatusCode(response.statusCode)) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRetryableStatusCode(response.statusCode)) {
|
||||||
|
const retryResponse = await uploadChunkRequest();
|
||||||
|
if (isSuccessStatusCode(retryResponse.statusCode)) {
|
||||||
|
return retryResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`Cache service responded with ${response.statusCode} during chunk upload.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue