Улучшение работы RemoteLib.downloadInpxFile
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
const fs = require('fs-extra');
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const WebSocket = require ('ws');
|
const WebSocket = require ('ws');
|
||||||
|
|
||||||
@@ -182,9 +181,9 @@ class WebSocketController {
|
|||||||
if (!this.config.allowRemoteLib)
|
if (!this.config.allowRemoteLib)
|
||||||
throw new Error('Remote lib access disabled');
|
throw new Error('Remote lib access disabled');
|
||||||
|
|
||||||
const data = await fs.readFile(this.config.inpxFile, 'base64');
|
const result = await this.webWorker.getInpxFile(req);
|
||||||
|
|
||||||
this.send({data}, req, ws);
|
this.send(result, req, ws);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ class InpxHashCreator {
|
|||||||
|
|
||||||
return utils.getBufHash(joinedHash, 'sha256', 'hex');
|
return utils.getBufHash(joinedHash, 'sha256', 'hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getInpxFileHash() {
|
||||||
|
return await utils.getFileHash(this.config.inpxFile, 'sha256', 'hex');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = InpxHashCreator;
|
module.exports = InpxHashCreator;
|
||||||
@@ -4,6 +4,7 @@ const utils = require('./utils');
|
|||||||
|
|
||||||
const FileDownloader = require('./FileDownloader');
|
const FileDownloader = require('./FileDownloader');
|
||||||
const WebSocketConnection = require('./WebSocketConnection');
|
const WebSocketConnection = require('./WebSocketConnection');
|
||||||
|
const InpxHashCreator = require('./InpxHashCreator');
|
||||||
const log = new (require('./AppLogger'))().log;//singleton
|
const log = new (require('./AppLogger'))().log;//singleton
|
||||||
|
|
||||||
//singleton
|
//singleton
|
||||||
@@ -20,10 +21,9 @@ class RemoteLib {
|
|||||||
|
|
||||||
this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://');
|
this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://');
|
||||||
|
|
||||||
this.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
|
||||||
this.lastUpdateTime = 0;
|
|
||||||
|
|
||||||
this.down = new FileDownloader(config.maxPayloadSize*1024*1024);
|
this.down = new FileDownloader(config.maxPayloadSize*1024*1024);
|
||||||
|
this.inpxHashCreator = new InpxHashCreator(config);
|
||||||
|
this.inpxFileHash = '';
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
}
|
||||||
@@ -46,17 +46,16 @@ class RemoteLib {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadInpxFile(getPeriod = 0) {
|
async downloadInpxFile() {
|
||||||
if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
if (!this.inpxFileHash)
|
||||||
return this.inpxFile;
|
this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
|
||||||
|
|
||||||
const response = await this.wsRequest({action: 'get-inpx-file'});
|
const response = await this.wsRequest({action: 'get-inpx-file', inpxFileHash: this.inpxFileHash});
|
||||||
|
|
||||||
await fs.writeFile(this.inpxFile, response.data, 'base64');
|
if (response.data) {
|
||||||
|
await fs.writeFile(this.config.inpxFile, response.data, 'base64');
|
||||||
this.lastUpdateTime = Date.now();
|
this.inpxFileHash = '';
|
||||||
|
}
|
||||||
return this.inpxFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadBook(bookPath, downFileName) {
|
async downloadBook(bookPath, downFileName) {
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ class WebWorker {
|
|||||||
this.remoteLib = new RemoteLib(config);
|
this.remoteLib = new RemoteLib(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.inpxHashCreator = new InpxHashCreator(config);
|
||||||
|
this.inpxFileHash = '';
|
||||||
|
|
||||||
this.wState = this.workerState.getControl('server_state');
|
this.wState = this.workerState.getControl('server_state');
|
||||||
this.myState = '';
|
this.myState = '';
|
||||||
this.db = null;
|
this.db = null;
|
||||||
@@ -136,6 +139,8 @@ class WebWorker {
|
|||||||
const config = this.config;
|
const config = this.config;
|
||||||
const dbPath = `${config.dataDir}/db`;
|
const dbPath = `${config.dataDir}/db`;
|
||||||
|
|
||||||
|
this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
|
||||||
|
|
||||||
//пересоздаем БД из INPX если нужно
|
//пересоздаем БД из INPX если нужно
|
||||||
if (config.recreateDb || recreate)
|
if (config.recreateDb || recreate)
|
||||||
await fs.remove(dbPath);
|
await fs.remove(dbPath);
|
||||||
@@ -427,6 +432,18 @@ class WebWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getInpxFile(params) {
|
||||||
|
let data = null;
|
||||||
|
if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) {
|
||||||
|
data = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data === null)
|
||||||
|
data = await fs.readFile(this.config.inpxFile, 'base64');
|
||||||
|
|
||||||
|
return {data};
|
||||||
|
}
|
||||||
|
|
||||||
logServerStats() {
|
logServerStats() {
|
||||||
try {
|
try {
|
||||||
const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
|
const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
|
||||||
@@ -516,18 +533,16 @@ class WebWorker {
|
|||||||
if (!inpxCheckInterval)
|
if (!inpxCheckInterval)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const inpxHashCreator = new InpxHashCreator(this.config);
|
|
||||||
|
|
||||||
while (1) {// eslint-disable-line no-constant-condition
|
while (1) {// eslint-disable-line no-constant-condition
|
||||||
try {
|
try {
|
||||||
while (this.myState != ssNormal)
|
while (this.myState != ssNormal)
|
||||||
await utils.sleep(1000);
|
await utils.sleep(1000);
|
||||||
|
|
||||||
if (this.remoteLib) {
|
if (this.remoteLib) {
|
||||||
await this.remoteLib.downloadInpxFile(60*1000);
|
await this.remoteLib.downloadInpxFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
const newInpxHash = await inpxHashCreator.getHash();
|
const newInpxHash = await this.inpxHashCreator.getHash();
|
||||||
|
|
||||||
const dbConfig = await this.dbConfig();
|
const dbConfig = await this.dbConfig();
|
||||||
const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
|
const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
|
||||||
|
|||||||
@@ -114,9 +114,10 @@ async function init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
config.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
||||||
const RemoteLib = require('./core/RemoteLib');//singleton
|
const RemoteLib = require('./core/RemoteLib');//singleton
|
||||||
const remoteLib = new RemoteLib(config);
|
const remoteLib = new RemoteLib(config);
|
||||||
config.inpxFile = await remoteLib.downloadInpxFile();
|
await remoteLib.downloadInpxFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
config.recreateDb = argv.recreate || false;
|
config.recreateDb = argv.recreate || false;
|
||||||
|
|||||||
Reference in New Issue
Block a user