Refactored how the music handler was being referenced
Some checks failed
Locusworks Team/eight-track/pipeline/head There was a failure building this commit

This commit is contained in:
Isaac Parenteau
2020-01-06 21:22:54 -06:00
parent 4c68e6bf09
commit 2f89362a8b
25 changed files with 169 additions and 120 deletions

View File

@@ -2,7 +2,7 @@
*
* Project: Eight Track, File: DatabaseCleanupService.java
*
* Copyright 2019-2019 Locusworks LLC.
* Copyright 2019-2020 Locusworks LLC.
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
* any means and imposes fines up to $25,000 for violation. No part of this material
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
@@ -27,11 +27,7 @@
*/
package net.locusworks.discord.eighttrack.services;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@@ -41,10 +37,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.locusworks.discord.eighttrack.database.repos.GuildRepository;
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
@Service
public class DatabaseCleanupService {
@@ -59,58 +52,10 @@ public class DatabaseCleanupService {
@Autowired
private GuildRepository guildRepo;
private JDA client;
public void setClient(JDA client) {
this.client = client;
}
/**
* Close out connections that are no long being played. clean up resources
*/
@Scheduled(fixedRate = 5 * MINUTE)
private void checkPlayers() {
if (client == null) {
logger.warn("Discord client is null. Unable to do cleanup");
return;
}
logger.debug("Checking players to see if anyone is listening");
OffsetDateTime now = OffsetDateTime.now();
for(Iterator<Entry<Long, GuildMusicHandler>> iterator = GuildMusicService.getMap().entrySet().iterator(); iterator.hasNext();) {
Entry<Long, GuildMusicHandler> entry = iterator.next();
GuildMusicHandler gmh = entry.getValue();
long guildId = entry.getKey();
Guild guild = client.getGuildById(guildId);
if (guild == null) {
iterator.remove();
continue;
}
OffsetDateTime lastPlayed = gmh.getLastPlayed();
Duration duration = Duration.between(lastPlayed, now);
if (duration.toMillis() > 5 * MINUTE) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
}
Long voiceChannelId = gmh.getCurrentVoiceChannelId();
if (voiceChannelId == null) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
} else {
VoiceChannel vc = guild.getVoiceChannelById(voiceChannelId);
if (vc != null && vc.getMembers().size() == 1) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
}
}
}
}
@Scheduled(fixedRate = DAY)
private void cleanDatabase() {
JDA client = EightTrackService.getClient();
if (client == null) {
logger.warn("Discord client is null. Unable to do cleanup");
return;