@@ -21,7 +21,6 @@ class RPC {
21
21
constructor ( options = { } ) {
22
22
this . logger = new JsonLogger ( options . logVerbosity ) ;
23
23
this . shares = new Map ( ) ;
24
- this . _restarts = { } ;
25
24
}
26
25
27
26
/**
@@ -44,48 +43,65 @@ class RPC {
44
43
}
45
44
46
45
/**
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
51
48
*/
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 ;
67
51
68
52
try {
69
53
statSync ( configPath ) ;
70
54
} catch ( err ) {
71
- return callback ( new Error ( `failed to read config at ${ configPath } ` ) ) ;
55
+ throw new Error ( `failed to read config at ${ configPath } ` ) ;
72
56
}
73
57
74
58
try {
75
- share . config = JSON . parse ( stripJsonComments (
59
+ config = JSON . parse ( stripJsonComments (
76
60
readFileSync ( configPath ) . toString ( )
77
61
) ) ;
78
62
} catch ( err ) {
79
- return callback ( new Error ( `failed to parse config at ${ configPath } ` ) ) ;
63
+ throw new Error ( `failed to parse config at ${ configPath } ` ) ;
80
64
}
81
65
82
66
try {
83
- utils . validate ( share . config ) ;
67
+ utils . validate ( config ) ;
84
68
} catch ( err ) {
85
- return callback ( new Error ( err . message . toLowerCase ( ) ) ) ;
69
+ throw new Error ( err . message . toLowerCase ( ) ) ;
86
70
}
87
71
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 } ` ) ;
89
105
90
106
if ( self . shares . has ( nodeId ) && self . shares . get ( nodeId ) . readyState === 1 ) {
91
107
return callback ( new Error ( `share ${ nodeId } is already running` ) ) ;
@@ -96,6 +112,7 @@ class RPC {
96
112
return callback ( new Error ( err . message . toLowerCase ( ) ) ) ;
97
113
}
98
114
115
+ share . meta . uptimeMs = 0 ;
99
116
/* istanbul ignore next */
100
117
let uptimeCounter = setInterval ( ( ) => share . meta . uptimeMs += 1000 , 1000 ) ;
101
118
@@ -126,15 +143,14 @@ class RPC {
126
143
127
144
// NB: Listen for exits and restart the share if not stopped manually
128
145
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 ;
131
147
share . readyState = RPC . SHARE_STOPPED ;
132
148
133
149
self . _log ( `share ${ nodeId } exited with code ${ code } ` ) ;
134
150
clearInterval ( uptimeCounter ) ;
135
151
136
152
if ( signal !== 'SIGINT' && ! maxRestartsReached ) {
137
- self . _restarts [ configPath ] ++ ;
153
+ share . meta . numRestarts ++ ;
138
154
self . restart ( nodeId , ( ) => null ) ;
139
155
}
140
156
} ) ;
@@ -199,12 +215,10 @@ class RPC {
199
215
* @param {RPC~statusCallback }
200
216
*/
201
217
status ( callback ) {
202
- const self = this ;
203
218
const statuses = [ ] ;
204
219
205
220
this . _log ( `got status query` ) ;
206
221
this . shares . forEach ( ( share , nodeId ) => {
207
- share . meta . numRestarts = self . _restarts [ share . path ] ;
208
222
statuses . push ( {
209
223
id : nodeId ,
210
224
config : share . config ,
@@ -248,7 +262,6 @@ class RPC {
248
262
}
249
263
250
264
let share = this . shares . get ( nodeId ) ;
251
- this . _restarts [ share . path ] = 0 ;
252
265
253
266
share . process . kill ( 'SIGINT' ) ;
254
267
this . shares . delete ( nodeId ) ;
0 commit comments