2024-11-25 21:54:31 +01:00
|
|
|
import { Instance } from "@versia/kit/db";
|
|
|
|
|
import { Instances } from "@versia/kit/tables";
|
|
|
|
|
import { Worker } from "bullmq";
|
|
|
|
|
import { eq } from "drizzle-orm";
|
2025-02-15 02:47:29 +01:00
|
|
|
import { config } from "~/config.ts";
|
2024-11-25 21:54:31 +01:00
|
|
|
import { connection } from "~/utils/redis.ts";
|
|
|
|
|
import {
|
|
|
|
|
type FetchJobData,
|
|
|
|
|
FetchJobType,
|
|
|
|
|
fetchQueue,
|
|
|
|
|
} from "../queues/fetch.ts";
|
|
|
|
|
|
|
|
|
|
export const getFetchWorker = (): Worker<FetchJobData, void, FetchJobType> =>
|
|
|
|
|
new Worker<FetchJobData, void, FetchJobType>(
|
|
|
|
|
fetchQueue.name,
|
|
|
|
|
async (job) => {
|
|
|
|
|
switch (job.name) {
|
|
|
|
|
case FetchJobType.Instance: {
|
|
|
|
|
const { uri } = job.data;
|
|
|
|
|
|
|
|
|
|
await job.log(`Fetching instance metadata from [${uri}]`);
|
|
|
|
|
|
|
|
|
|
// Check if exists
|
|
|
|
|
const host = new URL(uri).host;
|
|
|
|
|
|
|
|
|
|
const existingInstance = await Instance.fromSql(
|
|
|
|
|
eq(Instances.baseUrl, host),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (existingInstance) {
|
|
|
|
|
await job.log(
|
|
|
|
|
"Instance is known, refetching remote data.",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await existingInstance.updateFromRemote();
|
|
|
|
|
|
|
|
|
|
await job.log(
|
|
|
|
|
`Instance [${uri}] successfully refetched`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-01 16:32:18 +01:00
|
|
|
await Instance.resolve(new URL(uri));
|
2024-11-25 21:54:31 +01:00
|
|
|
|
|
|
|
|
await job.log(
|
2025-01-06 19:45:32 +01:00
|
|
|
`✔ Finished fetching instance metadata from [${uri}]`,
|
2024-11-25 21:54:31 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
connection,
|
|
|
|
|
removeOnComplete: {
|
2025-02-15 02:47:29 +01:00
|
|
|
age: config.queues.fetch?.remove_after_complete_seconds,
|
2024-11-25 21:54:31 +01:00
|
|
|
},
|
|
|
|
|
removeOnFail: {
|
2025-02-15 02:47:29 +01:00
|
|
|
age: config.queues.fetch?.remove_after_failure_seconds,
|
2024-11-25 21:54:31 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|