Cleanup transaction code. (#306)

This commit is contained in:
Kavin 2022-07-04 18:44:16 +01:00 committed by GitHub
parent 7c087d82b3
commit e6c0f7c0f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 36 deletions

View file

@ -10,7 +10,6 @@ import me.kavin.piped.utils.obj.db.User;
import me.kavin.piped.utils.obj.db.Video;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
@ -99,7 +98,7 @@ public class Main {
var root = cd.from(Video.class);
cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)));
Transaction tr = s.beginTransaction();
var tr = s.beginTransaction();
var query = s.createMutationQuery(cd);

View file

@ -1325,7 +1325,7 @@ public class ResponseHelper {
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
var tr = s.beginTransaction();
s.createQuery(pvQuery).executeUpdate();
s.createMutationQuery(pvQuery).executeUpdate();
tr.commit();
}
}
@ -1425,42 +1425,44 @@ public class ResponseHelper {
if (!channelId.matches("[A-Za-z\\d_-]+"))
return null;
try (Session s = DatabaseSessionFactory.createSession()) {
final ChannelInfo info;
final ChannelInfo info;
try {
info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
} catch (IOException | ExtractionException e) {
ExceptionUtils.rethrow(e);
return null;
}
var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(),
info.getAvatarUrl(), info.isVerified());
s.persist(channel);
s.beginTransaction().commit();
Multithreading.runAsync(() -> {
try {
subscribePubSub(channelId);
} catch (IOException e) {
ExceptionHandler.handle(e);
}
});
Multithreading.runAsync(() -> {
for (StreamInfoItem item : info.getRelatedItems()) {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
handleNewVideo(item.getUrl(), time, channel);
}
});
return channel;
try {
info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
} catch (IOException | ExtractionException e) {
ExceptionUtils.rethrow(e);
return null;
}
var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(),
info.getAvatarUrl(), info.isVerified());
try (Session s = DatabaseSessionFactory.createSession()) {
var tr = s.beginTransaction();
s.persist(channel);
tr.commit();
}
Multithreading.runAsync(() -> {
try {
subscribePubSub(channelId);
} catch (IOException e) {
ExceptionHandler.handle(e);
}
});
Multithreading.runAsync(() -> {
for (StreamInfoItem item : info.getRelatedItems()) {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
handleNewVideo(item.getUrl(), time, channel);
}
});
return channel;
}
public static void subscribePubSub(String channelId) throws IOException {