Улучшение работы RemoteLib.downloadInpxFile
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
const fs = require('fs-extra');
|
||||
const _ = require('lodash');
|
||||
const WebSocket = require ('ws');
|
||||
|
||||
@@ -182,9 +181,9 @@ class WebSocketController {
|
||||
if (!this.config.allowRemoteLib)
|
||||
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');
|
||||
}
|
||||
|
||||
async getInpxFileHash() {
|
||||
return await utils.getFileHash(this.config.inpxFile, 'sha256', 'hex');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = InpxHashCreator;
|
||||
@@ -4,6 +4,7 @@ const utils = require('./utils');
|
||||
|
||||
const FileDownloader = require('./FileDownloader');
|
||||
const WebSocketConnection = require('./WebSocketConnection');
|
||||
const InpxHashCreator = require('./InpxHashCreator');
|
||||
const log = new (require('./AppLogger'))().log;//singleton
|
||||
|
||||
//singleton
|
||||
@@ -20,10 +21,9 @@ class RemoteLib {
|
||||
|
||||
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.inpxHashCreator = new InpxHashCreator(config);
|
||||
this.inpxFileHash = '';
|
||||
|
||||
instance = this;
|
||||
}
|
||||
@@ -46,17 +46,16 @@ class RemoteLib {
|
||||
return response;
|
||||
}
|
||||
|
||||
async downloadInpxFile(getPeriod = 0) {
|
||||
if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
||||
return this.inpxFile;
|
||||
async downloadInpxFile() {
|
||||
if (!this.inpxFileHash)
|
||||
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');
|
||||
|
||||
this.lastUpdateTime = Date.now();
|
||||
|
||||
return this.inpxFile;
|
||||
if (response.data) {
|
||||
await fs.writeFile(this.config.inpxFile, response.data, 'base64');
|
||||
this.inpxFileHash = '';
|
||||
}
|
||||
}
|
||||
|
||||
async downloadBook(bookPath, downFileName) {
|
||||
|
||||
@@ -43,6 +43,9 @@ class WebWorker {
|
||||
this.remoteLib = new RemoteLib(config);
|
||||
}
|
||||
|
||||
this.inpxHashCreator = new InpxHashCreator(config);
|
||||
this.inpxFileHash = '';
|
||||
|
||||
this.wState = this.workerState.getControl('server_state');
|
||||
this.myState = '';
|
||||
this.db = null;
|
||||
@@ -136,6 +139,8 @@ class WebWorker {
|
||||
const config = this.config;
|
||||
const dbPath = `${config.dataDir}/db`;
|
||||
|
||||
this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
|
||||
|
||||
//пересоздаем БД из INPX если нужно
|
||||
if (config.recreateDb || recreate)
|
||||
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() {
|
||||
try {
|
||||
const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
|
||||
@@ -516,18 +533,16 @@ class WebWorker {
|
||||
if (!inpxCheckInterval)
|
||||
return;
|
||||
|
||||
const inpxHashCreator = new InpxHashCreator(this.config);
|
||||
|
||||
while (1) {// eslint-disable-line no-constant-condition
|
||||
try {
|
||||
while (this.myState != ssNormal)
|
||||
await utils.sleep(1000);
|
||||
|
||||
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 currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
|
||||
|
||||
@@ -114,9 +114,10 @@ async function init() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
config.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
||||
const RemoteLib = require('./core/RemoteLib');//singleton
|
||||
const remoteLib = new RemoteLib(config);
|
||||
config.inpxFile = await remoteLib.downloadInpxFile();
|
||||
await remoteLib.downloadInpxFile();
|
||||
}
|
||||
|
||||
config.recreateDb = argv.recreate || false;
|
||||
|
||||
Reference in New Issue
Block a user