From 870913ed7d0d838b55ef28cd566fe381b2ed1f70 Mon Sep 17 00:00:00 2001 From: Gordon Hall Date: Wed, 11 Jan 2017 11:43:38 -0500 Subject: [PATCH] restart on bad share exit, fix null ref in status command --- bin/storjshare-status.js | 9 +++++---- lib/api.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bin/storjshare-status.js b/bin/storjshare-status.js index 5aa3ecd..7aa2dd0 100755 --- a/bin/storjshare-status.js +++ b/bin/storjshare-status.js @@ -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: [] @@ -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()); diff --git a/lib/api.js b/lib/api.js index 023e670..3e10911 100644 --- a/lib/api.js +++ b/lib/api.js @@ -53,6 +53,7 @@ class RPC { config: null, meta: { uptimeMs: 0, + numRestarts: 0, farmerState: {} }, process: null, @@ -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); @@ -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;