Cron scheduler now saves last run timestamps in a file.

This commit is contained in:
Marc Baloup 2023-01-31 20:04:59 +01:00
parent 4c31c0d6e4
commit 33b40f0cba
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 55 additions and 7 deletions

View File

@ -13,7 +13,6 @@ import java.util.HashMap;
import java.util.Map;
public class Persist {
protected final BackupManager backupManager;
private Map<String, Long> dirtySince = new HashMap<>();
@ -22,7 +21,6 @@ public class Persist {
// private final Set<String> dirtyWorldsSave = new HashSet<>();
public Persist(BackupManager bm) {
backupManager = bm;
file = new File(bm.getBackupDirectory(), "source-dirty-since.json");
load();
}

View File

@ -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,24 +150,67 @@ public class CronScheduler {
private static final Map<String, Long> 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<String, Long> newData = Json.gson.fromJson(reader, new TypeToken<Map<String, Long>>(){}.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) {
synchronized (lock) {
savedLastRun.put(taskId, lastRun);
saveLastRuns();
}
}
private static long getLastRun(String taskId) {
synchronized (lock) {
return savedLastRun.getOrDefault(taskId, System.currentTimeMillis());
}
}
/**