[AB-237] adding job to clean up the cooldown storage

This commit is contained in:
Sheldan
2021-04-24 19:07:02 +02:00
parent 53bef3fdb3
commit 40eb516ddf
4 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
package dev.sheldan.abstracto.core.command.job;
import dev.sheldan.abstracto.core.command.service.CommandCoolDownServiceBean;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Slf4j
@DisallowConcurrentExecution
@Component
@PersistJobDataAfterExecution
public class CommandCooldownMapCleanupJob extends QuartzJobBean {
@Autowired
private CommandCoolDownServiceBean commandCoolDownServiceBean;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
log.info("Executing command cooldown cleanup job.");
commandCoolDownServiceBean.cleanUpCooldownStorage();
} catch (Exception exception) {
log.error("Command cooldown cleanup job failed.", exception);
}
}
}

View File

@@ -21,9 +21,11 @@ import dev.sheldan.abstracto.core.service.management.CoolDownChannelGroupManagem
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -235,6 +237,9 @@ public class CommandCoolDownServiceBean implements CommandCoolDownService {
}
try {
Duration coolDown = getServerCoolDownForCommand(command, serverId);
if(coolDown.equals(Duration.ZERO)) {
return;
}
Instant newExecutionPoint = Instant.now().plus(coolDown);
String commandName = command.getConfiguration().getName();
Map<Long, CommandReUseMap> serverCoolDowns = storage.getServerCoolDowns();
@@ -258,6 +263,9 @@ public class CommandCoolDownServiceBean implements CommandCoolDownService {
}
try {
Duration coolDown = getChannelGroupCoolDownForCommand(command, serverIdChannelId);
if(coolDown.equals(Duration.ZERO)) {
return;
}
Instant newExecutionPoint = Instant.now().plus(coolDown);
String commandName = command.getConfiguration().getName();
Long serverId = serverIdChannelId.getServerId();
@@ -322,8 +330,11 @@ public class CommandCoolDownServiceBean implements CommandCoolDownService {
takeLock();
}
try {
Long serverId = serverChannelUserId.getGuildId();
Duration coolDown = getMemberCoolDownForCommand(command, serverChannelUserId.toServerChannelId());
if(coolDown.equals(Duration.ZERO)) {
return;
}
Long serverId = serverChannelUserId.getGuildId();
Instant newExecutionPoint = Instant.now().plus(coolDown);
String commandName = command.getConfiguration().getName();
Map<Long, Map<Long, CommandReUseMap>> serverMemberCoolDowns = storage.getMemberCoolDowns();
@@ -398,4 +409,38 @@ public class CommandCoolDownServiceBean implements CommandCoolDownService {
reUseTimes.put(commandName, newExecutionPoint);
return CommandReUseMap.builder().reUseTimes(reUseTimes).build();
}
@Transactional
public void cleanUpCooldownStorage() {
takeLock();
try {
cleanUpLongReUseMap(storage.getServerCoolDowns());
cleanUpLongLongReUseMap(storage.getMemberCoolDowns());
cleanUpLongLongReUseMap(storage.getChannelGroupCoolDowns());
} finally {
releaseLock();
}
}
private void cleanUpLongLongReUseMap(Map<Long, Map<Long, CommandReUseMap>> longLongReuseMap) {
longLongReuseMap.forEach((aLong, longCommandReUseMapMap) -> cleanUpLongReUseMap(longCommandReUseMapMap));
longLongReuseMap.entrySet().removeIf(longMapEntry -> longMapEntry.getValue().isEmpty());
}
private void cleanUpLongReUseMap(Map<Long, CommandReUseMap> map) {
map.forEach((aLong, commandReUseMap) -> cleanUpReUseMap(commandReUseMap));
map.entrySet().removeIf(longCommandReUseMapEntry -> longCommandReUseMapEntry.getValue().getReUseTimes().isEmpty());
}
private void cleanUpReUseMap(CommandReUseMap commandReUseMap) {
List<String> commandsToRemove = new ArrayList<>();
Instant now = Instant.now();
commandReUseMap.getReUseTimes().forEach((commandName, reUseTime) -> {
if(reUseTime.isBefore(now)) {
commandsToRemove.add(commandName);
}
});
log.debug("Deleting {} command mappings.", commandsToRemove.size());
commandsToRemove.forEach(commandName -> commandReUseMap.getReUseTimes().remove(commandName));
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<changeSet author="Sheldan" id="commandCooldown-cleanup-insert">
<insert tableName="scheduler_job">
<column name="name" value="commandCooldownCleanupJob"/>
<column name="group_name" value="commmand"/>
<column name="clazz" value="dev.sheldan.abstracto.core.command.job.CommandCooldownMapCleanupJob"/>
<column name="active" value="true"/>
<column name="cron_expression" value="0 0 * * * ?"/>
<column name="recovery" value="false"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -8,4 +8,5 @@
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<include file="command.xml" relativeToChangelogFile="true"/>
<include file="channel_group_type.xml" relativeToChangelogFile="true"/>
<include file="command_cooldown_cleanup_job.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>