diff --git a/bin/cluster b/bin/cluster index 7bcdd6692..a1add9038 100644 --- a/bin/cluster +++ b/bin/cluster @@ -2,7 +2,7 @@ var cluster = require('cluster'); var fs = require('fs'); if (cluster.isMaster) { - fs.writeFile('./tmp/cluster.pid', process.pid, function (err) { + fs.writeFile('./tmp/cluster.pid', process.pid.toString(), function (err) { if (err) { console.log('Error: unable to create cluster.pid'); process.exit(1); diff --git a/scripts/sync.js b/scripts/sync.js index de1cb8d00..421b92801 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -1,9 +1,9 @@ var mongoose = require('mongoose') , db = require('../lib/database') - , Tx = require('../models/tx') - , Address = require('../models/address') - , Richlist = require('../models/richlist') - , Stats = require('../models/stats') + , Tx = require('../models/tx') + , Address = require('../models/address') + , Richlist = require('../models/richlist') + , Stats = require('../models/stats') , settings = require('../lib/settings') , fs = require('fs'); @@ -23,10 +23,10 @@ function usage() { console.log('check checks index for (and adds) any missing transactions/addresses'); console.log('reindex Clears index then resyncs from genesis to current block'); console.log(''); - console.log('notes:'); + console.log('notes:'); console.log('* \'current block\' is the latest created block when script is executed.'); console.log('* The market database only supports (& defaults to) reindex mode.'); - console.log('* If check mode finds missing data(ignoring new data since last sync),'); + console.log('* If check mode finds missing data(ignoring new data since last sync),'); console.log(' index_timeout in settings.json is set too low.') console.log(''); process.exit(0); @@ -34,34 +34,33 @@ function usage() { // check options if (process.argv[2] == 'index') { - if (process.argv.length <3) { + if (process.argv.length < 3) { usage(); } else { - switch(process.argv[3]) - { - case 'update': - mode = 'update'; - break; - case 'check': - mode = 'check'; - break; - case 'reindex': - mode = 'reindex'; - break; - default: - usage(); + switch (process.argv[3]) { + case 'update': + mode = 'update'; + break; + case 'check': + mode = 'check'; + break; + case 'reindex': + mode = 'reindex'; + break; + default: + usage(); } } -} else if (process.argv[2] == 'market'){ +} else if (process.argv[2] == 'market') { database = 'market'; } else { usage(); } function create_lock(cb) { - if ( database == 'index' ) { + if (database == 'index') { var fname = './tmp/' + database + '.pid'; - fs.appendFile(fname, process.pid, function (err) { + fs.appendFile(fname, process.pid.toString(), function (err) { if (err) { console.log("Error: unable to create %s", fname); process.exit(1); @@ -75,10 +74,10 @@ function create_lock(cb) { } function remove_lock(cb) { - if ( database == 'index' ) { + if (database == 'index') { var fname = './tmp/' + database + '.pid'; - fs.unlink(fname, function (err){ - if(err) { + fs.unlink(fname, function (err) { + if (err) { console.log("unable to remove lock: %s", fname); process.exit(1); } else { @@ -87,14 +86,14 @@ function remove_lock(cb) { }); } else { return cb(); - } + } } function is_locked(cb) { - if ( database == 'index' ) { + if (database == 'index') { var fname = './tmp/' + database + '.pid'; - fs.exists(fname, function (exists){ - if(exists) { + fs.exists(fname, function (exists) { + if (exists) { return cb(true); } else { return cb(false); @@ -102,11 +101,11 @@ function is_locked(cb) { }); } else { return cb(); - } + } } function exit() { - remove_lock(function(){ + remove_lock(function () { mongoose.disconnect(); process.exit(0); }); @@ -123,71 +122,71 @@ is_locked(function (exists) { console.log("Script already running.."); process.exit(0); } else { - create_lock(function (){ + create_lock(function () { console.log("script launched with pid: " + process.pid); - mongoose.connect(dbString , { useNewUrlParser: true }, function(err) { + mongoose.connect(dbString, { useNewUrlParser: true }, function (err) { if (err) { console.log('Unable to connect to database: %s', dbString); console.log('Aborting'); exit(); } else if (database == 'index') { - db.check_stats(settings.coin, function(exists) { + db.check_stats(settings.coin, function (exists) { if (exists == false) { console.log('Run \'npm start\' to create database structures before running this script.'); exit(); } else { - db.update_db(settings.coin, function(){ - db.get_stats(settings.coin, function(stats){ + db.update_db(settings.coin, function () { + db.get_stats(settings.coin, function (stats) { if (settings.heavy == true) { - db.update_heavy(settings.coin, stats.count, 20, function(){ - + db.update_heavy(settings.coin, stats.count, 20, function () { + }); } if (mode == 'reindex') { - Tx.remove({}, function(err) { - Address.remove({}, function(err2) { - Richlist.update({coin: settings.coin}, { + Tx.remove({}, function (err) { + Address.remove({}, function (err2) { + Richlist.update({ coin: settings.coin }, { received: [], balance: [], - }, function(err3) { - Stats.update({coin: settings.coin}, { + }, function (err3) { + Stats.update({ coin: settings.coin }, { last: 0, - }, function() { + }, function () { console.log('index cleared (reindex)'); - }); - db.update_tx_db(settings.coin, 1, stats.count, settings.update_timeout, function(){ - db.update_richlist('received', function(){ - db.update_richlist('balance', function(){ - db.get_stats(settings.coin, function(nstats){ + }); + db.update_tx_db(settings.coin, 1, stats.count, settings.update_timeout, function () { + db.update_richlist('received', function () { + db.update_richlist('balance', function () { + db.get_stats(settings.coin, function (nstats) { console.log('reindex complete (block: %s)', nstats.last); - db.update_cronjob_run(settings.coin,{list_blockchain_update: Math.floor(new Date() / 1000)}, function(cb) { + db.update_cronjob_run(settings.coin, { list_blockchain_update: Math.floor(new Date() / 1000) }, function (cb) { exit(); - }); + }); }); }); }); }); }); }); - }); + }); } else if (mode == 'check') { - db.update_tx_db(settings.coin, 1, stats.count, settings.check_timeout, function(){ - db.get_stats(settings.coin, function(nstats){ + db.update_tx_db(settings.coin, 1, stats.count, settings.check_timeout, function () { + db.get_stats(settings.coin, function (nstats) { console.log('check complete (block: %s)', nstats.last); - db.update_cronjob_run(settings.coin,{list_blockchain_update: Math.floor(new Date() / 1000)}, function(cb) { + db.update_cronjob_run(settings.coin, { list_blockchain_update: Math.floor(new Date() / 1000) }, function (cb) { exit(); - }); + }); }); }); } else if (mode == 'update') { - db.update_tx_db(settings.coin, stats.last, stats.count, settings.update_timeout, function(){ - db.update_richlist('received', function(){ - db.update_richlist('balance', function(){ - db.get_stats(settings.coin, function(nstats){ + db.update_tx_db(settings.coin, stats.last, stats.count, settings.update_timeout, function () { + db.update_richlist('received', function () { + db.update_richlist('balance', function () { + db.get_stats(settings.coin, function (nstats) { console.log('update complete (block: %s)', nstats.last); - db.update_cronjob_run(settings.coin,{list_blockchain_update: Math.floor(new Date() / 1000)}, function(cb) { + db.update_cronjob_run(settings.coin, { list_blockchain_update: Math.floor(new Date() / 1000) }, function (cb) { exit(); - }); + }); }); }); }); @@ -203,24 +202,24 @@ is_locked(function (exists) { var complete = 0; for (var x = 0; x < markets.length; x++) { var market = markets[x]; - db.check_market(market, function(mkt, exists) { + db.check_market(market, function (mkt, exists) { if (exists) { - db.update_markets_db(mkt, function(err) { + db.update_markets_db(mkt, function (err) { if (!err) { console.log('%s market data updated successfully.', mkt); complete++; if (complete == markets.length) { - db.update_cronjob_run(settings.coin,{list_market_update: Math.floor(new Date() / 1000)}, function(cb) { + db.update_cronjob_run(settings.coin, { list_market_update: Math.floor(new Date() / 1000) }, function (cb) { exit(); - }); + }); } } else { console.log('%s: %s', mkt, err); complete++; if (complete == markets.length) { - db.update_cronjob_run(settings.coin,{list_market_update: Math.floor(new Date() / 1000)}, function(cb) { + db.update_cronjob_run(settings.coin, { list_market_update: Math.floor(new Date() / 1000) }, function (cb) { exit(); - }); + }); } } });