refactor(worker): Move blurhash processing to worker

This commit is contained in:
Jesse Wierzbinski 2025-01-06 19:45:32 +01:00
parent 8188a6ffc7
commit b086e65404
No known key found for this signature in database
8 changed files with 34 additions and 37 deletions

View file

@ -1,4 +1,3 @@
import { getLogger } from "@logtape/logtape";
import { User } from "@versia/kit/db";
import { Worker } from "bullmq";
import chalk from "chalk";
@ -22,8 +21,6 @@ export const getDeliveryWorker = (): Worker<
case DeliveryJobType.FederateEntity: {
const { entity, recipientId, senderId } = job.data;
const logger = getLogger(["federation", "delivery"]);
const sender = await User.fromId(senderId);
if (!sender) {
@ -40,17 +37,15 @@ export const getDeliveryWorker = (): Worker<
);
}
logger.debug`Federating entity ${chalk.gray(
entity.id,
)} from ${chalk.gray(`@${sender.getAcct()}`)} to ${chalk.gray(
recipient.getAcct(),
)}`;
await job.log(
`Federating entity [${entity.id}] from @${sender.getAcct()} to @${recipient.getAcct()}`,
);
await sender.federateToUser(entity, recipient);
logger.debug`${chalk.green(
"✔",
)} Finished federating entity ${chalk.gray(entity.id)}`;
await job.log(
`✔ Finished federating entity [${entity.id}]`,
);
}
}
},

View file

@ -1,7 +1,6 @@
import { Instance } from "@versia/kit/db";
import { Instances } from "@versia/kit/tables";
import { Worker } from "bullmq";
import chalk from "chalk";
import { eq } from "drizzle-orm";
import { config } from "~/packages/config-manager";
import { connection } from "~/utils/redis.ts";
@ -45,9 +44,7 @@ export const getFetchWorker = (): Worker<FetchJobData, void, FetchJobType> =>
await Instance.resolve(uri);
await job.log(
`${chalk.green(
"✔",
)} Finished fetching instance metadata from [${uri}]`,
`✔ Finished fetching instance metadata from [${uri}]`,
);
}
}

View file

@ -51,7 +51,7 @@ export const getInboxWorker = (): Worker<InboxJobData, void, InboxJobType> =>
}
await job.log(
`Finished processing entity [${data.id}]`,
`Finished processing entity [${data.id}]`,
);
return;

View file

@ -3,6 +3,7 @@ import { Worker } from "bullmq";
import { config } from "~/packages/config-manager";
import { connection } from "~/utils/redis.ts";
import { MediaManager } from "../media/media-manager.ts";
import { BlurhashPreprocessor } from "../media/preprocessors/blurhash.ts";
import { ImageConversionPreprocessor } from "../media/preprocessors/image-conversion.ts";
import {
type MediaJobData,
@ -18,6 +19,8 @@ export const getMediaWorker = (): Worker<MediaJobData, void, MediaJobType> =>
case MediaJobType.ConvertMedia: {
const { attachmentId, filename } = job.data;
await job.log(`Fetching attachment ID [${attachmentId}]`);
const attachment = await Attachment.fromId(attachmentId);
if (!attachment) {
@ -27,6 +30,7 @@ export const getMediaWorker = (): Worker<MediaJobData, void, MediaJobType> =>
}
const processor = new ImageConversionPreprocessor(config);
const blurhashProcessor = new BlurhashPreprocessor();
const hash = attachment?.data.sha256;
@ -36,6 +40,11 @@ export const getMediaWorker = (): Worker<MediaJobData, void, MediaJobType> =>
);
}
await job.log(`Processing attachment [${attachmentId}]`);
await job.log(
`Fetching file from [${attachment.data.url}]`,
);
// Download the file and process it.
const blob = await (
await fetch(attachment.data.url)
@ -43,11 +52,19 @@ export const getMediaWorker = (): Worker<MediaJobData, void, MediaJobType> =>
const file = new File([blob], filename);
await job.log(`Converting attachment [${attachmentId}]`);
const { file: processedFile } =
await processor.process(file);
await job.log(`Generating blurhash for [${attachmentId}]`);
const { blurhash } = await blurhashProcessor.process(file);
const mediaManager = new MediaManager(config);
await job.log(`Uploading attachment [${attachmentId}]`);
const { path, uploadedFile } =
await mediaManager.addFile(processedFile);
@ -62,7 +79,12 @@ export const getMediaWorker = (): Worker<MediaJobData, void, MediaJobType> =>
.digest("hex"),
mimeType: uploadedFile.type,
size: uploadedFile.size,
blurhash,
});
await job.log(
`✔ Finished processing attachment [${attachmentId}]`,
);
}
}
},