Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public final void enable() throws Exception {
throw new Exception("Unable to connect to database, ensure local is enabled in config and your connection details are correct");
}


setupStorage();
} catch (SQLException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void handleLocalSync() {

if (runner.isRunning()) continue;

runner.run();
runWithLifecycle(runner);
}
}

Expand All @@ -55,7 +55,20 @@ private void handleGlobalSync() {

if (runner.isRunning()) continue;

runWithLifecycle(runner);
}
}

/**
* Runs a BmRunnable with proper lifecycle management.
* Ensures beforeRun() captures DB checkpoint and afterRun() updates lastChecked.
*/
private void runWithLifecycle(BmRunnable runner) {
runner.beforeRun();
try {
runner.run();
} finally {
runner.afterRun();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,25 @@ public DatabaseTableConfig<?> getTable(String table) {
public void addTable(String key, DatabaseTableConfig<?> config) {
tables.put(key, config);
}

/**
* Returns the SQL expression for the current unix timestamp in seconds.
* This expression can be embedded directly in INSERT/UPDATE statements.
* Works with MySQL, MariaDB, and H2 (in MySQL compatibility mode).
*
* @return SQL expression that evaluates to current unix timestamp
*/
public String getTimestampNow() {
return "UNIX_TIMESTAMP()";
}

/**
* Returns the SQL query to fetch the current unix timestamp.
* Works with MySQL, MariaDB, and H2 (in MySQL compatibility mode).
*
* @return SQL query that returns the current unix timestamp
*/
public String getTimestampQuery() {
return "SELECT UNIX_TIMESTAMP()";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class SchedulesConfig extends Config {

private ConcurrentHashMap<String, Integer> schedules = new ConcurrentHashMap<>(13);
private ConcurrentHashMap<String, Long> lastChecked = new ConcurrentHashMap<>(12);
private ConcurrentHashMap<String, Long> lastRunLocal = new ConcurrentHashMap<>(12);

public SchedulesConfig(File dataFolder, CommonLogger logger) {
super(dataFolder, "schedules.yml", logger);
Expand Down Expand Up @@ -41,6 +42,15 @@ public void setLastChecked(String key, long value) {
lastChecked.put(key, value);
}

public long getLastRunLocal(String taskName) {
Long runTime = lastRunLocal.get(taskName);
return runTime != null ? runTime : 0;
}

public void setLastRunLocal(String key, long value) {
lastRunLocal.put(key, value);
}

@Override
public void afterLoad() {
for (String key : conf.getConfigurationSection("scheduler").getKeys(false)) {
Expand All @@ -50,7 +60,6 @@ public void afterLoad() {
for (String key : conf.getConfigurationSection("lastChecked").getKeys(false)) {
lastChecked.put(key, conf.getLong(("lastChecked." + key), 0));
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.Getter;
import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.configs.DatabaseConfig;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.GenericRawResults;

import java.sql.SQLException;


public abstract class BmRunnable implements Runnable {
Expand All @@ -12,31 +17,82 @@ public abstract class BmRunnable implements Runnable {
@Getter
protected long lastChecked = 0;
@Getter
protected long runCheckpoint = 0;
@Getter
protected long lastRunLocal = 0;
@Getter
protected boolean isRunning = false;

public BmRunnable(BanManagerPlugin plugin, String schedulerName) {
this.plugin = plugin;
name = schedulerName;

lastChecked = plugin.getSchedulesConfig().getLastChecked(name);
lastRunLocal = plugin.getSchedulesConfig().getLastRunLocal(name);
}

public boolean shouldExecute() {
int scheduleSeconds = plugin.getSchedulesConfig().getSchedule(name);
// Setting schedule to 0 or negative disables this task (matches schedules.yml docs)
if (scheduleSeconds <= 0) {
return false;
}
return !isRunning && (System.currentTimeMillis() / 1000L) - lastChecked > scheduleSeconds;
return !isRunning && (System.currentTimeMillis() / 1000L) - lastRunLocal > scheduleSeconds;
}

public void beforeRun() {
isRunning = true;
runCheckpoint = fetchDbTime();
}

public void afterRun() {
lastChecked = System.currentTimeMillis() / 1000L;
if (runCheckpoint > 0) {
lastChecked = runCheckpoint;
} else {
lastChecked = System.currentTimeMillis() / 1000L;
}
plugin.getSchedulesConfig().setLastChecked(name, lastChecked);

lastRunLocal = System.currentTimeMillis() / 1000L;
plugin.getSchedulesConfig().setLastRunLocal(name, lastRunLocal);

isRunning = false;
}

protected long fetchDbTime() {
try {
String query = getCheckpointDbConfig().getTimestampQuery();
GenericRawResults<String[]> results = getCheckpointDao().queryRaw(query);
String[] firstRow = results.getFirstResult();
try {
results.close();
} catch (Exception ignored) {
// Ignore close errors
}

if (firstRow == null || firstRow.length == 0 || firstRow[0] == null) {
plugin.getLogger().warning("DB time query returned no results, falling back to local time");
return 0;
}

String result = firstRow[0];

// Handle decimal results (some drivers like MariaDB return 0.0)
if (result.contains(".")) {
return Double.valueOf(result).longValue();
} else {
return Long.parseLong(result);
}
} catch (SQLException e) {
plugin.getLogger().warning("Failed to fetch DB time for sync checkpoint: " + e.getMessage());
return 0;
}
}

protected DatabaseConfig getCheckpointDbConfig() {
return plugin.getConfig().getLocalDb();
}

protected BaseDaoImpl<?, ?> getCheckpointDao() {
return plugin.getPlayerStorage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.CommonPlayer;
import me.confuser.banmanager.common.configs.DatabaseConfig;
import me.confuser.banmanager.common.data.PlayerBanData;
import me.confuser.banmanager.common.data.global.GlobalPlayerBanData;
import me.confuser.banmanager.common.data.global.GlobalPlayerBanRecordData;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.storage.PlayerBanStorage;
import me.confuser.banmanager.common.storage.global.GlobalPlayerBanRecordStorage;
Expand All @@ -28,6 +30,16 @@ public GlobalBanSync(BanManagerPlugin plugin) {
recordStorage = plugin.getGlobalPlayerBanRecordStorage();
}

@Override
protected DatabaseConfig getCheckpointDbConfig() {
return plugin.getConfig().getGlobalDb();
}

@Override
protected BaseDaoImpl<?, ?> getCheckpointDao() {
return banStorage;
}

@Override
public void run() {
newBans();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.CommonPlayer;
import me.confuser.banmanager.common.configs.DatabaseConfig;
import me.confuser.banmanager.common.data.IpBanData;
import me.confuser.banmanager.common.data.global.GlobalIpBanData;
import me.confuser.banmanager.common.data.global.GlobalIpBanRecordData;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.storage.IpBanStorage;
import me.confuser.banmanager.common.storage.global.GlobalIpBanRecordStorage;
Expand All @@ -28,6 +30,16 @@ public GlobalIpSync(BanManagerPlugin plugin) {
recordStorage = plugin.getGlobalIpBanRecordStorage();
}

@Override
protected DatabaseConfig getCheckpointDbConfig() {
return plugin.getConfig().getGlobalDb();
}

@Override
protected BaseDaoImpl<?, ?> getCheckpointDao() {
return banStorage;
}

@Override
public void run() {
newBans();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.confuser.banmanager.common.runnables;

import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.configs.DatabaseConfig;
import me.confuser.banmanager.common.data.PlayerMuteData;
import me.confuser.banmanager.common.data.global.GlobalPlayerMuteData;
import me.confuser.banmanager.common.data.global.GlobalPlayerMuteRecordData;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.storage.PlayerMuteStorage;
import me.confuser.banmanager.common.storage.global.GlobalPlayerMuteRecordStorage;
Expand All @@ -25,6 +27,16 @@ public GlobalMuteSync(BanManagerPlugin plugin) {
recordStorage = plugin.getGlobalPlayerMuteRecordStorage();
}

@Override
protected DatabaseConfig getCheckpointDbConfig() {
return plugin.getConfig().getGlobalDb();
}

@Override
protected BaseDaoImpl<?, ?> getCheckpointDao() {
return muteStorage;
}

@Override
public void run() {
newMutes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.confuser.banmanager.common.runnables;

import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.configs.DatabaseConfig;
import me.confuser.banmanager.common.data.PlayerNoteData;
import me.confuser.banmanager.common.data.global.GlobalPlayerNoteData;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.storage.PlayerNoteStorage;
import me.confuser.banmanager.common.storage.global.GlobalPlayerNoteStorage;
Expand All @@ -21,6 +23,16 @@ public GlobalNoteSync(BanManagerPlugin plugin) {
localNoteStorage = plugin.getPlayerNoteStorage();
}

@Override
protected DatabaseConfig getCheckpointDbConfig() {
return plugin.getConfig().getGlobalDb();
}

@Override
protected BaseDaoImpl<?, ?> getCheckpointDao() {
return noteStorage;
}

@Override
public void run() {
newNotes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ public void run() {
if (!runner.shouldExecute()) continue;

runner.beforeRun();

// Ensure runner exceptions are caught to still allow others to execute
try {
runner.run();
} catch (Exception e) {
// Ensure runner exceptions are caught to still allow others to execute
e.printStackTrace();
} finally {
runner.afterRun();
}

runner.afterRun();
}
}

Expand Down
Loading