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 #69 from bookchin/master
Browse files Browse the repository at this point in the history
v2.4.0
  • Loading branch information
bookchin authored Feb 9, 2017
2 parents b2bbb46 + 48101ec commit 20d5c9d
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 93 deletions.
34 changes: 17 additions & 17 deletions bin/storjshare-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ function startDaemonRpcServer() {
.listen(config.daemonRpcPort, config.daemonRpcAddress);
}

function checkDaemonRpcStatus() {
utils.checkDaemonRpcStatus(config.daemonRpcPort, (isRunning) => {
utils.checkDaemonRpcStatus(config.daemonRpcPort, (isRunning) => {
if (storjshare_daemon.status) {
console.info(`\n * daemon ${isRunning ? 'is' : 'is not'} running`);
process.exitCode = isRunning ? 0 : 3;
});
}

if (storjshare_daemon.status) {
checkDaemonRpcStatus();
} else if (!storjshare_daemon.foreground) {
daemonize();
api.logger.pipe(logFile);
startDaemonRpcServer();
} else {
api.logger.pipe(process.stdout);
startDaemonRpcServer();
}


} else if (isRunning) {
return console.info('\n * daemon is already running');
} else {
if (storjshare_daemon.foreground) {
console.info('\n * starting daemon in foreground\n');
api.logger.pipe(process.stdout);
startDaemonRpcServer();
} else {
console.info('\n * starting daemon in background');
daemonize();
api.logger.pipe(logFile);
startDaemonRpcServer();
}
}
});
6 changes: 2 additions & 4 deletions bin/storjshare-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_destroy = require('commander');

storjshare_destroy
Expand All @@ -16,9 +16,7 @@ if (!storjshare_destroy.nodeid) {
process.exit(1);
}

const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.destroy(storjshare_destroy.nodeid, (err) => {
if (err) {
console.error(`\n cannot destroy node, reason: ${err.message}`);
Expand Down
11 changes: 3 additions & 8 deletions bin/storjshare-killall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
'use strict';

const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_killall = require('commander');

storjshare_killall
.description('destroys all running shares and stop daemon')
.parse(process.argv);

const sock = dnode.connect(config.daemonRpcPort);

sock.on('end', function() {
console.info('\n * daemon has stopped');
});

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
sock.on('end', () => console.info('\n * daemon has stopped'));
rpc.killall(() => sock.end());
});
6 changes: 2 additions & 4 deletions bin/storjshare-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const os = require('os');
const path = require('path');
const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_load = require('commander');

storjshare_load
Expand All @@ -25,9 +25,7 @@ if (!path.isAbsolute(storjshare_load.snapshot)) {
storjshare_load.snapshot);
}

