Работа над BookUpdateChecker

This commit is contained in:
Book Pauk
2022-07-26 00:41:07 +07:00
parent 52927c6188
commit e214ddf8d5
4 changed files with 93 additions and 6 deletions

View File

@@ -14,7 +14,6 @@ class BookUpdateCheckerController {
this.isDevelopment = (config.branch == 'development');
this.bucServer = new BUCServer(config);
this.bucServer.main(); //no await
this.wss = wss;

View File

@@ -4,6 +4,7 @@ const _ = require('lodash');
const ReaderWorker = require('../core/Reader/ReaderWorker');//singleton
const JembaReaderStorage = require('../core/Reader/JembaReaderStorage');//singleton
const WorkerState = require('../core/WorkerState');//singleton
const BUCClient = require('../core/BookUpdateChecker/BUCClient');//singleton
const log = new (require('../core/AppLogger'))().log;//singleton
const utils = require('../core/utils');
@@ -19,6 +20,10 @@ class WebSocketController {
this.readerWorker = new ReaderWorker(config);
this.workerState = new WorkerState();
if (config.bucEnabled) {
this.bucClient = new BUCClient(config);
}
this.wss = wss;
wss.on('connection', (ws) => {
@@ -76,6 +81,8 @@ class WebSocketController {
await this.uploadFileBuf(req, ws); break;
case 'upload-file-touch':
await this.uploadFileTouch(req, ws); break;
case 'check-buc':
await this.checkBuc(req, ws); break;
default:
throw new Error(`Action not found: ${req.action}`);
@@ -179,6 +186,21 @@ class WebSocketController {
this.send({url: await this.readerWorker.uploadFileTouch(req.url)}, req, ws);
}
async checkBuc(req, ws) {
if (!this.config.bucEnabled)
throw new Error('BookUpdateChecker disabled');
if (!req.bookUrls)
throw new Error(`key 'bookUrls' is empty`);
if (!Array.isArray(req.bookUrls))
throw new Error(`key 'bookUrls' must be array`);
const data = await this.bucClient.checkBuc(req.bookUrls);
this.send({state: 'success', data}, req, ws);
}
}
module.exports = WebSocketController;

View File

@@ -0,0 +1,64 @@
const JembaConnManager = require('../../db/JembaConnManager');//singleton
const ayncExit = new (require('../AsyncExit'))();
const utils = require('../utils');
const log = new (require('../AppLogger'))().log;//singleton
const minuteMs = 60*1000;
const hourMs = 60*minuteMs;
let instance = null;
//singleton
class BUCClient {
constructor(config) {
if (!instance) {
this.config = config;
this.connManager = new JembaConnManager();
this.db = this.connManager.db['book-update-server'];
//константы
if (this.config.branch !== 'development') {
this.syncPeriod = 1*hourMs;//период синхронизации с сервером BUC
} else {
this.syncPeriod = 1*minuteMs;//период синхронизации с сервером BUC
}
this.fromCheckTime = 1;
this.main();//no await
instance = this;
}
return instance;
}
async checkBuc(bookUrls) {
return [];
}
async findMaxCheckTime() {
let result = 1;
return result;
}
async main() {
if (!this.config.bucEnabled)
throw new Error('BookUpdateChecker disabled');
try {
this.fromCheckTime = await this.findMaxCheckTime();
this.periodicSync();//no await
log(`BUC Client started`);
} catch (e) {
log(LM_FATAL, e.stack);
ayncExit.exit(1);
}
}
}
module.exports = BUCClient;

View File

@@ -17,7 +17,7 @@ let instance = null;
class BUCServer {
constructor(config) {
if (!instance) {
this.config = Object.assign({}, config);
this.config = config;
//константы
if (this.config.branch !== 'development') {
@@ -52,6 +52,8 @@ class BUCServer {
this.checkQueue = [];
this.hostChecking = {};
this.main(); //no await
instance = this;
}
@@ -260,9 +262,9 @@ await this.db.insert({
for (let i = 0; i < 10; i++)
this.periodicCheck();//no await
log(`---------------------------`);
log(`Book Update checker started`);
log(`---------------------------`);
log(`------------------`);
log(`BUC Server started`);
log(`------------------`);
} catch (e) {
log(LM_FATAL, e.stack);
ayncExit.exit(1);
@@ -270,4 +272,4 @@ await this.db.insert({
}
}
module.exports = BUCServer;
module.exports = BUCServer;