diff --git a/server/controllers/BookUpdateCheckerController.js b/server/controllers/BookUpdateCheckerController.js index 084c6582..8b54616e 100644 --- a/server/controllers/BookUpdateCheckerController.js +++ b/server/controllers/BookUpdateCheckerController.js @@ -14,7 +14,6 @@ class BookUpdateCheckerController { this.isDevelopment = (config.branch == 'development'); this.bucServer = new BUCServer(config); - this.bucServer.main(); //no await this.wss = wss; diff --git a/server/controllers/WebSocketController.js b/server/controllers/WebSocketController.js index 8dc67a1c..43593e4b 100644 --- a/server/controllers/WebSocketController.js +++ b/server/controllers/WebSocketController.js @@ -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; diff --git a/server/core/BookUpdateChecker/BUCClient.js b/server/core/BookUpdateChecker/BUCClient.js index e69de29b..96c6780d 100644 --- a/server/core/BookUpdateChecker/BUCClient.js +++ b/server/core/BookUpdateChecker/BUCClient.js @@ -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; \ No newline at end of file diff --git a/server/core/BookUpdateChecker/BUCServer.js b/server/core/BookUpdateChecker/BUCServer.js index 796a9677..4a9c6457 100644 --- a/server/core/BookUpdateChecker/BUCServer.js +++ b/server/core/BookUpdateChecker/BUCServer.js @@ -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; \ No newline at end of file +module.exports = BUCServer;