Skip to content

Commit

Permalink
Add timeout to warm gittar cache step (#362)
Browse files Browse the repository at this point in the history
* feat: add gittar fetch in time limit helper function

* feat: add gittar timeout promise

* feat: log when an npm package is going to be added to the cache

* feat: log when fetching project template

* feat: add gittar fetch and gittar timeout promise to to promise.race

* feat: clear timeout

* feat: add timeout to fetch project template step

* feat: log when warm cache step is skipped after timeout

* refactor: remove unused code

* feat: increase timeout limit to 15s

* refactor: make timeout function general

* feat: add timeout to add npm package to cache step

* refactor: make timeout function general purpose

* feat: remove npm cache timeout

* chore: bump version to 0.6.4
  • Loading branch information
ymekuria authored Feb 14, 2023
1 parent 9c21080 commit 003f000
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkapp-cli",
"version": "0.6.3",
"version": "0.6.4",
"description": "CLI to create a zkApp (\"zero-knowledge app\") for Mina Protocol.",
"keywords": [
"cli",
Expand Down
31 changes: 30 additions & 1 deletion src/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function warmNpmCache() {

try {
for await (const pkgWithVersion of toCache) {
console.log(` Adding ${pkgWithVersion} to the cache.`);
await shExec(`npm cache add ${pkgWithVersion}`);
}
console.log(' Done.');
Expand All @@ -55,9 +56,37 @@ async function warmGittarCache() {
console.log(' Warm cache for project template.');

try {
const fetchMaxTimeLimit = 15000;
const src = 'github:o1-labs/zkapp-cli#main';
await gittar.fetch(src, { force: true });
console.log(' Fetching project template.');
// Skips "Warm cache for project template" step if operation takes
// longer than the fetch max time limit.
const response = await executeInTimeLimit(
gittar.fetch(src, { force: true }),
fetchMaxTimeLimit
);

if (response === null) {
console.log(' Skip warm cache for project template');
return;
}
} catch (err) {
console.error(err);
}
}

async function executeInTimeLimit(operation, maxTimeLimit) {
let timeout;

const timeoutPromise = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
resolve(null);
}, maxTimeLimit);
});

const response = await Promise.race([operation, timeoutPromise]);

if (timeout) clearTimeout(timeout);

return response;
}

0 comments on commit 003f000

Please sign in to comment.