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

Commit

Permalink
Merge pull request #14 from bookchin/master
Browse files Browse the repository at this point in the history
improve restart tracking
  • Loading branch information
bookchin committed Jan 13, 2017
2 parents fd9c8c8 + d0cbece commit faedea7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
77 changes: 45 additions & 32 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class RPC {
constructor(options={}) {
this.logger = new JsonLogger(options.logVerbosity);
this.shares = new Map();
this._restarts = {};
}

/**
Expand All @@ -44,48 +43,65 @@ class RPC {
}

/**
* Starts a share process with the given configuration
* @param {String} configPath
* @param {RPC~startCallback}
* @see https://storj.github.io/core/FarmerInterface.html
* Reads the config file and returns the parsed version
* @private
*/
start(configPath, callback) {
const self = this;
self._restarts[configPath] = self._restarts[configPath] || 0;
let share = {
config: null,
meta: {
uptimeMs: 0,
farmerState: {}
},
process: null,
readyState: 0,
path: configPath
};

this._log(`attempting to start share with config at path ${configPath}`);
_readConfig(configPath) {
let config = null;

try {
statSync(configPath);
} catch (err) {
return callback(new Error(`failed to read config at ${configPath}`));
throw new Error(`failed to read config at ${configPath}`);
}

try {
share.config = JSON.parse(stripJsonComments(
config = JSON.parse(stripJsonComments(
readFileSync(configPath).toString()
));
} catch (err) {
return callback(new Error(`failed to parse config at ${configPath}`));
throw new Error(`failed to parse config at ${configPath}`);
}

try {
utils.validate(share.config);
utils.validate(config);
} catch (err) {
return callback(new Error(err.message.toLowerCase()));
throw new Error(err.message.toLowerCase());
}

let nodeId = storj.KeyPair(share.config.networkPrivateKey).getNodeID();
return config;
}

/**
* Starts a share process with the given configuration
* @param {String} configPath
* @param {RPC~startCallback}
* @see https://storj.github.io/core/FarmerInterface.html
*/
start(configPath, callback) {
let config = null;

try {
config = this._readConfig(configPath);
} catch (err) {
return callback(err);
}

const self = this;
const nodeId = storj.KeyPair(config.networkPrivateKey).getNodeID();
const share = this.shares.get(nodeId) || {
config: config,
meta: {
uptimeMs: 0,
farmerState: {},
numRestarts: 0
},
process: null,
readyState: 0,
path: configPath
};

this._log(`attempting to start share with config at path ${configPath}`);

if (self.shares.has(nodeId) && self.shares.get(nodeId).readyState === 1) {
return callback(new Error(`share ${nodeId} is already running`));
Expand All @@ -96,6 +112,7 @@ class RPC {
return callback(new Error(err.message.toLowerCase()));
}

share.meta.uptimeMs = 0;
/* istanbul ignore next */
let uptimeCounter = setInterval(() => share.meta.uptimeMs += 1000, 1000);

Expand Down Expand Up @@ -126,15 +143,14 @@ class RPC {

// NB: Listen for exits and restart the share if not stopped manually
share.process.on('exit', (code, signal) => {
let totalRestarts = self._restarts[configPath];
let maxRestartsReached = totalRestarts >= RPC.MAX_RESTARTS;
let maxRestartsReached = share.meta.numRestarts >= RPC.MAX_RESTARTS;
share.readyState = RPC.SHARE_STOPPED;

self._log(`share ${nodeId} exited with code ${code}`);
clearInterval(uptimeCounter);

if (signal !== 'SIGINT' && !maxRestartsReached) {
self._restarts[configPath]++;
share.meta.numRestarts++;
self.restart(nodeId, () => null);
}
});
Expand Down Expand Up @@ -199,12 +215,10 @@ class RPC {
* @param {RPC~statusCallback}
*/
status(callback) {
const self = this;
const statuses = [];

this._log(`got status query`);
this.shares.forEach((share, nodeId) => {
share.meta.numRestarts = self._restarts[share.path];
statuses.push({
id: nodeId,
config: share.config,
Expand Down Expand Up @@ -248,7 +262,6 @@ class RPC {
}

let share = this.shares.get(nodeId);
this._restarts[share.path] = 0;

share.process.kill('SIGINT');
this.shares.delete(nodeId);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storjshare-daemon",
"version": "2.0.3",
"version": "2.0.4",
"description": "daemon + process manager for sharing space on the storj network",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit faedea7

Please sign in to comment.