Skip to content

Commit 3ab67a2

Browse files
authored
Merge pull request #2597 from github/mbg/caching/output-improvements
Dependency caching: small output improvements
2 parents 6e3a010 + 7bc6222 commit 3ab67a2

9 files changed

+59
-15
lines changed

Diff for: lib/caching-utils.js

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/caching-utils.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/dependency-caching.js

+18-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/dependency-caching.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/util.js

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/caching-utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import { isHostedRunner, tryGetFolderBytes } from "./util";
99
* Returns the total size of all the specified paths.
1010
* @param paths The paths for which to calculate the total size.
1111
* @param logger A logger to record some informational messages to.
12+
* @param quiet A value indicating whether to suppress logging warnings (default: false).
1213
* @returns The total size of all specified paths.
1314
*/
1415
export async function getTotalCacheSize(
1516
paths: string[],
1617
logger: Logger,
18+
quiet: boolean = false,
1719
): Promise<number> {
1820
const sizes = await Promise.all(
19-
paths.map((cacheDir) => tryGetFolderBytes(cacheDir, logger)),
21+
paths.map((cacheDir) => tryGetFolderBytes(cacheDir, logger, quiet)),
2022
);
2123
return sizes.map((a) => a || 0).reduce((a, b) => a + b, 0);
2224
}

Diff for: src/dependency-caching.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export async function uploadDependencyCaches(config: Config, logger: Logger) {
168168
// use the cache quota that we compete with. In that case, we do not wish to use up all of the quota
169169
// with the dependency caches. For this, we could use the Cache API to check whether other workflows
170170
// are using the quota and how full it is.
171-
const size = await getTotalCacheSize(cacheConfig.paths, logger);
171+
const size = await getTotalCacheSize(cacheConfig.paths, logger, true);
172172

173173
// Skip uploading an empty cache.
174174
if (size === 0) {
@@ -181,10 +181,25 @@ export async function uploadDependencyCaches(config: Config, logger: Logger) {
181181
const key = await cacheKey(language, cacheConfig);
182182

183183
logger.info(
184-
`Uploading cache of size ${size} for ${language} with key ${key}`,
184+
`Uploading cache of size ${size} for ${language} with key ${key}...`,
185185
);
186186

187-
await actionsCache.saveCache(cacheConfig.paths, key);
187+
try {
188+
await actionsCache.saveCache(cacheConfig.paths, key);
189+
} catch (error) {
190+
// `ReserveCacheError` indicates that the cache key is already in use, which means that a
191+
// cache with that key already exists or is in the process of being uploaded by another
192+
// workflow. We can ignore this.
193+
if (error instanceof actionsCache.ReserveCacheError) {
194+
logger.info(
195+
`Not uploading cache for ${language}, because ${key} is already in use.`,
196+
);
197+
logger.debug(error.message);
198+
} else {
199+
// Propagate other errors upwards.
200+
throw error;
201+
}
202+
}
188203
}
189204
}
190205

Diff for: src/util.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -785,16 +785,23 @@ export function listFolder(dir: string): string[] {
785785
*
786786
* @param cacheDir A directory to get the size of.
787787
* @param logger A logger to log any errors to.
788+
* @param quiet A value indicating whether to suppress warnings for errors (default: false).
789+
* Ignored if the log level is `debug`.
788790
* @returns The size in bytes of the folder, or undefined if errors occurred.
789791
*/
790792
export async function tryGetFolderBytes(
791793
cacheDir: string,
792794
logger: Logger,
795+
quiet: boolean = false,
793796
): Promise<number | undefined> {
794797
try {
795798
return await promisify<string, number>(getFolderSize)(cacheDir);
796799
} catch (e) {
797-
logger.warning(`Encountered an error while getting size of folder: ${e}`);
800+
if (!quiet || logger.isDebug()) {
801+
logger.warning(
802+
`Encountered an error while getting size of '${cacheDir}': ${e}`,
803+
);
804+
}
798805
return undefined;
799806
}
800807
}

0 commit comments

Comments
 (0)