Skip to content

Node.js based forum software built for the modern web

perf: closes #13145, reduce calls in actors.prune

Barış Soner Uşaklıd590c2afcf9a
diff --git a/src/activitypub/actors.js b/src/activitypub/actors.js
index b5ecdc8f96..ef4e1497c1 100644
--- a/src/activitypub/actors.js
+++ b/src/activitypub/actors.js
@@ -298,7 +298,7 @@ Actors.prune = async () => {
const days = parseInt(meta.config.activitypubUserPruneDays, 10);
const timestamp = Date.now() - (1000 * 60 * 60 * 24 * days);
- const uids = await db.getSortedSetRangeByScore('usersRemote:lastCrawled', 0, -1, '-inf', timestamp);
+ const uids = await db.getSortedSetRangeByScore('usersRemote:lastCrawled', 0, 500, '-inf', timestamp);
if (!uids.length) {
winston.info('[actors/prune] No remote users to prune, all done.');
return;
@@ -309,18 +309,19 @@ Actors.prune = async () => {
await batch.processArray(uids, async (uids) => {
const exists = await db.exists(uids.map(uid => `userRemote:${uid}`));
- const [postCounts, roomCounts] = await Promise.all([
- db.sortedSetsCard(uids.map(uid => `uid:${uid}:posts`)),
- db.sortedSetsCard(uids.map(uid => `uid:${uid}:chat:rooms`)),
+
+ const uidsThatExist = uids.filter((uid, idx) => exists[idx]);
+ const uidsThatDontExist = uids.filter((uid, idx) => !exists[idx]);
+ await db.sortedSetRemove('usersRemote:lastCrawled', uidsThatDontExist);
+
+ const [postCounts, roomCounts, followCounts] = await Promise.all([
+ db.sortedSetsCard(uidsThatExist.map(uid => `uid:${uid}:posts`)),
+ db.sortedSetsCard(uidsThatExist.map(uid => `uid:${uid}:chat:rooms`)),
+ Actors.getLocalFollowCounts(uidsThatExist),
]);
- await Promise.all(uids.map(async (uid, idx) => {
- if (!exists[idx]) {
- // id in zset but not asserted, handle and return early
- await db.sortedSetRemove('usersRemote:lastCrawled', uid);
- return;
- }
- const { followers, following } = await Actors.getLocalFollowCounts(uid);
+ await Promise.all(uidsThatExist.map(async (uid, idx) => {
+ const { followers, following } = followCounts[idx];
const postCount = postCounts[idx];
const roomCount = roomCounts[idx];
if ([postCount, roomCount, followers, following].every(metric => metric < 1)) {
diff --git a/src/activitypub/index.js b/src/activitypub/index.js
index 4a6d95b48b..3eec80eae1 100644
--- a/src/activitypub/index.js
+++ b/src/activitypub/index.js
@@ -66,7 +66,7 @@ ActivityPub.startJobs = () => {
}
}, null, true, null, null, false); // change last argument to true for debugging
- new CronJob('0 1 * * *', async () => {
+ new CronJob('*/30 * * * *', async () => {
try {
await ActivityPub.actors.prune();
} catch (err) {