Fixed stupid deadlock on auto reimportation

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-27 13:41:40 +01:00
parent 9aef9ac706
commit 8987fbb6aa
3 changed files with 25 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
import xyz.vallat.louis.managers.database.DBManager; import xyz.vallat.louis.managers.database.DBManager;
import xyz.vallat.louis.managers.discord.DiscordManager; import xyz.vallat.louis.managers.discord.DiscordManager;
import xyz.vallat.louis.timer.TimerManager; import xyz.vallat.louis.timer.TimerManager;
import xyz.vallat.louis.timer.tasks.AutoReimport;
public class App { public class App {

View File

@ -24,62 +24,65 @@ public final class EventManager {
public static int reimportAllEvents() { public static int reimportAllEvents() {
int importedEvents; int importedEvents;
TimerManager.stopAllTimersForStudents();
try (Connection connection = DBManager.getConnection()) { try (Connection connection = DBManager.getConnection()) {
connection.setAutoCommit(false); connection.setAutoCommit(false);
flushEvents(connection); flushEvents(connection);
Map<Student, List<VEvent>> studentEvents = new HashMap<>(); Map<Student, List<VEvent>> studentEvents = new HashMap<>();
StudentManager.getStudentsFromDatabase() StudentManager.getStudentsFromDatabase()
.forEach(s -> studentEvents.put(s, CalendarManager.getEventsFromResource(s.getAde()))); .forEach(s -> studentEvents.put(s, CalendarManager.getEventsFromResource(s.getAde())));
importedEvents = importEvents(studentEvents); importedEvents = importEvents(studentEvents, connection);
if (importedEvents == 0) { if (importedEvents == 0) {
logger.error("No events were re-imported. Rolling back."); logger.error("No events were re-imported. Rolling back.");
connection.rollback(); connection.rollback();
} else { } else {
logger.debug("'{}' events reimported, commiting.", importedEvents);
try { try {
logger.debug("'{}' events reimported, commiting.", importedEvents);
connection.commit(); connection.commit();
} catch (SQLException e) { } catch (SQLException e) {
logger.error("An SQLExcetpion occured, rolling back.", e);
connection.rollback(); connection.rollback();
} }
} }
TimerManager.stopAllTimersForStudents();
TimerManager.startAllTimersForStudents();
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLError while reimporting all events:", e); logger.error("SQLError while reimporting all events:", e);
return 0; return 0;
} }
TimerManager.startAllTimersForStudents();
return importedEvents; return importedEvents;
} }
private static void flushEvents(Connection connection) throws SQLException { private static void flushEvents(Connection connection) throws SQLException {
logger.debug("Flushing all events in database.");
String sql = "TRUNCATE events RESTART IDENTITY;"; String sql = "TRUNCATE events RESTART IDENTITY;";
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql); stmt.executeUpdate(sql);
} }
} }
public static int importEvents(Map<Student, List<VEvent>> events) { public static int importEvents(Map<Student, List<VEvent>> events, Connection connection) throws SQLException {
int importedEvents = 0; int importedEvents = 0;
try (Connection connection = DBManager.getConnection()) {
connection.setAutoCommit(false);
for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) { for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) {
if (e.getValue().isEmpty()) continue; logger.debug("Importing {} events for student '{}' in database.", e.getValue().size(), e.getKey().getAde());
if (e.getValue().isEmpty()) {
logger.warn("Student '{}' has no event, apparently.", e.getKey().getAde());
continue;
}
if (e.getKey().getId() == 0) e.getKey().setId(StudentManager if (e.getKey().getId() == 0) e.getKey().setId(StudentManager
.addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection)); .addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection));
for (VEvent event : e.getValue()) for (VEvent event : e.getValue())
if (importEvent(e.getKey(), event, connection) != 0) importedEvents++; if (importEvent(e.getKey(), event, connection) != 0) importedEvents++;
} }
try { return importedEvents;
connection.commit();
} catch (SQLException e) {
connection.rollback();
return 0;
} }
public static int importEvents(Map<Student, List<VEvent>> events) {
try (Connection connection = DBManager.getConnection()) {
return importEvents(events, connection);
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLError while importing events.", e); logger.error("SQLError while importing events.", e);
return 0; return 0;
} }
return importedEvents;
} }
public static List<Event> getNextEventsFromStudent(Student s) { public static List<Event> getNextEventsFromStudent(Student s) {

View File

@ -31,7 +31,7 @@ public final class TimerManager {
public static void startAutoReimportTimer() { public static void startAutoReimportTimer() {
logger.debug("Setting up auto reimport timer."); logger.debug("Setting up auto reimport timer.");
Calendar midday = Calendar.getInstance(); Calendar midday = Calendar.getInstance();
midday.set(Calendar.HOUR, 12); midday.set(Calendar.HOUR, 1);
midday.set(Calendar.MINUTE, 30); midday.set(Calendar.MINUTE, 30);
midday.set(Calendar.SECOND, 0); midday.set(Calendar.SECOND, 0);