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

Commit d0cbece

Browse files
author
Gordon Hall
committed
improve restart tracking
1 parent adf3446 commit d0cbece

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

lib/api.js

+45-32
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class RPC {
2121
constructor(options={}) {
2222
this.logger = new JsonLogger(options.logVerbosity);
2323
this.shares = new Map();
24-
this._restarts = {};
2524
}
2625

2726
/**
@@ -44,48 +43,65 @@ class RPC {
4443
}
4544

4645
/**
47-
* Starts a share process with the given configuration
48-
* @param {String} configPath
49-
* @param {RPC~startCallback}
50-
* @see https://storj.github.io/core/FarmerInterface.html
46+
* Reads the config file and returns the parsed version
47+
* @private
5148
*/
52-
start(configPath, callback) {
53-
const self = this;
54-
self._restarts[configPath] = self._restarts[configPath] || 0;
55-
let share = {
56-
config: null,
57-
meta: {
58-
uptimeMs: 0,
59-
farmerState: {}
60-
},
61-
process: null,
62-
readyState: 0,
63-
path: configPath
64-
};
65-
66-
this._log(`attempting to start share with config at path ${configPath}`);
49+
_readConfig(configPath) {
50+
let config = null;
6751

6852
try {
6953
statSync(configPath);
7054
} catch (err) {
71-
return callback(new Error(`failed to read config at ${configPath}`));
55+
throw new Error(`failed to read config at ${configPath}`);
7256
}
7357

7458
try {
75-
share.config = JSON.parse(stripJsonComments(
59+
config = JSON.parse(stripJsonComments(
7660
readFileSync(configPath).toString()
7761
));
7862
} catch (err) {
79-
return callback(new Error(`failed to parse config at ${configPath}`));
63+
throw new Error(`failed to parse config at ${configPath}`);
8064
}
8165

8266
try {
83-
utils.validate(share.config);
67+
utils.validate(config);
8468
} catch (err) {
85-
return callback(new Error(err.message.toLowerCase()));
69+
throw new Error(err.message.toLowerCase());
8670
}
8771

88-
let nodeId = storj.KeyPair(share.config.networkPrivateKey).getNodeID();
72+
return config;
73+
}
74+
75+
/**
76+
* Starts a share process with the given configuration
77+
* @param {String} configPath
78+
* @param {RPC~startCallback}
79+
* @see https://storj.github.io/core/FarmerInterface.html
80+
*/
81+
start(configPath, callback) {
82+
let config = null;
83+
84+
try {
85+
config = this._readConfig(configPath);
86+
} catch (err) {
87+
return callback(err);
88+
}
89+
90+
const self = this;
91+
const nodeId = storj.KeyPair(config.networkPrivateKey).getNodeID();
92+
const share = this.shares.get(nodeId) || {
93+
config: config,
94+
meta: {
95+
uptimeMs: 0,
96+
farmerState: {},
97+
numRestarts: 0
98+
},
99+
process: null,
100+
readyState: 0,
101+
path: configPath
102+
};
103+
104+
this._log(`attempting to start share with config at path ${configPath}`);
89105

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

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

@@ -126,15 +143,14 @@ class RPC {
126143

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

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

136152
if (signal !== 'SIGINT' && !maxRestartsReached) {
137-
self._restarts[configPath]++;
153+
share.meta.numRestarts++;
138154
self.restart(nodeId, () => null);
139155
}
140156
});
@@ -199,12 +215,10 @@ class RPC {
199215
* @param {RPC~statusCallback}
200216
*/
201217
status(callback) {
202-
const self = this;
203218
const statuses = [];
204219

205220
this._log(`got status query`);
206221
this.shares.forEach((share, nodeId) => {
207-
share.meta.numRestarts = self._restarts[share.path];
208222
statuses.push({
209223
id: nodeId,
210224
config: share.config,
@@ -248,7 +262,6 @@ class RPC {
248262
}
249263

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

253266
share.process.kill('SIGINT');
254267
this.shares.delete(nodeId);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "storjshare-daemon",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "daemon + process manager for sharing space on the storj network",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)