From ac1527f2c4a548caefb091d99b2d8fd7684de485 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 12 Aug 2024 05:50:54 +0200 Subject: [PATCH] Added CrawlCache expired feature. --- .../statoolinfosweb/CrawlCacheChecker.java | 112 ++++++++++++++++++ .../statoolinfosweb/StatoolInfosWeb.java | 35 ++++++ 2 files changed, 147 insertions(+) create mode 100644 src/fr/devinsy/statoolinfosweb/CrawlCacheChecker.java diff --git a/src/fr/devinsy/statoolinfosweb/CrawlCacheChecker.java b/src/fr/devinsy/statoolinfosweb/CrawlCacheChecker.java new file mode 100644 index 0000000..6c5d756 --- /dev/null +++ b/src/fr/devinsy/statoolinfosweb/CrawlCacheChecker.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2021-2024 Christian Pierre MOMON christian.momon@devinsy.fr>> + * + * This file is part of StatoolInfosWeb, webapp to value service statistics. + * + * StatoolInfosWeb is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * StatoolInfosWeb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with StatoolInfosWeb. If not, see . + */ +package fr.devinsy.statoolinfosweb; + +import jakarta.servlet.ServletContextEvent; +import jakarta.servlet.ServletContextListener; +import jakarta.servlet.annotation.WebListener; + +/** + * The Class Checker. + */ +@WebListener +public class CrawlCacheChecker implements ServletContextListener +{ + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CrawlCacheChecker.class); + + private static final int CHECK_DELAY = 60 * 1000; + + private Thread checker; + + @Override + public void contextInitialized(ServletContextEvent event) + { + logger.info("Starting checker thread…"); + this.checker = new Thread() + { + @Override + public void run() + { + // while (!Thread.currentThread().isInterrupted()) + // { + // try + // { + // Thread.sleep(CHECK_DELAY); + // StatoolInfosWeb.instance().check(); + // } + // catch (InterruptedException exception) + // { + // Thread.currentThread().interrupt(); + // logger.error("Check sleep interrupted so ending checker: {}", + // exception.getMessage()); + // } + // } + // + // boolean ended = false; + // while (!ended) + // { + // try + // { + // Thread.sleep(CHECK_DELAY); + // StatoolInfosWeb.instance().check(); + // if (Thread.currentThread().isInterrupted()) + // { + // logger.error("Interrupted status detected so ending + // checker."); + // ended = true; + // } + // } + // catch (InterruptedException exception) + // { + // logger.error("Check sleep interrupted so ending checker: {}", + // exception.getMessage()); + // ended = true; + // } + // } + + try + { + boolean ended = false; + while (!ended) + { + Thread.sleep(CHECK_DELAY); + StatoolInfosWeb.instance().check(); + if (Thread.currentThread().isInterrupted()) + { + logger.error("Interrupted status detected so ending checker."); + ended = true; + } + } + } + catch (InterruptedException exception) + { + logger.error("Check sleep interrupted so ending checker: {}", exception.getMessage()); + } + } + }; + this.checker.start(); + } + + @Override + public void contextDestroyed(ServletContextEvent event) + { + logger.info("Stopping checker thread."); + this.checker.interrupt(); + } +} diff --git a/src/fr/devinsy/statoolinfosweb/StatoolInfosWeb.java b/src/fr/devinsy/statoolinfosweb/StatoolInfosWeb.java index 8796ef3..d523a04 100644 --- a/src/fr/devinsy/statoolinfosweb/StatoolInfosWeb.java +++ b/src/fr/devinsy/statoolinfosweb/StatoolInfosWeb.java @@ -47,6 +47,7 @@ import fr.devinsy.statoolinfos.core.Organization; import fr.devinsy.statoolinfos.core.Service; import fr.devinsy.statoolinfos.core.Software; import fr.devinsy.statoolinfos.core.StatoolInfosContext; +import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.crawl.CrawlCache; import fr.devinsy.statoolinfos.uptime.UptimeJournal; import fr.devinsy.xidyn.XidynException; @@ -179,6 +180,40 @@ public class StatoolInfosWeb } } + /** + * Check. + */ + public void check() + { + try + { + if (this.context.isExpired()) + { + logger.info("Context expired, reloading context…"); + StatoolInfosContext newContext = new StatoolInfosContext(this.configurationFile); + this.context = newContext; + clear(); + } + else + { + logger.info("Context expiration not detected."); + } + } + catch (StatoolInfosException | IOException exception) + { + logger.error("Context update failed: {}", exception.getMessage()); + exception.printStackTrace(); + } + } + + /** + * Clear caches. + */ + public void clear() + { + KissDispatcher.instance().clear(); + } + /** * Gets the builds the information. *