diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 15e2289..d823bc0 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -5,6 +5,7 @@ import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.*; +import me.kavin.piped.utils.obj.db.PlaylistVideo; import me.kavin.piped.utils.obj.db.PubSub; import me.kavin.piped.utils.obj.db.User; import me.kavin.piped.utils.obj.db.Video; @@ -111,5 +112,29 @@ public class Main { } }, 0, TimeUnit.MINUTES.toMillis(60)); + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { + + CriteriaBuilder cb = s.getCriteriaBuilder(); + + var pvQuery = cb.createCriteriaDelete(PlaylistVideo.class); + var pvRoot = pvQuery.from(PlaylistVideo.class); + + var subQuery = pvQuery.subquery(me.kavin.piped.utils.obj.db.Playlist.class); + var subRoot = subQuery.from(me.kavin.piped.utils.obj.db.Playlist.class); + + subQuery.select(subRoot.join("videos").get("id")); + + pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); + + var tr = s.beginTransaction(); + s.createMutationQuery(pvQuery).executeUpdate(); + tr.commit(); + } + } + }, 0, TimeUnit.MINUTES.toMillis(60)); + } } diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index bfc190e..9f2220d 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -773,8 +773,6 @@ public class ResponseHelper { var tr = s.beginTransaction(); s.remove(user); tr.commit(); - - Multithreading.runAsync(ResponseHelper::pruneUnusedPlaylistVideos); } catch (Exception e) { return mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage())); } @@ -1091,7 +1089,6 @@ public class ResponseHelper { s.remove(playlist); tr.commit(); - Multithreading.runAsync(ResponseHelper::pruneUnusedPlaylistVideos); } return mapper.writeValueAsBytes(new AcceptedResponse()); @@ -1292,8 +1289,6 @@ public class ResponseHelper { s.merge(playlist); tr.commit(); - Multithreading.runAsync(ResponseHelper::pruneUnusedPlaylistVideos); - return mapper.writeValueAsBytes(new AcceptedResponse()); } } @@ -1314,27 +1309,6 @@ public class ResponseHelper { } } - private static void pruneUnusedPlaylistVideos() { - - try (Session s = DatabaseSessionFactory.createSession()) { - CriteriaBuilder cb = s.getCriteriaBuilder(); - - var pvQuery = cb.createCriteriaDelete(PlaylistVideo.class); - var pvRoot = pvQuery.from(PlaylistVideo.class); - - var subQuery = pvQuery.subquery(me.kavin.piped.utils.obj.db.Playlist.class); - var subRoot = subQuery.from(me.kavin.piped.utils.obj.db.Playlist.class); - - subQuery.select(subRoot.join("videos").get("id")); - - pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); - - var tr = s.beginTransaction(); - s.createMutationQuery(pvQuery).executeUpdate(); - tr.commit(); - } - } - private static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) { if (channel == null)