From 9e503579300774992265da68fe758a3dfa485703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Sun, 2 May 2021 14:00:39 +0200 Subject: [PATCH] Add automatic reindexing using a Cron --- package.json | 1 + server/env.js | 8 ++++++++ server/index.js | 13 ++++++++++++- server/tree/IndexCreator.js | 30 +++++++++++++++++++++++++----- yarn.lock | 19 +++++++++++++++++++ 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index cd6843f..cb7e43e 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "next": "^10.0.0", "next-compose-plugins": "^2.2.0", "next-cookies": "2.0.3", + "node-cron": "^3.0.0", "normalize.css": "^8.0.1", "pdfjs-dist": "^2.1.266", "percentile": "^1.2.2", diff --git a/server/env.js b/server/env.js index 2b885b0..dbb1065 100644 --- a/server/env.js +++ b/server/env.js @@ -31,3 +31,11 @@ process.env.DIR_CACHE = path.join(process.cwd(), "cache"); */ process.env.SERVER_PORT = 8080; process.env.SERVER_URL = `http://localhost:${process.env.SERVER_PORT}/`; + +/** + * The Schedule at which to refresh the library + * You can continue reading while the refresh is in progress + * + * You can easily create cron schedules at here : https://crontab.guru/#25_3_*_*_* + */ +process.env.REFRESH_SCHEDULE = '25 3 * * *'; diff --git a/server/index.js b/server/index.js index 63ed4ac..be07012 100644 --- a/server/index.js +++ b/server/index.js @@ -6,13 +6,15 @@ const express = require("express"); const loudRejection = require("loud-rejection"); const compression = require("compression"); const morgan = require("morgan"); +const cron = require("node-cron"); require("./env"); // Kickstart index creation -require("./comics"); +const comicsIndex = require("./comics"); const title = chalk.underline.bold; +const error = chalk.red; const app = next({ dev: process.env.NODE_ENV !== "production", @@ -24,6 +26,15 @@ loudRejection(); console.log(title("Starting server")); +cron.schedule(process.env.REFRESH_SCHEDULE, () => { + comicsIndex.reindex().then( () => { + console.log(title("Index ready ! Have a good read !")); + }, + e => { + console.error(error("Could not create index"), e); + }); +}); + app.prepare().then(() => { const server = express(); diff --git a/server/tree/IndexCreator.js b/server/tree/IndexCreator.js index ce51d50..b09acc8 100644 --- a/server/tree/IndexCreator.js +++ b/server/tree/IndexCreator.js @@ -37,11 +37,15 @@ function forEachAsync(items, cb) { module.exports = class IndexCreator { constructor(dirPath) { - this.foundBooks = 0; - this.foundThumbs = 0; this.dirPath = dirPath; this.isReady = false; this.phase = "NONE"; + this.initStats(); + } + + initStats() { + this.foundThumbs = 0; + this.foundBooks = 0; this.errors = []; this.stats = []; } @@ -209,8 +213,6 @@ module.exports = class IndexCreator { this.phase = "DONE"; this.isReady = true; - this.walker = new Walker(root); - this.writeStats(); return root; @@ -218,12 +220,30 @@ module.exports = class IndexCreator { async getList() { if (!this.rootNode) { - this.rootNode = this.getRootNode(); + this.rootNode = this.getRootNode() + .then(rootNode => { + this.walker = new Walker(rootNode); + + return rootNode; + }); } return this.rootNode; } + async reindex() { + if (this.phase != "DONE") { + console.log("Cancelling reindex, one is already running"); + } + + this.initStats(); + + const newRootNode = await this.getRootNode() + + this.walker = new Walker(newRootNode); + this.rootNode = Promise.resolve(newRootNode); + } + async getNode(node) { const list = await this.getList(); return list.getNode(node); diff --git a/yarn.lock b/yarn.lock index e68850f..6e43ba5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5415,6 +5415,18 @@ mkdirp@^1.0.3: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +moment-timezone@^0.5.31: + version "0.5.33" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.33.tgz#b252fd6bb57f341c9b59a5ab61a8e51a73bbd22c" + integrity sha512-PTc2vcT8K9J5/9rDEPe5czSIKgLoGsH8UNpA4qZTVw0Vd/Uz19geE9abbIOQKaAQFcnQ3v5YEXrbSc5BpshH+w== + dependencies: + moment ">= 2.9.0" + +"moment@>= 2.9.0": + version "2.29.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3" + integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ== + morgan@^1.9.1: version "1.10.0" resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7" @@ -5618,6 +5630,13 @@ node-addon-api@^3.1.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.1.0.tgz#98b21931557466c6729e51cb77cd39c965f42239" integrity sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw== +node-cron@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/node-cron/-/node-cron-3.0.0.tgz#b33252803e430f9cd8590cf85738efa1497a9522" + integrity sha512-DDwIvvuCwrNiaU7HEivFDULcaQualDv7KoNlB/UU1wPW0n1tDEmBJKhEIE6DlF2FuoOHcNbLJ8ITL2Iv/3AWmA== + dependencies: + moment-timezone "^0.5.31" + node-fetch@2.6.1, node-fetch@^2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"