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

Commit 769ca96

Browse files
author
Gordon Hall
committed
lint the bin/ dir, add --status to storjshare-daemon, increase timeout after daemon start before cmd exec
1 parent 50e4886 commit 769ca96

8 files changed

+144
-71
lines changed

.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"laxbreak": true,
23
"bitwise": false,
34
"browser": true,
45
"camelcase": false,

bin/storjshare-daemon.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ const daemonize = require('daemon');
66
const dnode = require('dnode');
77
const config = require('../lib/config/daemon');
88
const RPC = require('../lib/api');
9+
const utils = require('../lib/utils');
910
const api = new RPC({ logVerbosity: config.daemonLogVerbosity });
1011
const {createWriteStream} = require('fs');
1112
const logFile = createWriteStream(config.daemonLogFilePath, { flags: 'a' });
1213
const storjshare_daemon = require('commander');
1314

1415
storjshare_daemon
16+
.option('--status', 'print the status of the daemon and exit')
1517
.option('-F, --foreground', 'keeps the process in the foreground')
1618
.parse(process.argv);
1719

18-
if (!storjshare_daemon.foreground) {
20+
function startDaemonRpcServer() {
21+
dnode(api.methods)
22+
.on('error', (err) => api.logger.warn(err.message))
23+
.listen(config.daemonRpcPort, config.daemonRpcAddress);
24+
}
25+
26+
function checkDaemonRpcStatus() {
27+
utils.checkDaemonRpcStatus(config.daemonRpcPort, (isRunning) => {
28+
console.info(`\n * daemon ${isRunning ? 'is' : 'is not'} running`);
29+
process.exitCode = isRunning ? 0 : 3;
30+
});
31+
}
32+
33+
if (storjshare_daemon.status) {
34+
checkDaemonRpcStatus();
35+
} else if (!storjshare_daemon.foreground) {
1936
daemonize();
2037
api.logger.pipe(logFile);
38+
startDaemonRpcServer();
2139
} else {
2240
api.logger.pipe(process.stdout);
41+
startDaemonRpcServer();
2342
}
2443

25-
dnode(api.methods)
26-
.on('error', (err) => api.logger.warn(err.message))
27-
.listen(config.daemonRpcPort, config.daemonRpcAddress);
44+

bin/storjshare-logs.js

+49-50
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,52 @@ if (!storjshare_logs.nodeid) {
2121
process.exit(1);
2222
}
2323

24+
function getLastLines(filename, lines, callback) {
25+
let chunk = '';
26+
let size = Math.max(0, fs.statSync(filename).size - (lines * 200));
27+
28+
fs.createReadStream(filename, { start: size })
29+
.on('data', function(data) {
30+
chunk += data.toString();
31+
})
32+
.on('end', function() {
33+
chunk = chunk.split('\n').slice(-(lines + 1));
34+
chunk.pop();
35+
callback(chunk);
36+
});
37+
}
38+
39+
function prettyLog(line) {
40+
var output = ' ';
41+
42+
try {
43+
line = JSON.parse(line);
44+
} catch (err) {
45+
return;
46+
}
47+
48+
switch (line.level) {
49+
case 'debug':
50+
output += colors.magenta('[ debug ] ');
51+
break;
52+
case 'info':
53+
output += colors.cyan('[ info ] ');
54+
break;
55+
case 'warn':
56+
output += colors.yellow('[ warn ] ');
57+
break;
58+
case 'error':
59+
output += colors.red('[ error ] ');
60+
break;
61+
default:
62+
// noop
63+
}
64+
65+
output += colors.gray( `[ ${line.timestamp} ]\n [ `);
66+
output += `${line.message}`;
67+
console.log(output);
68+
}
69+
2470
const sock = dnode.connect(config.daemonRpcPort);
2571

2672
process.on('exit', () => {
@@ -50,56 +96,9 @@ sock.on('remote', function(rpc) {
5096
}
5197

5298
let logTail = new Tail(logFilePath);
53-
54-
function getLastLines(filename, lines, callback) {
55-
let chunk = '';
56-
let size = Math.max(0, fs.statSync(filename).size - (lines * 200));
57-
58-
fs.createReadStream(filename, { start: size })
59-
.on('data', function(data) {
60-
chunk += data.toString();
61-
})
62-
.on('end', function() {
63-
chunk = chunk.split('\n').slice(-(lines + 1));
64-
chunk.pop();
65-
callback(chunk);
66-
});
67-
}
68-
69-
function prettyLog(line) {
70-
var output = ' ';
71-
72-
try {
73-
line = JSON.parse(line);
74-
} catch (err) {
75-
return;
76-
}
77-
78-
switch (line.level) {
79-
case 'debug':
80-
output += colors.magenta('[ debug ] ');
81-
break;
82-
case 'info':
83-
output += colors.cyan('[ info ] ');
84-
break;
85-
case 'warn':
86-
output += colors.yellow('[ warn ] ');
87-
break;
88-
case 'error':
89-
output += colors.red('[ error ] ');
90-
break;
91-
default:
92-
// noop
93-
}
94-
95-
output += colors.gray( `[ ${line.timestamp} ]\n [ `)
96-
output += `${line.message}`;
97-
console.log(output);
98-
}
99-
100-
let numLines = storjshare_logs.lines ?
101-
parseInt(storjshare_logs.lines) :
102-
20;
99+
let numLines = storjshare_logs.lines
100+
? parseInt(storjshare_logs.lines)
101+
: 20;
103102

104103
getLastLines(logFilePath, numLines, (lines) => {
105104
lines.forEach((line) => prettyLog(line));

bin/storjshare-start.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
'use strict';
44

5-
const storj = require('storj-lib');
65
const dnode = require('dnode');
76
const path = require('path');
87
const config = require('../lib/config/daemon');

bin/storjshare.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
'use strict';
44

5-
const net = require('net');
65
const config = require('../lib/config/daemon');
76
const storjshare = require('commander');
87
const {fork} = require('child_process');
98
const path = require('path');
9+
const utils = require('../lib/utils');
1010
const {version} = require('../package');
1111
const {software: core, protocol} = require('storj-lib').version;
1212

13+
const TIME_WAIT_IF_STARTED = 1000;
14+
const TIME_WAIT_AFTER_START = 6000;
15+
1316
storjshare
1417
.version(`\n * daemon: ${version}, core: ${core}, protocol: ${protocol}`)
1518
.command('start', 'start a farming node')
@@ -25,17 +28,14 @@ storjshare
2528
.command('daemon', 'starts the daemon');
2629

2730
if (!['daemon'].includes(process.argv[2])) {
28-
const sock = net.connect(config.daemonRpcPort);
29-
30-
sock.once('error', function() {
31-
console.info('\n * daemon is not running, starting...');
32-
fork(path.join(__dirname, 'storjshare-daemon.js'), []);
33-
setTimeout(() => storjshare.parse(process.argv), 4000);
34-
});
35-
36-
sock.once('connect', function() {
37-
sock.end();
38-
setTimeout(() => storjshare.parse(process.argv), 1000);
31+
utils.checkDaemonRpcStatus(config.daemonRpcPort, (isRunning) => {
32+
if (isRunning) {
33+
setTimeout(() => storjshare.parse(process.argv), TIME_WAIT_IF_STARTED);
34+
} else {
35+
console.info('\n * daemon is not running, starting...');
36+
fork(path.join(__dirname, 'storjshare-daemon.js'), []);
37+
setTimeout(() => storjshare.parse(process.argv), TIME_WAIT_AFTER_START);
38+
}
3939
});
4040
} else {
4141
storjshare.parse(process.argv);

lib/utils.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
'use strict';
66

7+
const net = require('net');
78
const fs = require('fs');
89
const storj = require('storj-lib');
910
const {bitcore} = storj.deps;
@@ -34,7 +35,7 @@ exports._isValidDirectory = function(directory) {
3435
* @param {String} size
3536
*/
3637
exports._isValidSize = function(size) {
37-
return Number(size) > 0 && typeof size !== 'undefined';
38+
return Number(size) >= 0 && typeof size !== 'undefined';
3839
};
3940

4041
/**
@@ -145,3 +146,19 @@ exports.getFreeSpace = function(path, callback) {
145146
});
146147
};
147148

149+
/**
150+
* Checks the status of the daemon RPC server
151+
* @param {Number} port
152+
*/
153+
exports.checkDaemonRpcStatus = function(port, callback) {
154+
const sock = net.connect(port);
155+
156+
sock.once('error', function() {
157+
callback(false);
158+
});
159+
160+
sock.once('connect', () => {
161+
sock.end();
162+
callback(true);
163+
});
164+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test": "npm run testsuite && npm run linter",
2626
"testsuite": "STORJ_ALLOW_LOOPBACK=1 ./node_modules/.bin/mocha test/** --recursive",
2727
"coverage": "STORJ_ALLOW_LOOPBACK=1 ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive",
28-
"linter": "./node_modules/.bin/jshint --config .jshintrc ./index.js ./lib ./test"
28+
"linter": "./node_modules/.bin/jshint --config .jshintrc ./index.js ./lib ./test ./bin"
2929
},
3030
"preferGlobal": true,
3131
"repository": {

test/utils.unit.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const utils = require('../lib/utils');
44
const proxyquire = require('proxyquire');
55
const sinon = require('sinon');
66
const {expect} = require('chai');
7+
const {EventEmitter} = require('events');
78

89
describe('module:utils', function() {
910

@@ -51,8 +52,8 @@ describe('module:utils', function() {
5152
expect(utils._isValidSize(1)).to.equal(true);
5253
});
5354

54-
it('should return false for 0 or less', function() {
55-
expect(utils._isValidSize(0)).to.equal(false);
55+
it('should return false for less than 0', function() {
56+
expect(utils._isValidSize(0)).to.equal(true);
5657
expect(utils._isValidSize(-1)).to.equal(false);
5758
});
5859

@@ -182,4 +183,43 @@ describe('module:utils', function() {
182183

183184
});
184185

186+
describe('#checkDaemonRpcStatus', function() {
187+
188+
it('should callback true if connect', function(done) {
189+
let sock = new EventEmitter();
190+
sock.end = sinon.stub();
191+
let _utils = proxyquire('../lib/utils', {
192+
net: {
193+
connect: () => {
194+
setTimeout(() => sock.emit('connect'), 50);
195+
return sock;
196+
}
197+
}
198+
});
199+
_utils.checkDaemonRpcStatus(45015, (isRunning) => {
200+
expect(isRunning).to.equal(true);
201+
done();
202+
});
203+
});
204+
205+
it('should callback false if error', function(done) {
206+
let sock = new EventEmitter();
207+
sock.end = sinon.stub();
208+
let _utils = proxyquire('../lib/utils', {
209+
net: {
210+
connect: () => {
211+
setTimeout(() => sock.emit('error',
212+
new Error('ECONNREFUSED')), 50);
213+
return sock;
214+
}
215+
}
216+
});
217+
_utils.checkDaemonRpcStatus(45015, (isRunning) => {
218+
expect(isRunning).to.equal(false);
219+
done();
220+
});
221+
});
222+
223+
});
224+
185225
});

0 commit comments

Comments
 (0)