-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scheduler for purging expired records
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package com.mally.api.pastebin.repositories; | ||
|
||
import com.mally.api.pastebin.entities.Paste; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface PastebinRepository extends CrudRepository<Paste, Long> { | ||
Optional<Paste> findBySlug(String slug); | ||
|
||
@Transactional | ||
@Modifying | ||
@Query("DELETE FROM Paste p WHERE p.expiresAt < :now") | ||
void deleteExpiredPastes(@Param("now") ZonedDateTime now); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
apps/api/src/main/java/com/mally/api/shared/schedulers/ExpiredRecordsPurger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.mally.api.shared.schedulers; | ||
|
||
|
||
import com.mally.api.pastebin.services.PastebinService; | ||
import com.mally.api.urlshortener.services.UrlShortenerService; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@EnableScheduling | ||
@AllArgsConstructor | ||
@Slf4j | ||
@Component | ||
public class ExpiredRecordsPurger { | ||
static private final String AT_MIDNIGHT_CRON = "0 0 * * * *"; | ||
|
||
private final UrlShortenerService urlShortenerService; | ||
|
||
private final PastebinService pastebinService; | ||
|
||
@Scheduled(cron = AT_MIDNIGHT_CRON) | ||
public void purgeExpiredRecords() { | ||
deleteExpiredURLs(); | ||
deleteExpiredPastes(); | ||
|
||
log.info("Expired records purged."); | ||
} | ||
|
||
private void deleteExpiredURLs() { | ||
urlShortenerService.deleteExpiredURLs(); | ||
} | ||
|
||
private void deleteExpiredPastes() { | ||
pastebinService.deleteExpiredPastes(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
apps/api/src/main/java/com/mally/api/urlshortener/repositories/UrlShortenerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package com.mally.api.urlshortener.repositories; | ||
|
||
import com.mally.api.urlshortener.entities.Url; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UrlShortenerRepository extends CrudRepository<Url, Long> { | ||
Optional<Url> findBySlug(String slug); | ||
|
||
@Transactional | ||
@Modifying | ||
@Query("DELETE FROM Url u WHERE u.expiresAt < :now") | ||
void deleteExpiredURLs(@Param("now") ZonedDateTime now); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters