From 33b40f0cbab0da91d6df7cc7a451c839e239403d Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 31 Jan 2023 20:04:59 +0100 Subject: [PATCH] Cron scheduler now saves last run timestamps in a file. --- .../fr/pandacube/lib/core/backup/Persist.java | 2 - .../lib/core/cron/CronScheduler.java | 60 +++++++++++++++++-- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/Persist.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/Persist.java index e3b2883..75625c4 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/Persist.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/Persist.java @@ -13,7 +13,6 @@ import java.util.HashMap; import java.util.Map; public class Persist { - protected final BackupManager backupManager; private Map dirtySince = new HashMap<>(); @@ -22,7 +21,6 @@ public class Persist { // private final Set dirtyWorldsSave = new HashSet<>(); public Persist(BackupManager bm) { - backupManager = bm; file = new File(bm.getBackupDirectory(), "source-dirty-since.json"); load(); } diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/cron/CronScheduler.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/cron/CronScheduler.java index afe5002..c151c7e 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/cron/CronScheduler.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/cron/CronScheduler.java @@ -1,8 +1,15 @@ package fr.pandacube.lib.core.cron; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; import fc.cron.CronExpression; +import fr.pandacube.lib.core.json.Json; import fr.pandacube.lib.util.Log; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -143,26 +150,69 @@ public class CronScheduler { + + + + + private static final Map savedLastRun = new LinkedHashMap<>(); + private static final File lastRunFile = new File("cron-last-run.json"); private static void saveLastRuns() { - // TODO + try (FileWriter writer = new FileWriter(lastRunFile, false)) { + synchronized (lock) { + Json.gsonPrettyPrinting.toJson(savedLastRun, writer); + } + } + catch (JsonParseException | IOException e) { + Log.severe("could not save " + lastRunFile, e); + } } private static void loadLastRuns() { - // TODO + boolean loaded = false; + try (FileReader reader = new FileReader(lastRunFile)) { + Map newData = Json.gson.fromJson(reader, new TypeToken>(){}.getType()); + if (newData != null) { + loaded = true; + synchronized (lock) { + savedLastRun.clear(); + savedLastRun.putAll(newData); + } + } + } + catch (final IOException ignored) { } + catch (final JsonParseException e) { + Log.severe("cannot load " + lastRunFile, e); + } + finally { + } + + if (!loaded) { + saveLastRuns(); + } } /* package */ static void setLastRun(String taskId, long lastRun) { - savedLastRun.put(taskId, lastRun); - saveLastRuns(); + synchronized (lock) { + savedLastRun.put(taskId, lastRun); + saveLastRuns(); + } } private static long getLastRun(String taskId) { - return savedLastRun.getOrDefault(taskId, System.currentTimeMillis()); + synchronized (lock) { + return savedLastRun.getOrDefault(taskId, System.currentTimeMillis()); + } } + + + + + + /** * Tells when the next time is scheduled, according to the provided cron expression, strictly after the provided time. * @param expr the cron expression to use to determine the schedule time.