Skip to content

Commit f14b3d1

Browse files
authored
Merge pull request #526 from danthe1st/fix-startup
fix exception while loading message cache on startup
2 parents aac69cd + c42cd0c commit f14b3d1

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,23 @@ public MessageCache(BotConfig botConfig, MessageCacheRepository cacheRepository,
8383
}
8484

8585
/**
86-
* Synchronizes Messages saved in the Database with what is currently stored in memory.
86+
* Synchronizes Messages saved in the Database with what is currently stored in memory. This action is executed in the background.
8787
*/
8888
public void synchronize() {
8989
asyncPool.execute(()->{
90-
cacheRepository.delete(cache.size());
91-
cacheRepository.insertList(new ArrayList<>(cache));
92-
messageCount = 0;
93-
log.info("Synchronized Database with local Cache.");
90+
synchronizeNow();
9491
});
9592
}
93+
94+
/**
95+
* Synchronizes Messages saved in the Database with what is currently stored in memory and wait until the synchronization finishes.
96+
*/
97+
public void synchronizeNow() {
98+
cacheRepository.delete(cache.size());
99+
cacheRepository.insertList(new ArrayList<>(cache));
100+
messageCount = 0;
101+
log.info("Synchronized Database with local Cache.");
102+
}
96103

97104
/**
98105
* Caches a single {@link Message} object.

src/main/java/net/discordjug/javabot/data/h2db/message_cache/dao/MessageCacheRepository.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ public int getBatchSize() {
8282
public List<CachedMessage> getAll() throws DataAccessException {
8383
List<CachedMessage> messagesWithLink = jdbcTemplate.query(
8484
"SELECT * FROM message_cache LEFT JOIN message_cache_attachments ON message_cache.message_id = message_cache_attachments.message_id",
85-
(rs, rowNum) -> this.read(rs));
85+
(rs, _) -> this.read(rs));
8686
Map<Long, CachedMessage> messages=new LinkedHashMap<>();
8787
for (CachedMessage msg : messagesWithLink) {
88-
CachedMessage previous = messages.putIfAbsent(msg.getMessageId(), msg);
89-
if(previous!=null) {
90-
previous.getAttachments().addAll(msg.getAttachments());
91-
}
88+
messages.merge(msg.getMessageId(), msg, (oldValue, value) -> {
89+
ArrayList<String> attachments = new ArrayList<>(oldValue.getAttachments());
90+
attachments.addAll(value.getAttachments());
91+
return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getMessageContent(), attachments);
92+
});
9293
}
9394
return new ArrayList<>(messages.values());
9495
}

src/main/java/net/discordjug/javabot/systems/staff_commands/RedeployCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
6363
}
6464
log.warn("Redeploying... Requested by: " + UserUtils.getUserTag(event.getUser()));
6565
event.reply("**Redeploying...** This may take some time.").queue();
66-
messageCache.synchronize();
66+
messageCache.synchronizeNow();
6767
asyncPool.shutdownNow();
6868
try {
6969
asyncPool.awaitTermination(3, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)