Skip to content

Commit

Permalink
Add automatic reindexing using a Cron
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed May 2, 2021
1 parent 7d8eb17 commit 9e50357
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions server/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * *';
13 changes: 12 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();

Expand Down
30 changes: 25 additions & 5 deletions server/tree/IndexCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
Expand Down Expand Up @@ -209,21 +213,37 @@ module.exports = class IndexCreator {
this.phase = "DONE";
this.isReady = true;

this.walker = new Walker(root);

this.writeStats();

return root;
}

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);
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

[email protected], node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
Expand Down

0 comments on commit 9e50357

Please sign in to comment.