el.xwx.moe/scripts/worker.ts

191 lines
4.1 KiB
TypeScript
Raw Normal View History

import 'dotenv/config';
2023-12-11 02:05:47 -06:00
import { Collection, Link, User } from "@prisma/client";
import { prisma } from "../lib/api/db";
import archiveHandler from "../lib/api/archiveHandler";
const args = process.argv.slice(2).join(" ");
2023-12-11 02:05:47 -06:00
const archiveTakeCount = Number(process.env.ARCHIVE_TAKE_COUNT || "") || 5;
type LinksAndCollectionAndOwner = Link & {
collection: Collection & {
owner: User;
};
};
async function processBatch() {
2023-12-13 05:59:36 -06:00
const linksOldToNew = await prisma.link.findMany({
where: {
2023-12-13 05:59:36 -06:00
url: { not: null },
OR: [
{
collection: {
owner: {
archiveAsScreenshot: true,
},
},
image: null,
},
2023-12-11 02:05:47 -06:00
{
collection: {
owner: {
archiveAsScreenshot: true,
},
},
image: "pending",
2023-12-11 02:05:47 -06:00
},
///////////////////////
{
collection: {
owner: {
archiveAsPDF: true,
},
},
pdf: null,
},
2023-12-11 02:05:47 -06:00
{
collection: {
owner: {
archiveAsPDF: true,
},
},
pdf: "pending",
2023-12-11 02:05:47 -06:00
},
///////////////////////
{
readable: null,
},
2023-12-11 02:05:47 -06:00
{
readable: "pending",
},
2023-12-11 02:05:47 -06:00
],
},
take: archiveTakeCount,
2023-12-23 11:11:47 -06:00
orderBy: { id: "asc" },
include: {
2023-12-11 02:05:47 -06:00
collection: {
include: {
owner: true,
},
},
},
});
2023-12-13 05:59:36 -06:00
const linksNewToOld = await prisma.link.findMany({
where: {
url: { not: null },
OR: [
{
collection: {
owner: {
archiveAsScreenshot: true,
},
},
image: null,
2023-12-13 05:59:36 -06:00
},
{
collection: {
owner: {
archiveAsScreenshot: true,
},
},
image: "pending",
2023-12-13 05:59:36 -06:00
},
///////////////////////
{
collection: {
owner: {
archiveAsPDF: true,
},
},
pdf: null,
2023-12-13 05:59:36 -06:00
},
{
collection: {
owner: {
archiveAsPDF: true,
},
},
pdf: "pending",
2023-12-13 05:59:36 -06:00
},
///////////////////////
{
readable: null,
2023-12-13 05:59:36 -06:00
},
{
readable: "pending",
2023-12-13 05:59:36 -06:00
},
2023-12-23 11:11:47 -06:00
///////////////////////
{
preview: null,
},
{
preview: "pending",
},
2023-12-13 05:59:36 -06:00
],
},
take: archiveTakeCount,
2023-12-23 11:11:47 -06:00
orderBy: { id: "desc" },
2023-12-13 05:59:36 -06:00
include: {
collection: {
include: {
owner: true,
},
},
},
});
2023-12-11 02:05:47 -06:00
const archiveLink = async (link: LinksAndCollectionAndOwner) => {
try {
console.log(
2023-12-11 02:05:47 -06:00
"\x1b[34m%s\x1b[0m",
`Processing link ${link.url} for user ${link.collection.ownerId}`
);
await archiveHandler(link);
2023-12-11 02:05:47 -06:00
console.log(
"\x1b[34m%s\x1b[0m",
`Succeeded processing link ${link.url} for user ${link.collection.ownerId}.`
);
} catch (error) {
console.error(
2023-12-11 02:05:47 -06:00
"\x1b[34m%s\x1b[0m",
`Error processing link ${link.url} for user ${link.collection.ownerId}:`,
error
);
}
2023-12-11 02:05:47 -06:00
};
// Process each link in the batch concurrently
const processingPromises = [...linksOldToNew, ...linksNewToOld]
// Make sure we don't process the same link twice
.filter((value, index, self) => {
return self.findIndex((item) => item.id === value.id) === index;
})
.map((e) => archiveLink(e));
2023-12-11 02:05:47 -06:00
await Promise.allSettled(processingPromises);
}
2023-12-12 12:26:53 -06:00
const intervalInSeconds = Number(process.env.ARCHIVE_SCRIPT_INTERVAL) || 10;
2023-12-11 02:05:47 -06:00
function delay(sec: number) {
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
}
async function init() {
console.log("\x1b[34m%s\x1b[0m", "Starting the link processing task");
while (true) {
try {
await processBatch();
2023-12-12 12:26:53 -06:00
await delay(intervalInSeconds);
2023-12-11 02:05:47 -06:00
} catch (error) {
console.error("\x1b[34m%s\x1b[0m", "Error processing links:", error);
2023-12-12 12:26:53 -06:00
await delay(intervalInSeconds);
}
}
}
2023-12-11 02:05:47 -06:00
init();