Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Retry npm install in CI (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and alexander-fenster committed Sep 4, 2018
1 parent 58a9337 commit 2d7ff53
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Expand Up @@ -64,7 +64,7 @@ jobs:
name: Install and link the module
command: |-
mkdir -p /home/node/.npm-global
npm install
./.circleci/npm-install-retry.js
environment:
NPM_CONFIG_PREFIX: /home/node/.npm-global
- run: npm test
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:
command: |
cd samples/
npm link ../
npm install
./../.circleci/npm-install-retry.js
environment:
NPM_CONFIG_PREFIX: /home/node/.npm-global
- run:
Expand Down Expand Up @@ -172,6 +172,6 @@ jobs:
user: node
steps:
- checkout
- run: npm install
- run: ./.circleci/npm-install-retry.js
- run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
- run: npm publish --access=public
60 changes: 60 additions & 0 deletions .circleci/npm-install-retry.js
@@ -0,0 +1,60 @@
#!/usr/bin/env node

let spawn = require('child_process').spawn;

//
//USE: ./index.js <ms npm can be idle> <number of attempts> [... NPM ARGS]
//

let timeout = process.argv[2] || 60000;
let attempts = process.argv[3] || 3;
let args = process.argv.slice(4);
if (args.length === 0) {
args = ['install'];
}

(function npm() {
let timer;
args.push('--verbose');
let proc = spawn('npm', args);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.stdin.end();
proc.stdout.on('data', () => {
setTimer();
});
proc.stderr.on('data', () => {
setTimer();
});

// side effect: this also restarts when npm exits with a bad code even if it
// didnt timeout
proc.on('close', (code, signal) => {
clearTimeout(timer);
if (code || signal) {
console.log('[npm-are-you-sleeping] npm exited with code ' + code + '');

if (--attempts) {
console.log('[npm-are-you-sleeping] restarting');
npm();
} else {
console.log('[npm-are-you-sleeping] i tried lots of times. giving up.');
throw new Error("npm install fails");
}
}
});

function setTimer() {
clearTimeout(timer);
timer = setTimeout(() => {
console.log('[npm-are-you-sleeping] killing npm with SIGTERM');
proc.kill('SIGTERM');
// wait a couple seconds
timer = setTimeout(() => {
// its it's still not closed sigkill
console.log('[npm-are-you-sleeping] killing npm with SIGKILL');
proc.kill('SIGKILL');
}, 2000);
}, timeout);
}
})();

0 comments on commit 2d7ff53

Please sign in to comment.