Fixed stupid deadlock on auto reimportation
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
9aef9ac706
commit
8987fbb6aa
@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
|
||||
import xyz.vallat.louis.managers.database.DBManager;
|
||||
import xyz.vallat.louis.managers.discord.DiscordManager;
|
||||
import xyz.vallat.louis.timer.TimerManager;
|
||||
import xyz.vallat.louis.timer.tasks.AutoReimport;
|
||||
|
||||
public class App {
|
||||
|
||||
|
@ -24,62 +24,65 @@ public final class EventManager {
|
||||
|
||||
public static int reimportAllEvents() {
|
||||
int importedEvents;
|
||||
TimerManager.stopAllTimersForStudents();
|
||||
try (Connection connection = DBManager.getConnection()) {
|
||||
connection.setAutoCommit(false);
|
||||
flushEvents(connection);
|
||||
Map<Student, List<VEvent>> studentEvents = new HashMap<>();
|
||||
StudentManager.getStudentsFromDatabase()
|
||||
.forEach(s -> studentEvents.put(s, CalendarManager.getEventsFromResource(s.getAde())));
|
||||
importedEvents = importEvents(studentEvents);
|
||||
importedEvents = importEvents(studentEvents, connection);
|
||||
if (importedEvents == 0) {
|
||||
logger.error("No events were re-imported. Rolling back.");
|
||||
connection.rollback();
|
||||
} else {
|
||||
logger.debug("'{}' events reimported, commiting.", importedEvents);
|
||||
try {
|
||||
logger.debug("'{}' events reimported, commiting.", importedEvents);
|
||||
connection.commit();
|
||||
} catch (SQLException e) {
|
||||
logger.error("An SQLExcetpion occured, rolling back.", e);
|
||||
connection.rollback();
|
||||
}
|
||||
}
|
||||
TimerManager.stopAllTimersForStudents();
|
||||
TimerManager.startAllTimersForStudents();
|
||||
} catch (SQLException e) {
|
||||
logger.error("SQLError while reimporting all events:", e);
|
||||
return 0;
|
||||
}
|
||||
TimerManager.startAllTimersForStudents();
|
||||
return importedEvents;
|
||||
}
|
||||
|
||||
private static void flushEvents(Connection connection) throws SQLException {
|
||||
logger.debug("Flushing all events in database.");
|
||||
String sql = "TRUNCATE events RESTART IDENTITY;";
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
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;
|
||||
for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) {
|
||||
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
|
||||
.addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection));
|
||||
for (VEvent event : e.getValue())
|
||||
if (importEvent(e.getKey(), event, connection) != 0) importedEvents++;
|
||||
}
|
||||
return importedEvents;
|
||||
}
|
||||
|
||||
public static int importEvents(Map<Student, List<VEvent>> events) {
|
||||
try (Connection connection = DBManager.getConnection()) {
|
||||
connection.setAutoCommit(false);
|
||||
for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) {
|
||||
if (e.getValue().isEmpty()) continue;
|
||||
if (e.getKey().getId() == 0) e.getKey().setId(StudentManager
|
||||
.addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection));
|
||||
for (VEvent event : e.getValue())
|
||||
if (importEvent(e.getKey(), event, connection) != 0) importedEvents++;
|
||||
}
|
||||
try {
|
||||
connection.commit();
|
||||
} catch (SQLException e) {
|
||||
connection.rollback();
|
||||
return 0;
|
||||
}
|
||||
return importEvents(events, connection);
|
||||
} catch (SQLException e) {
|
||||
logger.error("SQLError while importing events.", e);
|
||||
return 0;
|
||||
}
|
||||
return importedEvents;
|
||||
}
|
||||
|
||||
public static List<Event> getNextEventsFromStudent(Student s) {
|
||||
|
@ -31,7 +31,7 @@ public final class TimerManager {
|
||||
public static void startAutoReimportTimer() {
|
||||
logger.debug("Setting up auto reimport timer.");
|
||||
Calendar midday = Calendar.getInstance();
|
||||
midday.set(Calendar.HOUR, 12);
|
||||
midday.set(Calendar.HOUR, 1);
|
||||
midday.set(Calendar.MINUTE, 30);
|
||||
midday.set(Calendar.SECOND, 0);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user