Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

fix user blocking queue

daniel31x13eb3f1eeb5b07
diff --git a/.env.sample b/.env.sample
index bd3abcb0..e204f52d 100644
--- a/.env.sample
+++ b/.env.sample
@@ -17,6 +17,8 @@ NEXT_PUBLIC_CREDENTIALS_ENABLED=
DISABLE_NEW_SSO_USERS=
MAX_LINKS_PER_USER=
ARCHIVE_TAKE_COUNT=
+ARCHIVE_USER_TAKE_COUNT=
+ARCHIVE_PER_USER_CAP=
BROWSER_TIMEOUT=
IGNORE_UNAUTHORIZED_CA=
IGNORE_HTTPS_ERRORS=
diff --git a/apps/worker/lib/getLinkBatchFairly.ts b/apps/worker/lib/getLinkBatchFairly.ts
new file mode 100644
index 00000000..5cbf1c1d
--- /dev/null
+++ b/apps/worker/lib/getLinkBatchFairly.ts
@@ -0,0 +1,107 @@
+import { prisma } from "@linkwarden/prisma";
+
+type Options = {
+ usersPerRound: number;
+ perUserCap: number;
+ batchLimit: number;
+ overfetchFactor?: number;
+};
+
+export async function getLinkBatchFairly(opts: Options) {
+ const { usersPerRound, perUserCap, batchLimit, overfetchFactor = 3 } = opts;
+
+ const candidates = await prisma.user.findMany({
+ where: {
+ subscriptions: {
+ is: {
+ active: true,
+ currentPeriodEnd: { gt: new Date() },
+ },
+ },
+ collections: {
+ some: {
+ links: {
+ some: {
+ url: { not: null },
+ OR: [
+ { lastPreserved: null },
+ {
+ aiTagged: false,
+ createdBy: {
+ aiTagExistingLinks: true,
+ NOT: { aiTaggingMethod: "DISABLED" },
+ },
+ },
+ ],
+ },
+ },
+ },
+ },
+ },
+ orderBy: { lastPickedAt: "asc" },
+ take: usersPerRound,
+ select: { id: true },
+ });
+
+ console.log("Found candidates:", candidates);
+
+ if (candidates.length === 0) return [];
+
+ const ownerIds = candidates.map((u) => u.id);
+
+ const targetCount = Math.min(
+ batchLimit * overfetchFactor,
+ usersPerRound * perUserCap * overfetchFactor
+ );
+
+ const links = await prisma.link.findMany({
+ where: {
+ url: { not: null },
+ collection: { ownerId: { in: ownerIds } },
+ OR: [
+ { lastPreserved: null },
+ {
+ aiTagged: false,
+ createdBy: {
+ aiTagExistingLinks: true,
+ NOT: { aiTaggingMethod: "DISABLED" },
+ },
+ },
+ ],
+ },
+ include: {
+ collection: { include: { owner: true } },
+ tags: true,
+ },
+ orderBy: { id: "desc" },
+ take: targetCount,
+ });
+
+ if (links.length === 0) return [];
+
+ const perUserPicked: Record<number, number> = {};
+ const picked: typeof links = [];
+
+ for (const l of links) {
+ const ownerId = l.collection.ownerId;
+ const count = perUserPicked[ownerId] ?? 0;
+ if (count < perUserCap) {
+ perUserPicked[ownerId] = count + 1;
+ picked.push(l);
+ if (picked.length >= batchLimit) break;
+ }
+ }
+
+ if (picked.length === 0) return [];
+
+ const touchedUserIds = [...new Set(picked.map((l) => l.collection.ownerId))];
+
+ await prisma.$transaction([
+ prisma.user.updateMany({
+ where: { id: { in: touchedUserIds } },
+ data: { lastPickedAt: new Date() },
+ }),
+ ]);
+
+ return picked;
+}
diff --git a/apps/worker/workers/linkProcessing.ts b/apps/worker/workers/linkProcessing.ts
index 10e3ed37..03c9fbc5 100644
--- a/apps/worker/workers/linkProcessing.ts
+++ b/apps/worker/workers/linkProcessing.ts
@@ -1,41 +1,23 @@
import { prisma } from "@linkwarden/prisma";
import archiveHandler from "../lib/archiveHandler";
import { LinkWithCollectionOwnerAndTags } from "@linkwarden/types";
-import getLinkBatch from "../lib/getLinkBatch";
import { delay } from "@linkwarden/lib";
+import { getLinkBatchFairly } from "../lib/getLinkBatchFairly";
-const takeCount = Number(process.env.ARCHIVE_TAKE_COUNT || "") || 5;
+const ARCHIVE_TAKE_COUNT = Number(process.env.ARCHIVE_TAKE_COUNT || "") || 5;
+const ARCHIVE_USER_TAKE_COUNT =
+ Number(process.env.ARCHIVE_USER_TAKE_COUNT || "") || 1;
+const ARCHIVE_PER_USER_CAP =
+ Number(process.env.ARCHIVE_PER_USER_CAP || "") || 5;
export async function startProcessing(interval = 10) {
console.log("\x1b[34m%s\x1b[0m", "Starting link processing...");
while (true) {
- const links = await getLinkBatch({
- where: {
- url: { not: null },
- OR: [
- {
- lastPreserved: null,
- },
- {
- createdBy: {
- aiTagExistingLinks: true,
- NOT: {
- aiTaggingMethod: "DISABLED",
- },
- },
- aiTagged: false,
- },
- ],
- },
- take: takeCount,
- include: {
- collection: {
- include: {
- owner: true,
- },
- },
- tags: true,
- },
+ const links = await getLinkBatchFairly({
+ usersPerRound: ARCHIVE_USER_TAKE_COUNT,
+ perUserCap: ARCHIVE_PER_USER_CAP,
+ batchLimit: ARCHIVE_TAKE_COUNT,
+ overfetchFactor: 3,
});
if (links.length === 0) {
diff --git a/packages/prisma/migrations/20250812205616_add_field_to_user_model/migration.sql b/packages/prisma/migrations/20250812205616_add_field_to_user_model/migration.sql
new file mode 100644
index 00000000..341e087b
--- /dev/null
+++ b/packages/prisma/migrations/20250812205616_add_field_to_user_model/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "User" ADD COLUMN "lastPickedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma
index fa3903f1..e24fb0be 100644
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@ -67,6 +67,7 @@ model User {
isPrivate Boolean @default(false)
referredBy String?
dashboardSections DashboardSection[]
+ lastPickedAt DateTime @default(now())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}