Delete events older than 90 days once per day.

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-09-05 11:23:39 -07:00
parent e6b7efa95f
commit ce4d00a788
3 changed files with 34 additions and 2 deletions

View file

@ -13,7 +13,6 @@ repositories {
dependencies {
implementation project(':shared')
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'

View file

@ -2,12 +2,13 @@ package com.wbrawner.flayre.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class FlayreApplication {
public static void main(String[] args) {
SpringApplication.run(FlayreApplication.class, args);
}
}

View file

@ -0,0 +1,32 @@
package com.wbrawner.flayre.server.events;
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.Component;
import java.util.Calendar;
import java.util.TimeZone;
/**
* Responsible for deleting events older than a given date
*/
@Component
public class EventScheduler {
private static final Logger logger = LoggerFactory.getLogger(EventScheduler.class);
private final EventRepository eventRepository;
@Autowired
public EventScheduler(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
@Scheduled(cron = "0 0 * * *")
public void deleteOldEvents() {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.DATE, -90);
int deletedEvents = eventRepository.deleteEvents(calendar.getTime());
logger.info("Deleted {} events older than 90 days", deletedEvents);
}
}