Initial Database Migration

This commit is contained in:
Isaac Parenteau
2019-10-06 20:07:47 -05:00
parent 38dbc5bbf5
commit af8e8fa52a
20 changed files with 1567 additions and 113 deletions

View File

@@ -0,0 +1,113 @@
/**
*
* Project: Eight Track, File: DatabaseCleanupService.java
*
* Copyright 2019 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,
* modified, duplicated, adapted or translated into another program language in any
* form or by any means, electronic, mechanical, photocopying, recording, or
* otherwise, without the prior written permission from Locusworks. Locusworks
* affirms that Eight-Track(R) software and data is subject to United States
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
*
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
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;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.locusworks.discord.eighttrack.database.repos.GuildRepository;
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
@Service
public class DatabaseCleanupService {
private static final long SECOND = 1000;
private static final long MINUTE = 60 * SECOND;
private static final long HOUR = 60 * MINUTE;
private static final long DAY = 24 * HOUR;
private Logger logger = LoggerFactory.getLogger(DatabaseCleanupService.class);
@Autowired
private GuildRepository guildRepo;
@Autowired
private GuildMusicService musicService;
private JDA client;
public void setClient(JDA client) {
this.client = client;
}
@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 = musicService.entrySet().iterator(); iterator.hasNext();) {
Entry<Long, GuildMusicHandler> entry = iterator.next();
GuildMusicHandler gmh = entry.getValue();
long guildId = entry.getKey();
OffsetDateTime lastPlayed = gmh.getLastPlayed();
Duration duration = Duration.between(lastPlayed, now);
if (duration.getSeconds() > 300) {
Guild guild = client.getGuildById(guildId);
if (guild == null) {
iterator.remove();
continue;
}
guild.getAudioManager().closeAudioConnection();
iterator.remove();
}
}
}
@Scheduled(fixedRate = DAY)
private void cleanDatabase() {
if (client == null) {
logger.warn("Discord client is null. Unable to do cleanup");
return;
}
Set<Long> currentGuildsIds = client.getSelfUser().getMutualGuilds().stream().map(guild -> guild.getIdLong()).collect(Collectors.toSet());
try {
guildRepo.deleteGuildsNoLongerJoined(currentGuildsIds);
} catch (Exception ex) {
logger.error("Unable to clean up servers: " + ex.getMessage());
}
}
}