Skip to content

Commit

Permalink
refactor: cache handling and improve cache action logs (#46)
Browse files Browse the repository at this point in the history
* fix: use getBooleanInput and add logging

* chore: update dist build
  • Loading branch information
penandlim authored Feb 19, 2024
1 parent 66af43d commit 362aa1b
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 56 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ runs:
using: "node20"
main: "dist/index.js"
post: "dist/save/index.js"
post-if: "github.event.inputs.cache && success()"
post-if: success()
34 changes: 25 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86172,6 +86172,7 @@ function wrappy (fn, cb) {
/***/ 2121:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

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

async function restoreRPCCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
return;
}
core.info(`Cache restored from key: ${cacheKey}`);
}

async function saveCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (fs.existsSync(CACHE_PATHS[0])) {
await cache.saveCache(CACHE_PATHS, key);
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (!fs.existsSync(CACHE_PATHS[0])) {
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
return;
}
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
if (cacheId === -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
}

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

// Get cache input
const cache = core.getInput("cache");
const cache = core.getBooleanInput("cache");

if (cache) {
// Restore the RPC cache, if any.
await restoreRPCCache();
// If cache input is false, skip restoring cache
if (!cache) {
core.info("Cache not requested, not restoring cache");
return;
}

// Restore the RPC cache
await restoreRPCCache();
} catch (err) {
core.setFailed(err);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

95 changes: 65 additions & 30 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84665,6 +84665,7 @@ function wrappy (fn, cb) {
/***/ 2121:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

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

async function restoreRPCCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
return;
}
core.info(`Cache restored from key: ${cacheKey}`);
}

async function saveCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (fs.existsSync(CACHE_PATHS[0])) {
await cache.saveCache(CACHE_PATHS, key);
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (!fs.existsSync(CACHE_PATHS[0])) {
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
return;
}
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
if (cacheId === -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
}

module.exports = {
Expand All @@ -84694,24 +84706,6 @@ module.exports = {
};


/***/ }),

/***/ 5209:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const { saveCache } = __nccwpck_require__(2121);

async function save() {
await saveCache();
}

module.exports = save;

if (require.main === require.cache[eval('__filename')]) {
save();
}


/***/ }),

/***/ 2877:
Expand Down Expand Up @@ -85024,13 +85018,54 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(5209);
/******/ module.exports = __webpack_exports__;
/******/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const { saveCache } = __nccwpck_require__(2121);
const core = __nccwpck_require__(2186);

// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on("uncaughtException", (e) => {
const warningPrefix = "[warning]";
core.info(`${warningPrefix}${e.message}`);
});

// Added early exit to resolve issue with slow post action step:
// - https://github.com/actions/setup-node/issues/878
// https://github.com/actions/cache/pull/1217
async function run(earlyExit) {
try {
const cacheInput = core.getBooleanInput("cache");
if (cacheInput) {
await saveCache();
} else {
core.info("Cache not requested, not saving cache");
}

if (earlyExit) {
process.exit(0);
}
} catch (error) {
let message = "Unknown error!";
if (error instanceof Error) {
message = error.message;
}
if (typeof error === "string") {
message = error;
}
core.warning(message);
}
}

if (require.main === require.cache[eval('__filename')]) {
run(true);
}

})();

module.exports = __webpack_exports__;
/******/ })()
;
//# sourceMappingURL=index.js.map
2 changes: 1 addition & 1 deletion dist/save/index.js.map

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const core = require("@actions/core");
const cache = require("@actions/cache");
const github = require("@actions/github");
const fs = require("fs");
Expand All @@ -9,16 +10,27 @@ const PLATFORM = os.platform();
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];

async function restoreRPCCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
const restoreKeys = [PLATFORM + "-foundry-chain-fork-"];
await cache.restoreCache(CACHE_PATHS, key, restoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
return;
}
core.info(`Cache restored from key: ${cacheKey}`);
}

async function saveCache() {
const key = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (fs.existsSync(CACHE_PATHS[0])) {
await cache.saveCache(CACHE_PATHS, key);
const primaryKey = PLATFORM + "-foundry-chain-fork-" + github.context.sha;
if (!fs.existsSync(CACHE_PATHS[0])) {
core.info(`Cache path does not exist, not saving cache : ${CACHE_PATHS[0]}`);
return;
}
const cacheId = await cache.saveCache(CACHE_PATHS, primaryKey);
if (cacheId === -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
}

module.exports = {
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ async function main() {
core.addPath(path.join(pathToCLI, download.binPath));

// Get cache input
const cache = core.getInput("cache");
const cache = core.getBooleanInput("cache");

if (cache) {
// Restore the RPC cache, if any.
await restoreRPCCache();
// If cache input is false, skip restoring cache
if (!cache) {
core.info("Cache not requested, not restoring cache");
return;
}

// Restore the RPC cache
await restoreRPCCache();
} catch (err) {
core.setFailed(err);
}
Expand Down
40 changes: 35 additions & 5 deletions src/save.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
const { saveCache } = require("./cache.js");
const core = require("@actions/core");

async function save() {
await saveCache();
}
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on("uncaughtException", (e) => {
const warningPrefix = "[warning]";
core.info(`${warningPrefix}${e.message}`);
});

// Added early exit to resolve issue with slow post action step:
// - https://github.com/actions/setup-node/issues/878
// https://github.com/actions/cache/pull/1217
async function run(earlyExit) {
try {
const cacheInput = core.getBooleanInput("cache");
if (cacheInput) {
await saveCache();
} else {
core.info("Cache not requested, not saving cache");
}

module.exports = save;
if (earlyExit) {
process.exit(0);
}
} catch (error) {
let message = "Unknown error!";
if (error instanceof Error) {
message = error.message;
}
if (typeof error === "string") {
message = error;
}
core.warning(message);
}
}

if (require.main === module) {
save();
run(true);
}

0 comments on commit 362aa1b

Please sign in to comment.