Shutdown Hook

This commit is contained in:
Rsl1122 2017-09-09 15:52:55 +03:00
parent 4a246d4c5c
commit f632657102
8 changed files with 151 additions and 9 deletions

View File

@ -48,6 +48,7 @@ import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.metrics.BStats;
import org.bukkit.ChatColor;
import java.util.HashSet;
@ -192,13 +193,13 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
hookHandler = new HookHandler(this);
Benchmark.stop("Enable", "Hook to 3rd party plugins");
//Analytics temporarily disabled TODO enable before release
// BStats bStats = new BStats(this);
// bStats.registerMetrics();
BStats bStats = new BStats(this);
bStats.registerMetrics();
Log.debug("Verbose debug messages are enabled.");
Log.logDebug("Enable", Benchmark.stop("Enable", "Enable"));
Log.info(Locale.get(Msg.ENABLED).toString());
new ShutdownHook(this);
} catch (Exception e) {
Log.error("Plugin Failed to Initialize Correctly.");
Log.logStackTrace(e);

View File

@ -0,0 +1,95 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.tables.Actions;
import main.java.com.djrapitops.plan.database.tables.SessionsTable;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class ShutdownHook extends Thread {
private static boolean active = false;
private static DataCache dataCache;
private static SQLDB db;
public ShutdownHook(Plan plugin) {
if (!active) {
Runtime.getRuntime().addShutdownHook(this);
}
active = true;
db = (SQLDB) plugin.getDB();
dataCache = plugin.getDataCache();
}
@Override
public void run() {
try {
Map<UUID, Session> activeSessions = dataCache.getActiveSessions();
long now = MiscUtils.getTime();
if (db == null) {
return;
}
if (!db.isOpen()) {
db.init();
}
saveFirstSessionInformation(now);
saveActiveSessions(activeSessions, now);
} catch (DatabaseInitException e) {
Log.toLog(this.getClass().getName(), e);
} finally {
if (db != null) {
try {
db.close();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
db = null;
dataCache = null;
}
}
private void saveFirstSessionInformation(long now) {
try {
for (Map.Entry<UUID, Integer> entry : dataCache.getFirstSessionMsgCounts().entrySet()) {
UUID uuid = entry.getKey();
int messagesSent = entry.getValue();
db.getActionsTable().insertAction(uuid, new Action(now, Actions.FIRST_LOGOUT, "Messages sent: " + messagesSent));
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
private void saveActiveSessions(Map<UUID, Session> activeSessions, long now) {
SessionsTable sessionsTable = db.getSessionsTable();
for (Map.Entry<UUID, Session> entry : activeSessions.entrySet()) {
UUID uuid = entry.getKey();
Session session = entry.getValue();
session.endSession(now);
try {
sessionsTable.saveSession(uuid, session);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}
}

View File

@ -138,4 +138,8 @@ public class DataCache extends SessionCache {
public Set<UUID> getUuids() {
return playerNames.keySet();
}
public Map<UUID, Integer> getFirstSessionMsgCounts() {
return firstSessionInformation;
}
}

View File

@ -121,9 +121,8 @@ public class PlanPlayerListener implements Listener {
new EndSessionProcessor(uuid, time)
);
int messagesSent = plugin.getDataCache().getFirstSessionMsgCount(uuid);
if (cache.isFirstSession(uuid)) {
int messagesSent = plugin.getDataCache().getFirstSessionMsgCount(uuid);
plugin.addToProcessQueue(new FirstLeaveProcessor(uuid, time, messagesSent));
}
}

View File

@ -171,7 +171,14 @@ public class Analysis {
return;
}
if (analysisTypes.contains(AnalysisType.HTML)) {
replaceMap.put(source.getPlaceholderName(AnalysisType.HTML.getPlaceholderModifier()), source.getHtmlReplaceValue(AnalysisType.HTML.getModifier(), UUID.randomUUID()));
String html = source.getHtmlReplaceValue(AnalysisType.HTML.getModifier(), UUID.randomUUID());
String placeholderName = source.getPlaceholderName(AnalysisType.HTML.getPlaceholderModifier());
int length = html.length();
if (length < 20000) {
replaceMap.put(placeholderName, html);
} else {
replaceMap.put(placeholderName, "<p>Html was removed because it contained too many characters to be responsive (" + length + "/20000)</p>");
}
return;
}
for (AnalysisType type : totalTypes) {

View File

@ -43,7 +43,7 @@
</a>
</div>
<div id="limiter" class="main-limiter">
<div id="main" class="main-wrapper">
<div id="main" class="main-wrapper" style="width: 400%;">
<div id="tab-information" class="tab">
<div class="row">
<div class="column">
@ -109,7 +109,7 @@
<h2><i class="fa fa-braille" aria-hidden="true"></i> Join PunchCard</h2>
</div>
<div class="box-footer">
<div id="punchcard" style="width: 75%; height: 400px;"></div>
<div id="punchcard" style="width: 100%; height: 400px;"></div>
</div>
<div class="box-header">
<h2><i class="fa fa-pie-chart" aria-hidden="true"></i> Server Preference</h2>
@ -208,6 +208,7 @@
data: ${punchCardSeries}
};
</script>
<script>
var navButtons = document.getElementsByClassName("nav-button");
@ -259,6 +260,7 @@
}
</script>
</div>
</body>

View File

@ -1,7 +1,7 @@
name: Plan
author: Rsl1122
main: main.java.com.djrapitops.plan.Plan
version: 4.0.0
version: 3.9.9
softdepend:
- OnTime
- EssentialsX

View File

@ -0,0 +1,34 @@
package main.java.com.djrapitops.plan.systems.cache;
import main.java.com.djrapitops.plan.data.Session;
import org.junit.Test;
import test.java.utils.MockUtils;
import java.util.Optional;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class SessionCacheTest {
private final UUID uuid = MockUtils.getPlayerUUID();
@Test
public void testAtomity() {
SessionCache sessionCache = new SessionCache(null);
Session session = new Session(12345L, "World1", "SURVIVAL");
sessionCache.cacheSession(uuid, session);
SessionCache reloaded = new SessionCache(null);
Optional<Session> cachedSession = reloaded.getCachedSession(uuid);
assertTrue(cachedSession.isPresent());
assertEquals(session, cachedSession.get());
}
}