Improved (sync/backup) scripts + misc updates
-Added locks to all sync processes (blocks, markets, peers, masternodes) as well as "create backup", "restore backup" and "delete database" functions. This helps prevent problems with syncing data while a backup is in progress for example -The code to initialize certain database collections on startup was moved into database.js and is now called from restore_backup.js and delete_database.js. This effectively allows the database to be deleted or restored to a completely different backup while the explorer is still running -Lock functions (create_lock, remove_lock, is_locked) were moved into explorer.js for better reusability and rewriten to be synchronous -is_locked function now accepts an array of lock files to be able to check for multiple locks in a single call -remove_sync_message() function was moved into database.js so that restore_backup.js and delete_database.js can also check for and remove the sync msg if it exists -Useful Scripts section updated in the README to make it clear that the explorer no longer needs to be stopped for these scripts to be run -Most if not all log messages now start with a capitlal letter
This commit is contained in:
@@ -1315,5 +1315,93 @@ module.exports = {
|
||||
}, function() {
|
||||
return cb(arr_vin, tx_type);
|
||||
});
|
||||
},
|
||||
|
||||
create_lock: function(lock) {
|
||||
const fs = require('fs');
|
||||
var fname = './tmp/' + lock + '.pid';
|
||||
|
||||
try {
|
||||
fs.appendFileSync(fname, process.pid.toString());
|
||||
return true;
|
||||
} catch(err) {
|
||||
console.log("Error: Unable to remove lock: %s", fname);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
remove_lock: function(lock) {
|
||||
const fs = require('fs');
|
||||
var fname = './tmp/' + lock + '.pid';
|
||||
|
||||
try {
|
||||
fs.unlinkSync(fname);
|
||||
return true;
|
||||
} catch(err) {
|
||||
console.log("Error: Unable to remove lock: %s", fname);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
is_locked: function(lock_array) {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
var retVal = false;
|
||||
|
||||
// loop through all lock files that need to be checked
|
||||
for (var i = 0; i < lock_array.length; i++) {
|
||||
var pidFile = path.join(path.dirname(__dirname), 'tmp', `${lock_array[i]}.pid`);
|
||||
|
||||
// check if the script is already running (tmp/file.pid file already exists)
|
||||
if (fs.existsSync(pidFile)) {
|
||||
const { execSync } = require('child_process');
|
||||
var deactivateLock = false;
|
||||
|
||||
// the pid file exists
|
||||
// determine the operating system
|
||||
switch (process.platform) {
|
||||
case 'win32':
|
||||
// windows
|
||||
// run a cmd that will determine if the lock should still be active
|
||||
var cmdResult = execSync(`tasklist /FI "PID eq ${fs.readFileSync(pidFile).toString()}"`);
|
||||
|
||||
// check if the process that created the lock is actually still running (crude check by testing for # of carriage returns or node.exe process running, but should work universally across different systems and languages)
|
||||
if (cmdResult.toString().split('\n').length < 4 || cmdResult.toString().toLowerCase().indexOf('\nnode.exe') == -1) {
|
||||
// lock should be deactivated
|
||||
deactivateLock = true;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
// linux or other
|
||||
// run a cmd that will determine if the lock should still be active
|
||||
|
||||
try {
|
||||
var cmdResult = execSync('ps -p `cat ' + pidFile + '` > /dev/null');
|
||||
} catch (err) {
|
||||
// if an error occurs, the process is NOT running and therefore the lock should be deactivated
|
||||
deactivateLock = true;
|
||||
}
|
||||
}
|
||||
|
||||
// check if the lock should be deactivated
|
||||
if (deactivateLock) {
|
||||
// script is not actually running so the lock file can be deleted
|
||||
try {
|
||||
fs.rmSync(pidFile);
|
||||
} catch(err) {
|
||||
console.log(`Failed to delete lock file ${pidFile}: ${err}`);
|
||||
}
|
||||
} else {
|
||||
// script is running
|
||||
console.log(`${lock_array[i]} script is running..`);
|
||||
retVal = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user