Skip to content

Commit

Permalink
Merge pull request #642 from TeamPiped/pubsub-upsert
Browse files Browse the repository at this point in the history
Use upsert for handling pubsub and run on limited executors
  • Loading branch information
FireMasterK authored Jul 6, 2023
2 parents 6a2c688 + f9289a0 commit f99aea6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,29 @@ AsyncServlet mainServlet(Executor executor) {
.map(GET, "/webhooks/pubsub", AsyncServlet.ofBlocking(executor, request -> {
var topic = request.getQueryParameter("hub.topic");
if (topic != null)
Multithreading.runAsync(() -> {
Multithreading.runAsyncLimited(() -> {
String channelId = StringUtils.substringAfter(topic, "channel_id=");
PubSubHelper.updatePubSub(channelId);
});
return HttpResponse.ok200().withPlainText(Objects.requireNonNull(request.getQueryParameter("hub.challenge")));

var challenge = request.getQueryParameter("hub.challenge");
return HttpResponse.ok200()
.withPlainText(Objects.requireNonNullElse(challenge, "ok"));
})).map(POST, "/webhooks/pubsub", AsyncServlet.ofBlocking(executor, request -> {
try {

SyndFeed feed = new SyndFeedInput().build(
new InputSource(new ByteArrayInputStream(request.loadBody().getResult().asArray())));

Multithreading.runAsync(() -> {
Multithreading.runAsyncLimited(() -> {
for (var entry : feed.getEntries()) {
String url = entry.getLinks().get(0).getHref();
String videoId = StringUtils.substring(url, -11);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (DatabaseHelper.doesVideoExist(s, videoId))
continue;
}
Multithreading.runAsync(() -> {
Multithreading.runAsyncLimited(() -> {
try {
Sentry.setExtra("videoId", videoId);
var extractor = YOUTUBE_SERVICE.getStreamExtractor("https://youtube.com/watch?v=" + videoId);
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/me/kavin/piped/utils/PubSubHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ public static void subscribePubSub(String channelId) throws IOException {
}

public static void updatePubSub(String channelId) {
var pubsub = DatabaseHelper.getPubSubFromId(channelId);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
s.beginTransaction();
if (pubsub == null) {
pubsub = new PubSub(channelId, System.currentTimeMillis());
s.insert(pubsub);
} else {
pubsub.setSubbedAt(System.currentTimeMillis());
s.update(pubsub);
}
s.getTransaction().commit();
var tr = s.beginTransaction();
s.createNativeMutationQuery("INSERT INTO pubsub (id, subbed_at) VALUES (?, ?) " +
"ON CONFLICT (id) DO UPDATE SET subbed_at = excluded.subbed_at")
.setParameter(1, channelId)
.setParameter(2, System.currentTimeMillis())
.executeUpdate();
tr.commit();
}
}
}

0 comments on commit f99aea6

Please sign in to comment.