Skip to content

Commit 362aa1b

Browse files
authored
refactor: cache handling and improve cache action logs (#46)
* fix: use getBooleanInput and add logging * chore: update dist build
1 parent 66af43d commit 362aa1b

File tree

8 files changed

+153
-56
lines changed

8 files changed

+153
-56
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ runs:
2626
using: "node20"
2727
main: "dist/index.js"
2828
post: "dist/save/index.js"
29-
post-if: "github.event.inputs.cache && success()"
29+
post-if: success()

dist/index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86172,6 +86172,7 @@ function wrappy (fn, cb) {
8617286172
/***/ 2121:
8617386173
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
8617486174

86175+
const core = __nccwpck_require__(2186);
8617586176
const cache = __nccwpck_require__(7799);
8617686177
const github = __nccwpck_require__(5438);
8617786178
const fs = __nccwpck_require__(7147);
@@ -86183,16 +86184,27 @@ const PLATFORM = os.platform();
8618386184
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];
8618486185

8618586186
async function restoreRPCCache() {
86186-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
86187+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
8618786188
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
86188-
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
86189+
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
86190+
if (!cacheKey) {
86191+
core.info("Cache not found");
86192+
return;
86193+
}
86194+
core.info(`Cache restored from key: ${cacheKey}`);
8618986195
}
8619086196

8619186197
async function saveCache() {
86192-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
86193-
if (fs.existsSync(CACHE_PATHS[0])) {
86194-
await cache.saveCache(CACHE_PATHS, key);
86198+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
86199+
if (!fs.existsSync(CACHE_PATHS[0])) {
86200+
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
86201+
return;
8619586202
}
86203+
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
86204+
if (cacheId === -1) {
86205+
return;
86206+
}
86207+
core.info(`Cache saved with the key: ${primaryKey}`);
8619686208
}
8619786209

8619886210
module.exports = {
@@ -86232,12 +86244,16 @@ async function main() {
8623286244
core.addPath(path.join(pathToCLI, download.binPath));
8623386245

8623486246
// Get cache input
86235-
const cache = core.getInput("cache");
86247+
const cache = core.getBooleanInput("cache");
8623686248

86237-
if (cache) {
86238-
// Restore the RPC cache, if any.
86239-
await restoreRPCCache();
86249+
// If cache input is false, skip restoring cache
86250+
if (!cache) {
86251+
core.info("Cache not requested, not restoring cache");
86252+
return;
8624086253
}
86254+
86255+
// Restore the RPC cache
86256+
await restoreRPCCache();
8624186257
} catch (err) {
8624286258
core.setFailed(err);
8624386259
}

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/save/index.js

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84665,6 +84665,7 @@ function wrappy (fn, cb) {
8466584665
/***/ 2121:
8466684666
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
8466784667

84668+
const core = __nccwpck_require__(2186);
8466884669
const cache = __nccwpck_require__(7799);
8466984670
const github = __nccwpck_require__(5438);
8467084671
const fs = __nccwpck_require__(7147);
@@ -84676,16 +84677,27 @@ const PLATFORM = os.platform();
8467684677
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];
8467784678

8467884679
async function restoreRPCCache() {
84679-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
84680+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
8468084681
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
84681-
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
84682+
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
84683+
if (!cacheKey) {
84684+
core.info("Cache not found");
84685+
return;
84686+
}
84687+
core.info(`Cache restored from key: ${cacheKey}`);
8468284688
}
8468384689

8468484690
async function saveCache() {
84685-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
84686-
if (fs.existsSync(CACHE_PATHS[0])) {
84687-
await cache.saveCache(CACHE_PATHS, key);
84691+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
84692+
if (!fs.existsSync(CACHE_PATHS[0])) {
84693+
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
84694+
return;
84695+
}
84696+
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
84697+
if (cacheId === -1) {
84698+
return;
8468884699
}
84700+
core.info(`Cache saved with the key: ${primaryKey}`);
8468984701
}
8469084702

8469184703
module.exports = {
@@ -84694,24 +84706,6 @@ module.exports = {
8469484706
};
8469584707

8469684708

84697-
/***/ }),
84698-
84699-
/***/ 5209:
84700-
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
84701-
84702-
const { saveCache } = __nccwpck_require__(2121);
84703-
84704-
async function save() {
84705-
await saveCache();
84706-
}
84707-
84708-
module.exports = save;
84709-
84710-
if (require.main === require.cache[eval('__filename')]) {
84711-
save();
84712-
}
84713-
84714-
8471584709
/***/ }),
8471684710

8471784711
/***/ 2877:
@@ -85024,13 +85018,54 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
8502485018
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
8502585019
/******/
8502685020
/************************************************************************/
85027-
/******/
85028-
/******/ // startup
85029-
/******/ // Load entry module and return exports
85030-
/******/ // This entry module is referenced by other modules so it can't be inlined
85031-
/******/ var __webpack_exports__ = __nccwpck_require__(5209);
85032-
/******/ module.exports = __webpack_exports__;
85033-
/******/
85021+
var __webpack_exports__ = {};
85022+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
85023+
(() => {
85024+
const { saveCache } = __nccwpck_require__(2121);
85025+
const core = __nccwpck_require__(2186);
85026+
85027+
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
85028+
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
85029+
// throw an uncaught exception. Instead of failing this action, just warn.
85030+
process.on("uncaughtException", (e) => {
85031+
const warningPrefix = "[warning]";
85032+
core.info(`${warningPrefix}${e.message}`);
85033+
});
85034+
85035+
// Added early exit to resolve issue with slow post action step:
85036+
// - https://github.com/actions/setup-node/issues/878
85037+
// https://github.com/actions/cache/pull/1217
85038+
async function run(earlyExit) {
85039+
try {
85040+
const cacheInput = core.getBooleanInput("cache");
85041+
if (cacheInput) {
85042+
await saveCache();
85043+
} else {
85044+
core.info("Cache not requested, not saving cache");
85045+
}
85046+
85047+
if (earlyExit) {
85048+
process.exit(0);
85049+
}
85050+
} catch (error) {
85051+
let message = "Unknown error!";
85052+
if (error instanceof Error) {
85053+
message = error.message;
85054+
}
85055+
if (typeof error === "string") {
85056+
message = error;
85057+
}
85058+
core.warning(message);
85059+
}
85060+
}
85061+
85062+
if (require.main === require.cache[eval('__filename')]) {
85063+
run(true);
85064+
}
85065+
85066+
})();
85067+
85068+
module.exports = __webpack_exports__;
8503485069
/******/ })()
8503585070
;
8503685071
//# sourceMappingURL=index.js.map

