Skip to content

Commit

Permalink
patch Issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviator-Coding committed Sep 26, 2020
1 parent 0d61609 commit bd462d6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion bin/cluster
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
137 changes: 68 additions & 69 deletions scripts/sync.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -23,45 +23,44 @@ 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);
}

// 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);
Expand All @@ -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 {
Expand All @@ -87,26 +86,26 @@ 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);
}
});
} else {
return cb();
}
}
}

function exit() {
remove_lock(function(){
remove_lock(function () {
mongoose.disconnect();
process.exit(0);
});
Expand All @@ -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();
});
});
});
});
});
Expand All @@ -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();
});
});
}
}
});
Expand Down

0 comments on commit bd462d6

Please sign in to comment.