TRAP Caching: Add timeouts to upload/download operations

This commit is contained in:
Edoardo Pirovano 2022-09-30 10:44:36 +01:00
parent 61b87c69a6
commit c0641ea1d3
No known key found for this signature in database
GPG key ID: 047556B5D93FFE28
9 changed files with 145 additions and 16 deletions

17
lib/trap-caching.js generated
View file

@ -37,6 +37,11 @@ const CACHE_SIZE_MB = 1024;
// This constant sets the minimum size in megabytes of a TRAP
// cache for us to consider it worth uploading.
const MINIMUM_CACHE_MB_TO_UPLOAD = 10;
// The maximum number of milliseconds to wait for TRAP cache
// uploads or downloads to complete before continuing. Note
// this timeout is per operation, so will be run as many
// times as there are languages with TRAP caching enabled.
const MAX_CACHE_OPERATION_MS = 120000; // Two minutes
async function getTrapCachingExtractorConfigArgs(config) {
const result = [];
for (const language of config.languages)
@ -95,9 +100,11 @@ async function downloadTrapCaches(codeql, languages, logger) {
// The SHA from the base of the PR is the most similar commit we might have a cache for
const preferredKey = await cacheKey(codeql, language, baseSha);
logger.info(`Looking in Actions cache for TRAP cache with key ${preferredKey}`);
const found = await cache.restoreCache([cacheDir], preferredKey, [
const found = await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, cache.restoreCache([cacheDir], preferredKey, [
await cachePrefix(codeql, language), // Fall back to any cache with the right key prefix
]);
]), () => {
logger.info(`Timed out waiting for TRAP cache download for ${language}, will continue without it`);
});
if (found === undefined) {
// We didn't find a TRAP cache in the Actions cache, so the directory on disk is
// still just an empty directory. There's no reason to tell the extractor to use it,
@ -119,7 +126,6 @@ exports.downloadTrapCaches = downloadTrapCaches;
async function uploadTrapCaches(codeql, config, logger) {
if (!(await actionsUtil.isAnalyzingDefaultBranch()))
return false; // Only upload caches from the default branch
const toAwait = [];
for (const language of config.languages) {
const cacheDir = config.trapCaches[language];
if (cacheDir === undefined)
@ -135,9 +141,10 @@ async function uploadTrapCaches(codeql, config, logger) {
}
const key = await cacheKey(codeql, language, process.env.GITHUB_SHA || "unknown");
logger.info(`Uploading TRAP cache to Actions cache with key ${key}`);
toAwait.push(cache.saveCache([cacheDir], key));
await (0, util_1.withTimeout)(MAX_CACHE_OPERATION_MS, cache.saveCache([cacheDir], key), () => {
logger.info(`Timed out waiting for TRAP cache for ${language} to upload, will continue without uploading`);
});
}
await Promise.all(toAwait);
return true;
}
exports.uploadTrapCaches = uploadTrapCaches;