1
0
mirror of https://github.com/GOSTSec/gostexplr synced 2025-02-06 11:54:17 +00:00

Acquire lock before running syncBlockchain

This commit is contained in:
xcps 2018-09-27 15:40:46 +05:00 committed by R4SAS
parent 96e73a862e
commit 906b94061f
3 changed files with 31 additions and 8 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules
package-lock.json package-lock.json
config/config.json config/config.json
todo todo
sync.lock

View File

@ -1,4 +1,6 @@
var http = require('http'); var http = require('http');
const fs = require('fs-ext');
var models = require('../models'); var models = require('../models');
var rpcConfig = require('../config/config')['rpc']; var rpcConfig = require('../config/config')['rpc'];
@ -150,19 +152,38 @@ async function getSyncedHeight() {
return height; return height;
} }
async function syncBlockchain() { async function acquireLock() {
let syncedHeight = await getSyncedHeight(); let fd = fs.openSync('sync.lock', 'w');
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight);
let currentHeight = await getCurrentHeight();
console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight);
try { try {
fs.flockSync(fd, 'exnb');
} catch(ex) {
if (ex.code === 'EAGAIN') {
console.log('Synchronization is already running');
} else {
console.log('Could\'nt lock file', ex);
}
throw ex;
}
}
async function syncBlockchain() {
try {
await acquireLock();
let syncedHeight = await getSyncedHeight();
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight);
let currentHeight = await getCurrentHeight();
console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight);
while (syncedHeight < currentHeight) { while (syncedHeight < currentHeight) {
syncedHeight = await syncNextBlock(syncedHeight); syncedHeight = await syncNextBlock(syncedHeight);
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight) console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight)
} }
} catch (e) { } catch (e) {
console.log('=====', e); console.log(e);
} finally { } finally {
models.sequelize.close().then(() => process.exit(0)); models.sequelize.close().then(() => process.exit(0));
} }

View File

@ -21,8 +21,9 @@
"body-parser": "~1.18.2", "body-parser": "~1.18.2",
"cookie-parser": "~1.4.3", "cookie-parser": "~1.4.3",
"debug": "~2.6.9", "debug": "~2.6.9",
"express": "~4.15.5", "express": "^4.16.3",
"forever": "^0.15.3", "forever": "^0.15.3",
"fs-ext": "^1.2.1",
"morgan": "~1.9.0", "morgan": "~1.9.0",
"mysql": "^2.15.0", "mysql": "^2.15.0",
"mysql2": "^1.5.1", "mysql2": "^1.5.1",