Cron scheduler now saves last run timestamps in a file.
This commit is contained in:
parent
4c31c0d6e4
commit
33b40f0cba
@ -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();
|
||||
}
|
||||
|
@ -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<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) {
|
||||
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.
|
||||
|
Loading…
Reference in New Issue
Block a user