mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-11 19:02:16 +01:00
Created ServerShutdownTransaction
This commit is contained in:
parent
11369fe62d
commit
40a62874a3
@ -22,6 +22,7 @@ import com.djrapitops.plan.api.exceptions.database.DBOpException;
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.db.Database;
|
||||
import com.djrapitops.plan.db.access.transactions.events.ServerShutdownTransaction;
|
||||
import com.djrapitops.plan.system.cache.SessionCache;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
@ -77,39 +78,49 @@ public class ShutdownHook extends Thread {
|
||||
try {
|
||||
Map<UUID, Session> activeSessions = SessionCache.getActiveSessions();
|
||||
long now = System.currentTimeMillis();
|
||||
saveActiveSessions(activeSessions, now);
|
||||
prepareSessionsForStorage(activeSessions, System.currentTimeMillis());
|
||||
saveActiveSessions(activeSessions);
|
||||
} catch (IllegalStateException ignored) {
|
||||
/* Database is not initialized */
|
||||
} catch (DBInitException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
} finally {
|
||||
try {
|
||||
dbSystem.getDatabase().close();
|
||||
} catch (DBException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
}
|
||||
closeDatabase(dbSystem.getDatabase());
|
||||
}
|
||||
}
|
||||
|
||||
private void saveActiveSessions(Map<UUID, Session> activeSessions, long now) throws DBInitException {
|
||||
for (Map.Entry<UUID, Session> entry : activeSessions.entrySet()) {
|
||||
UUID uuid = entry.getKey();
|
||||
Session session = entry.getValue();
|
||||
private void saveActiveSessions(Map<UUID, Session> activeSessions) throws DBInitException {
|
||||
Database database = dbSystem.getDatabase();
|
||||
if (!database.isOpen()) {
|
||||
// Ensure that database is not closed when performing the transaction.
|
||||
database.init();
|
||||
}
|
||||
|
||||
saveSessions(activeSessions, database);
|
||||
}
|
||||
|
||||
private void prepareSessionsForStorage(Map<UUID, Session> activeSessions, long now) {
|
||||
for (Session session : activeSessions.values()) {
|
||||
Optional<Long> end = session.getValue(SessionKeys.END);
|
||||
if (!end.isPresent()) {
|
||||
session.endSession(now);
|
||||
}
|
||||
Database database = dbSystem.getDatabase();
|
||||
if (!database.isOpen()) {
|
||||
database.init();
|
||||
}
|
||||
// TODO Create a server shutdown transaction.
|
||||
try {
|
||||
database.save().session(uuid, session);
|
||||
} catch (DBOpException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
}
|
||||
}
|
||||
activeSessions.clear();
|
||||
}
|
||||
|
||||
private void saveSessions(Map<UUID, Session> activeSessions, Database database) {
|
||||
try {
|
||||
database.executeTransaction(new ServerShutdownTransaction(activeSessions.values()));
|
||||
} catch (DBOpException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeDatabase(Database database) {
|
||||
try {
|
||||
database.close();
|
||||
} catch (DBException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -329,7 +329,7 @@ public class LargeStoreQueries {
|
||||
};
|
||||
}
|
||||
|
||||
public static Executable storeAllSessionsWithoutKillOrWorldData(List<Session> sessions) {
|
||||
public static Executable storeAllSessionsWithoutKillOrWorldData(Collection<Session> sessions) {
|
||||
if (Verify.isEmpty(sessions)) {
|
||||
return Executable.empty();
|
||||
}
|
||||
@ -351,7 +351,7 @@ public class LargeStoreQueries {
|
||||
};
|
||||
}
|
||||
|
||||
public static Executable storeAllSessionsWithKillAndWorldData(List<Session> sessions) {
|
||||
public static Executable storeAllSessionsWithKillAndWorldData(Collection<Session> sessions) {
|
||||
return connection -> {
|
||||
storeAllSessionsWithoutKillOrWorldData(sessions).execute(connection);
|
||||
storeSessionKillData(sessions).execute(connection);
|
||||
@ -359,7 +359,7 @@ public class LargeStoreQueries {
|
||||
};
|
||||
}
|
||||
|
||||
private static Executable storeSessionKillData(List<Session> sessions) {
|
||||
private static Executable storeSessionKillData(Collection<Session> sessions) {
|
||||
if (Verify.isEmpty(sessions)) {
|
||||
return Executable.empty();
|
||||
}
|
||||
@ -374,7 +374,7 @@ public class LargeStoreQueries {
|
||||
};
|
||||
}
|
||||
|
||||
private static Executable storeSessionWorldTimeData(List<Session> sessions) {
|
||||
private static Executable storeSessionWorldTimeData(Collection<Session> sessions) {
|
||||
if (Verify.isEmpty(sessions)) {
|
||||
return Executable.empty();
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.db.access.transactions.events;
|
||||
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.db.access.queries.LargeStoreQueries;
|
||||
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Transaction to store sessions on server shutdown.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ServerShutdownTransaction extends Transaction {
|
||||
|
||||
private final Collection<Session> unsavedSessions;
|
||||
|
||||
public ServerShutdownTransaction(Collection<Session> unsavedSessions) {
|
||||
this.unsavedSessions = unsavedSessions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllSessionsWithKillAndWorldData(unsavedSessions));
|
||||
}
|
||||
}
|
@ -16,14 +16,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.database.databases.operation;
|
||||
|
||||
import com.djrapitops.plan.data.container.GeoInfo;
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.container.UserInfo;
|
||||
import com.djrapitops.plan.data.store.objects.Nickname;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -38,30 +32,6 @@ public interface SaveOperations {
|
||||
|
||||
// Bulk save
|
||||
|
||||
@Deprecated
|
||||
void insertTPS(Map<UUID, List<TPS>> ofServers);
|
||||
|
||||
@Deprecated
|
||||
void insertCommandUsage(Map<UUID, Map<String, Integer>> ofServers);
|
||||
|
||||
@Deprecated
|
||||
void insertUsers(Map<UUID, UserInfo> ofServers);
|
||||
|
||||
@Deprecated
|
||||
void insertSessions(Map<UUID, Map<UUID, List<Session>>> ofServers, boolean containsExtraData);
|
||||
|
||||
@Deprecated
|
||||
void kickAmount(Map<UUID, Integer> ofUsers);
|
||||
|
||||
@Deprecated
|
||||
void insertUserInfo(Map<UUID, List<UserInfo>> ofServers);
|
||||
|
||||
@Deprecated
|
||||
void insertNicknames(Map<UUID, Map<UUID, List<Nickname>>> ofServers);
|
||||
|
||||
@Deprecated
|
||||
void insertAllGeoInfo(Map<UUID, List<GeoInfo>> ofUsers);
|
||||
|
||||
// Single data point
|
||||
|
||||
@Deprecated
|
||||
|
@ -16,19 +16,13 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.database.databases.sql.operation;
|
||||
|
||||
import com.djrapitops.plan.data.container.*;
|
||||
import com.djrapitops.plan.data.store.objects.Nickname;
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.queries.DataStoreQueries;
|
||||
import com.djrapitops.plan.db.access.queries.LargeStoreQueries;
|
||||
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* SaveOperations implementation for SQL databases.
|
||||
@ -41,90 +35,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
|
||||
super(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertTPS(Map<UUID, List<TPS>> ofServers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllTPSData(ofServers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertCommandUsage(Map<UUID, Map<String, Integer>> ofServers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllCommandUsageData(ofServers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertUsers(Map<UUID, UserInfo> ofUsers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
Collection<BaseUser> users = ofUsers.values().stream()
|
||||
.map(user -> new BaseUser(user.getPlayerUuid(), user.getPlayerUuid().toString(), user.getRegistered(), 0))
|
||||
.collect(Collectors.toSet());
|
||||
execute(LargeStoreQueries.storeAllCommonUserInformation(users));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> ofServers, boolean containsExtraData) {
|
||||
List<Session> sessions = ofServers.values().stream()
|
||||
.map(Map::values)
|
||||
.flatMap(Collection::stream)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(containsExtraData ? LargeStoreQueries.storeAllSessionsWithKillAndWorldData(sessions)
|
||||
: LargeStoreQueries.storeAllSessionsWithoutKillOrWorldData(sessions));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kickAmount(Map<UUID, Integer> ofUsers) {
|
||||
// Moved to BaseUser storage methods
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertUserInfo(Map<UUID, List<UserInfo>> ofServers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storePerServerUserInformation(ofServers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertNicknames(Map<UUID, Map<UUID, List<Nickname>>> ofServers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllNicknameData(ofServers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertAllGeoInfo(Map<UUID, List<GeoInfo>> ofUsers) {
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllGeoInformation(ofUsers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void banStatus(UUID uuid, boolean banned) {
|
||||
userInfoTable.updateBanStatus(uuid, banned);
|
||||
|
Loading…
Reference in New Issue
Block a user