mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-02-14 23:48:26 -06:00
Creator/RAM cleanup!!! Thanks call from reminding me a thousand times.
This commit is contained in:
@@ -33,6 +33,8 @@ public class Announcement {
|
||||
|
||||
private boolean editing;
|
||||
|
||||
private long lastEdit;
|
||||
|
||||
/**
|
||||
* Use this constructor when creating NEW announcements!!!
|
||||
*
|
||||
@@ -48,6 +50,8 @@ public class Announcement {
|
||||
hoursBefore = 0;
|
||||
minutesBefore = 0;
|
||||
info = "None";
|
||||
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,6 +72,7 @@ public class Announcement {
|
||||
info = "None";
|
||||
|
||||
editing = false;
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public Announcement(Announcement from) {
|
||||
@@ -85,6 +90,7 @@ public class Announcement {
|
||||
setSubscriberUserIdsFromString(from.getSubscriberUserIdString());
|
||||
|
||||
editing = false;
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public Announcement(Announcement from, boolean copyId) {
|
||||
@@ -106,6 +112,7 @@ public class Announcement {
|
||||
setSubscriberUserIdsFromString(from.getSubscriberUserIdString());
|
||||
|
||||
editing = false;
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
//Getters
|
||||
@@ -250,6 +257,10 @@ public class Announcement {
|
||||
return editing;
|
||||
}
|
||||
|
||||
public long getLastEdit() {
|
||||
return lastEdit;
|
||||
}
|
||||
|
||||
//Setters
|
||||
|
||||
/**
|
||||
@@ -333,6 +344,10 @@ public class Announcement {
|
||||
editing = _editing;
|
||||
}
|
||||
|
||||
public void setLastEdit(long _lastEdit) {
|
||||
lastEdit = _lastEdit;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,6 +20,8 @@ public class PreCalendar {
|
||||
|
||||
private IMessage creatorMessage;
|
||||
|
||||
private long lastEdit;
|
||||
|
||||
/**
|
||||
* Creates a new PreCalendar for the Guild.
|
||||
*
|
||||
@@ -31,6 +33,8 @@ public class PreCalendar {
|
||||
summary = _summary;
|
||||
|
||||
editing = false;
|
||||
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public PreCalendar(long _guildId, Calendar calendar) {
|
||||
@@ -45,6 +49,8 @@ public class PreCalendar {
|
||||
}
|
||||
|
||||
editing = false;
|
||||
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
//Getters
|
||||
@@ -97,6 +103,10 @@ public class PreCalendar {
|
||||
return creatorMessage;
|
||||
}
|
||||
|
||||
public long getLastEdit() {
|
||||
return lastEdit;
|
||||
}
|
||||
|
||||
//Setters
|
||||
|
||||
/**
|
||||
@@ -138,6 +148,10 @@ public class PreCalendar {
|
||||
creatorMessage = _message;
|
||||
}
|
||||
|
||||
public void setLastEdit(long _lastEdit) {
|
||||
lastEdit = _lastEdit;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,6 +45,8 @@ public class PreEvent {
|
||||
|
||||
private IMessage creatorMessage;
|
||||
|
||||
private long lastEdit;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new PreEvent for the specified Guild.
|
||||
@@ -65,6 +67,7 @@ public class PreEvent {
|
||||
eventData = new EventData(guildId);
|
||||
|
||||
editing = false;
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public PreEvent(long _guildId, Event e) {
|
||||
@@ -130,6 +133,7 @@ public class PreEvent {
|
||||
eventData = DatabaseManager.getManager().getEventData(guildId, e.getId());
|
||||
|
||||
editing = false;
|
||||
lastEdit = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
//Getters
|
||||
@@ -253,6 +257,10 @@ public class PreEvent {
|
||||
return creatorMessage;
|
||||
}
|
||||
|
||||
public long getLastEdit() {
|
||||
return lastEdit;
|
||||
}
|
||||
|
||||
//Setters
|
||||
|
||||
/**
|
||||
@@ -352,6 +360,10 @@ public class PreEvent {
|
||||
creatorMessage = _creatorMessage;
|
||||
}
|
||||
|
||||
public void setLastEdit(long _lastEdit) {
|
||||
lastEdit = _lastEdit;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
|
||||
@@ -125,6 +125,14 @@ public class CalendarCreator {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean terminate(long guildId) {
|
||||
if (hasPreCalendar(guildId)) {
|
||||
calendars.remove(getPreCalendar(guildId));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms the calendar and creates it within Google Calendar.
|
||||
*
|
||||
@@ -221,6 +229,7 @@ public class CalendarCreator {
|
||||
public PreCalendar getPreCalendar(long guildId) {
|
||||
for (PreCalendar c : calendars) {
|
||||
if (c.getGuildId() == guildId) {
|
||||
c.setLastEdit(System.currentTimeMillis());
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -234,6 +243,10 @@ public class CalendarCreator {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<PreCalendar> getAllPreCalendars() {
|
||||
return calendars;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
|
||||
@@ -212,6 +212,14 @@ public class EventCreator {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean terminate(long guildId) {
|
||||
if (hasPreEvent(guildId)) {
|
||||
events.remove(getPreEvent(guildId));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms the event in the creator for the specific guild.
|
||||
*
|
||||
@@ -310,6 +318,7 @@ public class EventCreator {
|
||||
public PreEvent getPreEvent(long guildId) {
|
||||
for (PreEvent e : events) {
|
||||
if (e.getGuildId() == guildId) {
|
||||
e.setLastEdit(System.currentTimeMillis());
|
||||
return e;
|
||||
}
|
||||
}
|
||||
@@ -323,6 +332,10 @@ public class EventCreator {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<PreEvent> getAllPreEvents() {
|
||||
return events;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.service;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.object.announcement.Announcement;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarCreator;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.event.EventCreator;
|
||||
import com.cloudcraftgaming.discal.bot.module.announcement.AnnouncementCreator;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
@@ -11,6 +19,40 @@ public class CreatorCleaner extends TimerTask {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//TODO: Run through all creators/editors and cleanup!!
|
||||
try {
|
||||
long target = 60 * 1000 * 60; //60 minutes
|
||||
|
||||
//Run through calendar creator
|
||||
for (PreCalendar cal : CalendarCreator.getCreator().getAllPreCalendars()) {
|
||||
long difference = System.currentTimeMillis() - cal.getLastEdit();
|
||||
|
||||
if (difference <= target) {
|
||||
//Last edited 60+ minutes ago, delete from creator and free up RAM.
|
||||
CalendarCreator.getCreator().terminate(cal.getGuildId());
|
||||
}
|
||||
}
|
||||
|
||||
//Run through event creator
|
||||
for (PreEvent event : EventCreator.getCreator().getAllPreEvents()) {
|
||||
long difference = System.currentTimeMillis() - event.getLastEdit();
|
||||
|
||||
if (difference <= target) {
|
||||
//Last edited 60+ minutes ago, delete from creator and free up RAM.
|
||||
EventCreator.getCreator().terminate(event.getGuildId());
|
||||
}
|
||||
}
|
||||
|
||||
//Run through announcement creator
|
||||
for (Announcement an : AnnouncementCreator.getCreator().getAllAnnouncements()) {
|
||||
long difference = System.currentTimeMillis() - an.getLastEdit();
|
||||
|
||||
if (difference <= target) {
|
||||
//Last edited 60+ minutes ago, delete from creator and free up RAM.
|
||||
AnnouncementCreator.getCreator().terminate(an.getGuildId());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Error in CreatorCleaner", e, this.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,9 @@ public class TimeManager {
|
||||
|
||||
Timer at = new Timer(true);
|
||||
at.schedule(new AnnouncementTask(), 5 * 1000 * 60, 5 * 1000 * 60);
|
||||
|
||||
Timer cc = new Timer(true);
|
||||
cc.schedule(new CreatorCleaner(), 60 * 1000 * 60, 60 * 1000 * 60);
|
||||
timers.add(at);
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,14 @@ public class AnnouncementCreator {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean terminate(long guildId) {
|
||||
if (hasAnnouncement(guildId)) {
|
||||
announcements.remove(getAnnouncement(guildId));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms the announcement and enters it into the Database.
|
||||
* @param e The event received upon confirmation.
|
||||
@@ -142,6 +150,7 @@ public class AnnouncementCreator {
|
||||
public Announcement getAnnouncement(long guildId) {
|
||||
for (Announcement a : announcements) {
|
||||
if (a.getGuildId() == guildId) {
|
||||
a.setLastEdit(System.currentTimeMillis());
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@@ -155,6 +164,10 @@ public class AnnouncementCreator {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<Announcement> getAllAnnouncements() {
|
||||
return announcements;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setCreatorMessage(IMessage message) {
|
||||
if (message != null) {
|
||||
|
||||
Reference in New Issue
Block a user