Use a timer task instead for deleting playlist videos not in any playlists. (#311)

This commit is contained in:
Kavin 2022-07-08 00:06:39 +01:00 committed by GitHub
parent 7a8aed5629
commit c85cbf6034
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 26 deletions

View file

@ -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));
}
}

View file

@ -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)