aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workers
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-09-06 06:24:05 +0000
committerMohamed Bassem <me@mbassem.com>2025-09-06 06:25:15 +0000
commit3760d23abc4d02eb2c3823b8aa322f53914fd9b6 (patch)
tree1d720b1f1d1391317185b319d1fb1fba3f18a274 /apps/workers
parentfcfe6a53b49dc2fdff6abac876b41b52f1b0fed7 (diff)
downloadkarakeep-3760d23abc4d02eb2c3823b8aa322f53914fd9b6.tar.zst
refactor: Extract quota logic into its own class
Diffstat (limited to 'apps/workers')
-rw-r--r--apps/workers/workers/assetPreprocessingWorker.ts7
-rw-r--r--apps/workers/workers/crawlerWorker.ts10
-rw-r--r--apps/workers/workers/videoWorker.ts11
3 files changed, 13 insertions, 15 deletions
diff --git a/apps/workers/workers/assetPreprocessingWorker.ts b/apps/workers/workers/assetPreprocessingWorker.ts
index 73cf8bb5..d059e21c 100644
--- a/apps/workers/workers/assetPreprocessingWorker.ts
+++ b/apps/workers/workers/assetPreprocessingWorker.ts
@@ -14,6 +14,7 @@ import {
bookmarkAssets,
bookmarks,
} from "@karakeep/db/schema";
+import { QuotaService, StorageQuotaError } from "@karakeep/shared-server";
import { newAssetId, readAsset, saveAsset } from "@karakeep/shared/assetdb";
import serverConfig from "@karakeep/shared/config";
import logger from "@karakeep/shared/logger";
@@ -22,10 +23,6 @@ import {
OpenAIQueue,
triggerSearchReindex,
} from "@karakeep/shared/queues";
-import {
- checkStorageQuota,
- StorageQuotaError,
-} from "@karakeep/trpc/lib/storageQuota";
export class AssetPreprocessingWorker {
static build() {
@@ -136,7 +133,7 @@ export async function extractAndSavePDFScreenshot(
}
// Check storage quota before inserting
- const quotaApproved = await checkStorageQuota(
+ const quotaApproved = await QuotaService.checkStorageQuota(
db,
bookmark.userId,
screenshot.buffer.byteLength,
diff --git a/apps/workers/workers/crawlerWorker.ts b/apps/workers/workers/crawlerWorker.ts
index 36068f14..2aaab776 100644
--- a/apps/workers/workers/crawlerWorker.ts
+++ b/apps/workers/workers/crawlerWorker.ts
@@ -41,6 +41,7 @@ import {
bookmarks,
users,
} from "@karakeep/db/schema";
+import { QuotaService } from "@karakeep/shared-server";
import {
ASSET_TYPES,
getAssetSize,
@@ -65,7 +66,6 @@ import {
} from "@karakeep/shared/queues";
import { tryCatch } from "@karakeep/shared/tryCatch";
import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
-import { checkStorageQuota } from "@karakeep/trpc/lib/storageQuota";
import metascraperReddit from "../metascraper-plugins/metascraper-reddit";
@@ -536,7 +536,7 @@ async function storeScreenshot(
// Check storage quota before saving the screenshot
const { data: quotaApproved, error: quotaError } = await tryCatch(
- checkStorageQuota(db, userId, screenshot.byteLength),
+ QuotaService.checkStorageQuota(db, userId, screenshot.byteLength),
);
if (quotaError) {
@@ -586,7 +586,7 @@ async function downloadAndStoreFile(
// Check storage quota before saving the asset
const { data: quotaApproved, error: quotaError } = await tryCatch(
- checkStorageQuota(db, userId, buffer.byteLength),
+ QuotaService.checkStorageQuota(db, userId, buffer.byteLength),
);
if (quotaError) {
@@ -655,7 +655,7 @@ async function archiveWebpage(
const fileSize = stats.size;
const { data: quotaApproved, error: quotaError } = await tryCatch(
- checkStorageQuota(db, userId, fileSize),
+ QuotaService.checkStorageQuota(db, userId, fileSize),
);
if (quotaError) {
@@ -813,7 +813,7 @@ async function storeHtmlContent(
}
const { data: quotaApproved, error: quotaError } = await tryCatch(
- checkStorageQuota(db, userId, contentBuffer.byteLength),
+ QuotaService.checkStorageQuota(db, userId, contentBuffer.byteLength),
);
if (quotaError) {
logger.warn(
diff --git a/apps/workers/workers/videoWorker.ts b/apps/workers/workers/videoWorker.ts
index 69f88a21..68be0126 100644
--- a/apps/workers/workers/videoWorker.ts
+++ b/apps/workers/workers/videoWorker.ts
@@ -7,6 +7,7 @@ import { workerStatsCounter } from "metrics";
import { db } from "@karakeep/db";
import { AssetTypes } from "@karakeep/db/schema";
+import { QuotaService, StorageQuotaError } from "@karakeep/shared-server";
import {
ASSET_TYPES,
newAssetId,
@@ -20,10 +21,6 @@ import {
ZVideoRequest,
zvideoRequestSchema,
} from "@karakeep/shared/queues";
-import {
- checkStorageQuota,
- StorageQuotaError,
-} from "@karakeep/trpc/lib/storageQuota";
import { getBookmarkDetails, updateAsset } from "../workerUtils";
@@ -148,7 +145,11 @@ async function runWorker(job: DequeuedJob<ZVideoRequest>) {
const fileSize = stats.size;
try {
- const quotaApproved = await checkStorageQuota(db, userId, fileSize);
+ const quotaApproved = await QuotaService.checkStorageQuota(
+ db,
+ userId,
+ fileSize,
+ );
await saveAssetFromFile({
userId,