Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
restart on bad share exit, fix null ref in status command
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Hall committed Jan 11, 2017
1 parent f9372be commit 870913e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 5 additions & 4 deletions bin/storjshare-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ storjshare_status
sock.on('remote', function(rpc) {
rpc.status(function(err, shares) {
let table = new Table({
head: ['Node ID', 'Status', 'Uptime', 'Peers', '% Shared'],
head: ['Node ID', 'Status', 'Uptime', 'Restarts', 'Peers', '% Shared'],
style: {
head: ['cyan', 'bold'],
border: []
Expand All @@ -41,11 +41,12 @@ sock.on('remote', function(rpc) {
}

table.push([
share.id,
share.id || '?',
status,
prettyMs(share.meta.uptimeMs),
share.meta.farmerState.totalPeers,
share.meta.farmerState.percentUsed
share.meta.numRestarts || 0,
share.meta.farmerState.totalPeers || 0,
share.meta.farmerState.percentUsed || '...'
]);
});
console.log('\n' + table.toString());
Expand Down
10 changes: 10 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class RPC {
config: null,
meta: {
uptimeMs: 0,
numRestarts: 0,
farmerState: {}
},
process: null,
Expand Down Expand Up @@ -119,11 +120,19 @@ class RPC {
self._log(err.message, 'error');
clearInterval(uptimeCounter);
});

// NB: Listen for exits and restart the share if not stopped manually
share.process.on('exit', (code) => {
share.readyState = RPC.SHARE_STOPPED;
self._log(`share ${nodeId} exited with code ${code}`);
clearInterval(uptimeCounter);

if (code !== 0 && share.meta.numRestarts < RPC.MAX_AUTO_RESTARTS) {
share.meta.numRestarts++;
self.restart(nodeId, () => null);
}
});

share.process.on('message', (msg) => self._processShareIpc(share, msg));
self.shares.set(nodeId, share);
callback(null);
Expand Down Expand Up @@ -246,5 +255,6 @@ class RPC {
RPC.SHARE_STARTED = 1;
RPC.SHARE_STOPPED = 0;
RPC.SHARE_ERRORED = 2;
RPC.MAX_AUTO_RESTARTS = 30;

module.exports = RPC;

0 comments on commit 870913e

Please sign in to comment.