Улучшения механизма запуска периодических задач

This commit is contained in:
Book Pauk
2023-02-03 16:23:13 +07:00
parent 69a2e5cda3
commit 4c8c921b03

View File

@@ -393,6 +393,9 @@ class Reader {
this.recentItemKeys = []; this.recentItemKeys = [];
//сохранение в удаленном хранилище //сохранение в удаленном хранилище
await this.$refs.serverStorage.saveRecent(itemKeys); await this.$refs.serverStorage.saveRecent(itemKeys);
//periodicTasks
this.periodicTasks();//no await
} catch (e) { } catch (e) {
if (!this.offlineModeActive) if (!this.offlineModeActive)
this.$root.notify.error(e.message); this.$root.notify.error(e.message);
@@ -442,26 +445,15 @@ class Reader {
this.$refs.recentBooksPage.init(); this.$refs.recentBooksPage.init();
})(); })();
//проверки обновлений читалки //единственный запуск periodicTasks при инициализации
//дальнейшие запуски periodicTasks выполняются из debouncedSaveRecent
//т.е. только по действию пользователя
(async() => { (async() => {
await utils.sleep(15*1000);
this.isFirstNeedUpdateNotify = true; this.isFirstNeedUpdateNotify = true;
//вечный цикл, запрашиваем периодически конфиг для проверки выхода новой версии читалки
while (1) {// eslint-disable-line no-constant-condition
await this.checkNewVersionAvailable();
await utils.sleep(60*60*1000); //каждый час
}
//дальше хода нет
})();
//проверки обновлений книг this.allowPeriodicTasks = true;
(async() => { this.periodicTasks();//no await
await utils.sleep(15*1000); //подождем неск. секунд перед первым запросом
//вечный цикл, запрашиваем периодически обновления
while (1) {// eslint-disable-line no-constant-condition
await this.checkBuc();
await utils.sleep(70*60*1000); //каждые 70 минут
}
//дальше хода нет
})(); })();
} }
@@ -560,26 +552,56 @@ class Reader {
} }
} }
async checkNewVersionAvailable() { async periodicTasks() {
if (!this.checkingNewVersion && this.showNeedUpdateNotify) { if (!this.allowPeriodicTasks || this.doingPeriodicTasks)
this.checkingNewVersion = true; return;
try {
await utils.sleep(15*1000); //подождем 15 секунд, чтобы прогрузился ServiceWorker при выходе новой версии
const config = await miscApi.loadConfig();
this.commit('config/setConfig', config);
let againMes = ''; this.doingPeriodicTasks = true;
if (this.isFirstNeedUpdateNotify) { try {
againMes = ' еще один раз'; if (!this.taskList) {
const taskArr = [
['checkNewVersionAvailable', 60], //проверки обновлений читалки, каждый час
['checkBuc', 70], //проверки обновлений книг, каждые 70 минут
];
this.taskList = [];
for (const task of taskArr) {
const [method, period] = task;
this.taskList.push({method, period, lastRunTime: 0});
} }
}
for (const task of this.taskList) {
if (Date.now() - task.lastRunTime >= task.period*60*1000) {
try {
//console.log('task run', task.method);
await this[task.method]();
} catch (e) {
console.error(e);
}
task.lastRunTime = Date.now();
}
}
} catch (e) {
console.error(e);
} finally {
this.doingPeriodicTasks = false;
}
}
async checkNewVersionAvailable() {
if (this.showNeedUpdateNotify) {
const config = await miscApi.loadConfig();
this.commit('config/setConfig', config);
let againMes = '';
if (this.isFirstNeedUpdateNotify) {
againMes = ' еще один раз';
}
if (this.version != this.clientVersion)
this.$root.notify.info(`Вышла новая версия (v${this.version}) читалки.<br>Пожалуйста, обновите страницу${againMes}.`, 'Обновление');
if (this.version != this.clientVersion)
this.$root.notify.info(`Вышла новая версия (v${this.version}) читалки.<br>Пожалуйста, обновите страницу${againMes}.`, 'Обновление');
} catch(e) {
console.error(e);
} finally {
this.checkingNewVersion = false;
}
this.isFirstNeedUpdateNotify = false; this.isFirstNeedUpdateNotify = false;
} }
} }
@@ -588,82 +610,78 @@ class Reader {
if (!this.bothBucEnabled) if (!this.bothBucEnabled)
return; return;
try { const sorted = bookManager.getSortedRecent();
const sorted = bookManager.getSortedRecent();
//выберем все кандидиаты на обновление //выберем все кандидиаты на обновление
const updateUrls = new Set(); const updateUrls = new Set();
for (const book of sorted) { for (const book of sorted) {
if (!book.deleted && book.checkBuc && book.url && book.url.indexOf('disk://') !== 0) if (!book.deleted && book.checkBuc && book.url && book.url.indexOf('disk://') !== 0)
updateUrls.add(book.url); updateUrls.add(book.url);
}
//теперь по кусочкам запросим сервер
const arr = Array.from(updateUrls);
const bucSize = {};
const chunkSize = 100;
for (let i = 0; i < arr.length; i += chunkSize) {
const chunk = arr.slice(i, i + chunkSize);
const data = await readerApi.checkBuc(chunk);
for (const item of data) {
bucSize[item.id] = item.size;
} }
//теперь по кусочкам запросим сервер await utils.sleep(1000);//чтобы не ддосить сервер
const arr = Array.from(updateUrls); }
const bucSize = {};
const chunkSize = 100;
for (let i = 0; i < arr.length; i += chunkSize) {
const chunk = arr.slice(i, i + chunkSize);
const data = await readerApi.checkBuc(chunk); const checkSetTime = {};
//проставим новые размеры у книг
for (const item of data) { for (const book of sorted) {
bucSize[item.id] = item.size; if (book.deleted)
} continue;
await utils.sleep(1000);//чтобы не ддосить сервер //размер 0 считаем отсутствующим
if (book.url && bucSize[book.url] && bucSize[book.url] !== book.bucSize) {
book.bucSize = bucSize[book.url];
await bookManager.recentSetItem(book);
} }
const checkSetTime = {}; //подготовка к следующему шагу, ищем книгу по url с максимальной датой установки checkBucTime/loadTime
//проставим новые размеры у книг //от этой даты будем потом отсчитывать bucCancelDays
for (const book of sorted) { if (updateUrls.has(book.url)) {
if (book.deleted) let rec = checkSetTime[book.url] || {time: 0, loadTime: 0};
continue;
//размер 0 считаем отсутствующим
if (book.url && bucSize[book.url] && bucSize[book.url] !== book.bucSize) {
book.bucSize = bucSize[book.url];
await bookManager.recentSetItem(book);
}
//подготовка к следующему шагу, ищем книгу по url с максимальной датой установки checkBucTime/loadTime const time = (book.checkBucTime ? book.checkBucTime : (rec.loadTime || 0));
//от этой даты будем потом отсчитывать bucCancelDays if (time > rec.time || (time == rec.time && (book.loadTime > rec.loadTime)))
if (updateUrls.has(book.url)) { rec = {time, loadTime: book.loadTime, key: book.key};
let rec = checkSetTime[book.url] || {time: 0, loadTime: 0};
const time = (book.checkBucTime ? book.checkBucTime : (rec.loadTime || 0)); checkSetTime[book.url] = rec;
if (time > rec.time || (time == rec.time && (book.loadTime > rec.loadTime)))
rec = {time, loadTime: book.loadTime, key: book.key};
checkSetTime[book.url] = rec;
}
} }
}
//bucCancelEnabled и bucCancelDays //bucCancelEnabled и bucCancelDays
//снимем флаг checkBuc у необновлявшихся bucCancelDays //снимем флаг checkBuc у необновлявшихся bucCancelDays
if (this.bucCancelEnabled) { if (this.bucCancelEnabled) {
for (const rec of Object.values(checkSetTime)) { for (const rec of Object.values(checkSetTime)) {
if (rec.time && Date.now() - rec.time > this.bucCancelDays*24*3600*1000) { if (rec.time && Date.now() - rec.time > this.bucCancelDays*24*3600*1000) {
const book = await bookManager.getRecentBook({key: rec.key}); const book = await bookManager.getRecentBook({key: rec.key});
const needBookUpdate = const needBookUpdate =
book.checkBuc book.checkBuc
&& book.bucSize && book.bucSize
&& utils.hasProp(book, 'downloadSize') && utils.hasProp(book, 'downloadSize')
&& book.bucSize !== book.downloadSize && book.bucSize !== book.downloadSize
&& (book.bucSize - book.downloadSize >= this.bucSizeDiff) && (book.bucSize - book.downloadSize >= this.bucSizeDiff)
; ;
if (book && !needBookUpdate) { if (book && !needBookUpdate) {
await bookManager.setCheckBuc(book, undefined);//!!! await bookManager.setCheckBuc(book, undefined);//!!!
}
} }
} }
} }
await this.$refs.recentBooksPage.updateTableData();
} catch (e) {
console.error(e);
} }
await this.$refs.recentBooksPage.updateTableData();
} }
updateCountChanged(event) { updateCountChanged(event) {
@@ -1409,8 +1427,6 @@ class Reader {
if (!this.showHelpOnErrorIfNeeded(url)) { if (!this.showHelpOnErrorIfNeeded(url)) {
this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'}); this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
} }
} finally {
this.checkNewVersionAvailable();
} }
} }