Работа над BookUpdateChecker

This commit is contained in:
Book Pauk
2022-07-26 00:11:15 +07:00
parent 92ca9dd983
commit 52927c6188
6 changed files with 105 additions and 4 deletions

View File

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

View File

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

View File

@@ -13,7 +13,6 @@ class BookUpdateCheckerController {
this.config = config;
this.isDevelopment = (config.branch == 'development');
//this.readerStorage = new JembaReaderStorage();
this.bucServer = new BUCServer(config);
this.bucServer.main(); //no await
@@ -62,6 +61,10 @@ class BookUpdateCheckerController {
switch (req.action) {
case 'test':
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:
throw new Error(`Action not found: ${req.action}`);
@@ -93,6 +96,28 @@ class BookUpdateCheckerController {
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;

View File

@@ -58,6 +58,48 @@ class BUCServer {
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() {
const db = this.db;

View File

@@ -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'
}],
]
};

View File

@@ -1,6 +1,7 @@
module.exports = {
table: 'migration1',
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')},
]
}