const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.load(storjshare_load.snapshot, (err) => {
if (err) {
console.error(`\n cannot load snapshot, reason: ${err.message}`);
Expand Down
13 changes: 5 additions & 8 deletions bin/storjshare-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const {Tail} = require('tail');
const colors = require('colors/safe');
Expand Down Expand Up @@ -67,14 +66,12 @@ function prettyLog(line) {
console.log(output);
}

const sock = dnode.connect(config.daemonRpcPort);

process.on('exit', () => {
sock.end();
process.exit(0);
});
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
process.on('exit', () => {
sock.end();
process.exit(0);
});

sock.on('remote', function(rpc) {
rpc.status((err, shares) => {
if (err) {
console.error(`\n cannot get status, reason: ${err.message}`);
Expand Down
6 changes: 2 additions & 4 deletions bin/storjshare-restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_restart = require('commander');

storjshare_restart
Expand All @@ -17,9 +17,7 @@ if (!storjshare_restart.nodeid && !storjshare_restart.all) {
process.exit(1);
}

const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
if (storjshare_restart.all) {
console.info('\n * restarting all managed shares');
}
Expand Down
6 changes: 2 additions & 4 deletions bin/storjshare-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const os = require('os');
const path = require('path');
const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_save = require('commander');

storjshare_save
Expand All @@ -25,9 +25,7 @@ if (!path.isAbsolute(storjshare_save.snapshot)) {
storjshare_save.snapshot);
}

const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.save(storjshare_save.snapshot, (err) => {
if (err) {
console.error(`\n cannot save snapshot, reason: ${err.message}`);
Expand Down
45 changes: 33 additions & 12 deletions bin/storjshare-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

'use strict';

const dnode = require('dnode');
const {spawn} = require('child_process');
const utils = require('../lib/utils');
const path = require('path');
const config = require('../lib/config/daemon');
const storjshare_start = require('commander');

storjshare_start
.description('starts a new network share')
.option('-c, --config <path>', 'specify the configuration path')
.option('-d, --detached', 'run share without management from daemon')
.parse(process.argv);

if (!storjshare_start.config) {
Expand All @@ -20,15 +22,34 @@ if (!storjshare_start.config) {
const configPath = path.isAbsolute(storjshare_start.config) ?
path.normalize(storjshare_start.config) :
path.join(process.cwd(), storjshare_start.config);
const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
rpc.start(configPath, (err) => {
if (err) {
console.error(`\n failed to start share, reason: ${err.message}`);
return sock.end();
}
console.info(`\n * starting share with config at ${configPath}`);
sock.end();

function runDetachedShare() {
const scriptPath = path.join(__dirname, '../script/farmer.js');
const shareProc = spawn(scriptPath, ['--config', configPath]);

process.stdin.pipe(shareProc.stdin);
shareProc.stdout.pipe(process.stdout);
shareProc.stderr.pipe(process.stderr);
shareProc.on('exit', (code) => process.exit(code));
}

function runManagedShare() {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.start(configPath, (err) => {
if (err) {
console.error(`\n failed to start share, reason: ${err.message}`);
return sock.end();
}
console.info(`\n * starting share with config at ${configPath}`);
sock.end();
});
});
});
}

if (storjshare_start.detached) {
runDetachedShare();
} else {
runManagedShare();
}


5 changes: 2 additions & 3 deletions bin/storjshare-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

const prettyMs = require('pretty-ms');
const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const Table = require('cli-table');
const sock = dnode.connect(config.daemonRpcPort);
const colors = require('colors/safe');
const storjshare_status = require('commander');

storjshare_status
.description('prints the status of all managed shares')
.parse(process.argv);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.status(function(err, shares) {
let table = new Table({
head: ['Share', 'Status', 'Uptime', 'Restarts', 'Peers', 'Shared'],
Expand Down
6 changes: 2 additions & 4 deletions bin/storjshare-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

const config = require('../lib/config/daemon');
const dnode = require('dnode');
const utils = require('../lib/utils');
const storjshare_stop = require('commander');

storjshare_stop
Expand All @@ -16,9 +16,7 @@ if (!storjshare_stop.nodeid) {
process.exit(1);
}

const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
utils.connectToDaemon(config.daemonRpcPort, function(rpc, sock) {
rpc.stop(storjshare_stop.nodeid, (err) => {
if (err) {
console.error(`\n cannot stop node, reason: ${err.message}`);
Expand Down
24 changes: 2 additions & 22 deletions bin/storjshare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

'use strict';

const config = require('../lib/config/daemon');
const storjshare = require('commander');
const {fork} = require('child_process');
const path = require('path');
const utils = require('../lib/utils');
const {version} = require('../package');
const {software: core, protocol} = require('storj-lib').version;

const TIME_WAIT_IF_STARTED = 1000;
const TIME_WAIT_AFTER_START = 6000;

storjshare
.version(`\n * daemon: ${version}, core: ${core}, protocol: ${protocol}`)
.command('start', 'start a farming node')
Expand All @@ -25,18 +18,5 @@ storjshare
.command('load', 'load a snapshot of previously managed shares')
.command('destroy', 'kills the farming node')
.command('killall', 'kills all shares and stops the daemon')
.command('daemon', 'starts the daemon');

if (!['daemon'].includes(process.argv[2])) {
utils.checkDaemonRpcStatus(config.daemonRpcPort, (isRunning) => {
if (isRunning) {
setTimeout(() => storjshare.parse(process.argv), TIME_WAIT_IF_STARTED);
} else {
console.info('\n * daemon is not running, starting...');
fork(path.join(__dirname, 'storjshare-daemon.js'), []);
setTimeout(() => storjshare.parse(process.argv), TIME_WAIT_AFTER_START);
}
});
} else {
storjshare.parse(process.argv);
}
.command('daemon', 'starts the daemon')
.parse(process.argv);
17 changes: 17 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'use strict';

const dnode = require('dnode');
const net = require('net');
const fs = require('fs');
const storj = require('storj-lib');
Expand Down Expand Up @@ -162,3 +163,19 @@ exports.checkDaemonRpcStatus = function(port, callback) {
callback(true);
});
};

/**
* Connects to the daemon and callback with rpc
* @param {Number} port
* @param {Function} callback
*/
exports.connectToDaemon = function(port, callback) {
const sock = dnode.connect(port);

sock.on('error', function() {
process.exitCode = 1;
console.error('\n daemon is not running, try: storjshare daemon');
});

sock.on('remote', (rpc) => callback(rpc, sock));
};
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.3.0",
"version": "2.4.0",
"description": "daemon + process manager for sharing space on the storj network",
"main": "index.js",
"bin": {
Expand Down
8 changes: 6 additions & 2 deletions script/farmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Logger = require('kad-logger-json');
const Telemetry = require('storj-telemetry-reporter');
const config = JSON.parse(JSON.stringify(require('../lib/config/farmer')));
const bytes = require('bytes');
const processIsManaged = typeof process.send === 'function';

let spaceAllocation = bytes.parse(config.storageAllocation);
let farmerState = {
Expand Down Expand Up @@ -71,10 +72,13 @@ function sendTelemetryReport() {
}

updatePercentUsed();
sendFarmerState();
setInterval(sendFarmerState, 10 * 1000); // Update state every 10 secs
setInterval(updatePercentUsed, 10 * 60 * 1000); // Update space every 10 mins

if (processIsManaged) {
sendFarmerState();
setInterval(sendFarmerState, 10 * 1000); // Update state every 10 secs
}

if (config.enableTelemetryReporting) {
setInterval(sendTelemetryReport, 10 * 60 * 1000);
}
Loading

0 comments on commit 20d5c9d

Please sign in to comment.