Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cache handling and improve cache action logs #46

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when does this happen? should we add a log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it happens when there is an existing cache with the primary key thus fails saving the cache.
It doesn't throw an error and prints a message like this before returning with -1.

Failed to save: Unable to reserve cache with key linux-foundry-chain-fork-c2ec5babbad85fecbae9c860472722f98f36c164, another job may be creating this cache. More details: Cache already exists. Scope: refs/heads/master, Key: linux-foundry-chain-fork-c2ec5babbad85fecbae9c860472722f98f36c164, Version: d8cfedd31e23b815ad861b2e68523a31664eaf28c2ddc7b682eb115646a473d9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
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);
}
Loading