浏览代码

Работа над BookUpdateChecker

Book Pauk 2 年之前
父节点
当前提交
52927c6188

+ 8 - 1
server/config/base.js

@@ -66,9 +66,16 @@ module.exports = {
     remoteStorage: false,
     remoteStorage: false,
     /*
     /*
     remoteStorage: {
     remoteStorage: {
-        url: 'https://127.0.0.1:11900',
+        url: 'wss://127.0.0.1:11900',
         accessToken: '',
         accessToken: '',
     },
     },
     */
     */
+    bucEnabled: false,
+    bucServer: false,
+    /*
+    bucServer: {
+        url: 'wss://127.0.0.1:33443',
+    }
+    */
 };
 };
 
 

+ 3 - 1
server/config/index.js

@@ -10,7 +10,9 @@ const propsToSave = [
     'useExternalBookConverter',
     'useExternalBookConverter',
     
     
     'servers',
     'servers',
-    'remoteWebDavStorage',
+    'remoteStorage',
+    'bucEnabled',
+    'bucServer',
 ];
 ];
 
 
 let instance = null;
 let instance = null;

+ 26 - 1
server/controllers/BookUpdateCheckerController.js

@@ -13,7 +13,6 @@ class BookUpdateCheckerController {
         this.config = config;
         this.config = config;
         this.isDevelopment = (config.branch == 'development');
         this.isDevelopment = (config.branch == 'development');
 
 
-        //this.readerStorage = new JembaReaderStorage();
         this.bucServer = new BUCServer(config);
         this.bucServer = new BUCServer(config);
         this.bucServer.main(); //no await
         this.bucServer.main(); //no await
 
 
@@ -62,6 +61,10 @@ class BookUpdateCheckerController {
             switch (req.action) {
             switch (req.action) {
                 case 'test':
                 case 'test':
                     await this.test(req, ws); break;
                     await this.test(req, ws); break;
+                case 'get-buc':
+                    await this.getBuc(req, ws); break;
+                case 'update-buc':
+                    await this.updateBuc(req, ws); break;
 
 
                 default:
                 default:
                     throw new Error(`Action not found: ${req.action}`);
                     throw new Error(`Action not found: ${req.action}`);
@@ -93,6 +96,28 @@ class BookUpdateCheckerController {
         this.send({message: 'Liberama project is awesome'}, req, ws);
         this.send({message: 'Liberama project is awesome'}, req, ws);
     }
     }
 
 
+    async getBuc(req, ws) {
+        if (!req.fromCheckTime)
+            throw new Error(`key 'fromCheckTime' is empty`);
+
+        await this.bucServer.getBuc(req.fromCheckTime, (rows) => {
+            this.send({state: 'get', rows}, req, ws);
+        });
+
+        this.send({state: 'finish'}, req, ws);
+    }
+
+    async updateBuc(req, ws) {
+        if (!req.bookUrls)
+            throw new Error(`key 'bookUrls' is empty`);
+
+        if (!Array.isArray(req.bookUrls))
+            throw new Error(`key 'bookUrls' must be array`);
+
+        await this.bucServer.updateBuc(req.bookUrls);
+
+        this.send({state: 'success'}, req, ws);
+    }
 }
 }
 
 
 module.exports = BookUpdateCheckerController;
 module.exports = BookUpdateCheckerController;

+ 42 - 0
server/core/BookUpdateChecker/BUCServer.js

@@ -58,6 +58,48 @@ class BUCServer {
         return instance;
         return instance;
     }
     }
 
 
+    async getBuc(fromCheckTime, callback) {
+        const db = this.db;
+
+        while (1) {//eslint-disable-line
+            const rows = await db.select({
+                table: 'buc',
+                where: `
+                    let iter = @getItem('getBuc');
+                    if (!iter) {
+                        iter = @dirtyIndexLR('checkTime', ${db.esc(fromCheckTime)});
+                        @setItem('getBuc', iter);
+                    }
+
+                    const ids = new Set();
+                    let id = iter.next();
+                    while (!id.done && ids.size < 100) {
+                        ids.add(id.value);
+                        id = iter.next();
+                    }
+
+                    return ids;
+                `
+            });
+
+            if (rows.length)
+                callback(rows);
+            else
+                break;
+        }
+    }
+
+    async updateBuc(bookUrls) {
+        const db = this.db;
+        const now = Date.now();
+
+        await db.update({
+            table: 'buc',
+            mod: `(r) => r.queryTime = ${db.esc(now)}`,
+            where: `@@id(${db.esc(bookUrls)})`
+        });
+    }
+
     async fillCheckQueue() {
     async fillCheckQueue() {
         const db = this.db;
         const db = this.db;
 
 

+ 24 - 0
server/db/jembaMigrations/app/002-create.js

@@ -0,0 +1,24 @@
+module.exports = {
+    up: [
+        ['create', {
+            /*{
+                id, // book URL
+                queryTime: Number,
+                checkTime: Number, // 0 - never checked
+                size: Number,
+                checkSum: String, //sha256
+                state: Number, // 0 - not processing, 1 - processing
+                error: String,
+            }*/
+            table: 'buc',
+            index: [
+                {field: 'queryTime', type: 'number'},
+            ]
+        }],
+    ],    
+    down: [
+        ['drop', {
+            table: 'buc'
+        }],
+    ]
+};

+ 2 - 1
server/db/jembaMigrations/app/index.js

@@ -1,6 +1,7 @@
 module.exports = {
 module.exports = {
     table: 'migration1',
     table: 'migration1',
     data: [
     data: [
-        {id: 1, name: 'create', data: require('./001-create')}
+        {id: 1, name: 'create', data: require('./001-create')},
+        {id: 2, name: 'create', data: require('./002-create')},
     ]
     ]
 }
 }