dist/save/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const core = require("@actions/core");
12
const cache = require("@actions/cache");
23
const github = require("@actions/github");
34
const fs = require("fs");
@@ -9,16 +10,27 @@ const PLATFORM = os.platform();
910
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];
1011

1112
async function restoreRPCCache() {
12-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
13+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
1314
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
14-
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
15+
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
16+
if (!cacheKey) {
17+
core.info("Cache not found");
18+
return;
19+
}
20+
core.info(`Cache restored from key: ${cacheKey}`);
1521
}
1622

1723
async function saveCache() {
18-
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
19-
if (fs.existsSync(CACHE_PATHS[0])) {
20-
await cache.saveCache(CACHE_PATHS, key);
24+
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
25+
if (!fs.existsSync(CACHE_PATHS[0])) {
26+
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
27+
return;
28+
}
29+
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
30+
if (cacheId === -1) {
31+
return;
2132
}
33+
core.info(`Cache saved with the key: ${primaryKey}`);
2234
}
2335

2436
module.exports = {

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ async function main() {
2424
core.addPath(path.join(pathToCLI, download.binPath));
2525

2626
// Get cache input
27-
const cache = core.getInput("cache");
27+
const cache = core.getBooleanInput("cache");
2828

29-
if (cache) {
30-
// Restore the RPC cache, if any.
31-
await restoreRPCCache();
29+
// If cache input is false, skip restoring cache
30+
if (!cache) {
31+
core.info("Cache not requested, not restoring cache");
32+
return;
3233
}
34+
35+
// Restore the RPC cache
36+
await restoreRPCCache();
3337
} catch (err) {
3438
core.setFailed(err);
3539
}

src/save.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
const { saveCache } = require("./cache.js");
2+
const core = require("@actions/core");
23

3-
async function save() {
4-
await saveCache();
5-
}
4+
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
5+
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
6+
// throw an uncaught exception. Instead of failing this action, just warn.
7+
process.on("uncaughtException", (e) => {
8+
const warningPrefix = "[warning]";
9+
core.info(`${warningPrefix}${e.message}`);
10+
});
11+
12+
// Added early exit to resolve issue with slow post action step:
13+
// - https://github.com/actions/setup-node/issues/878
14+
// https://github.com/actions/cache/pull/1217
15+
async function run(earlyExit) {
16+
try {
17+
const cacheInput = core.getBooleanInput("cache");
18+
if (cacheInput) {
19+
await saveCache();
20+
} else {
21+
core.info("Cache not requested, not saving cache");
22+
}
623

7-
module.exports = save;
24+
if (earlyExit) {
25+
process.exit(0);
26+
}
27+
} catch (error) {
28+
let message = "Unknown error!";
29+
if (error instanceof Error) {
30+
message = error.message;
31+
}
32+
if (typeof error === "string") {
33+
message = error;
34+
}
35+
core.warning(message);
36+
}
37+
}
838

939
if (require.main === module) {
10-
save();
40+
run(true);
1141
}

0 commit comments

Comments
 (0)