2023-12-11 02:05:47 -06:00
|
|
|
import { Collection, Link, User } from "@prisma/client";
|
2023-12-10 14:26:44 -06:00
|
|
|
import { prisma } from "../lib/api/db";
|
|
|
|
import urlHandler from "./lib/urlHandler";
|
|
|
|
|
|
|
|
const args = process.argv.slice(2).join(" ");
|
|
|
|
|
2023-12-11 02:05:47 -06:00
|
|
|
console.log(process.env.NEXTAUTH_URL);
|
2023-12-10 14:26:44 -06:00
|
|
|
|
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-10 14:26:44 -06:00
|
|
|
const links = await prisma.link.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
collection: {
|
|
|
|
owner: {
|
|
|
|
archiveAsScreenshot: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
screenshotPath: null,
|
|
|
|
},
|
2023-12-11 02:05:47 -06:00
|
|
|
{
|
|
|
|
collection: {
|
|
|
|
owner: {
|
|
|
|
archiveAsScreenshot: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
screenshotPath: "pending",
|
|
|
|
},
|
|
|
|
///////////////////////
|
2023-12-10 14:26:44 -06:00
|
|
|
{
|
|
|
|
collection: {
|
|
|
|
owner: {
|
|
|
|
archiveAsPDF: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pdfPath: null,
|
|
|
|
},
|
2023-12-11 02:05:47 -06:00
|
|
|
{
|
|
|
|
collection: {
|
|
|
|
owner: {
|
|
|
|
archiveAsPDF: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pdfPath: "pending",
|
|
|
|
},
|
|
|
|
///////////////////////
|
2023-12-10 14:26:44 -06:00
|
|
|
{
|
|
|
|
readabilityPath: null,
|
|
|
|
},
|
2023-12-11 02:05:47 -06:00
|
|
|
{
|
|
|
|
readabilityPath: "pending",
|
2023-12-10 14:26:44 -06:00
|
|
|
},
|
2023-12-11 02:05:47 -06:00
|
|
|
],
|
2023-12-10 14:26:44 -06:00
|
|
|
},
|
|
|
|
take: archiveTakeCount,
|
|
|
|
orderBy: { createdAt: "asc" },
|
|
|
|
include: {
|
2023-12-11 02:05:47 -06:00
|
|
|
collection: {
|
|
|
|
include: {
|
|
|
|
owner: true,
|
|
|
|
},
|
|
|
|
},
|
2023-12-10 14:26:44 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-12-11 02:05:47 -06:00
|
|
|
const archiveLink = async (link: LinksAndCollectionAndOwner) => {
|
2023-12-10 14:26:44 -06:00
|
|
|
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}`
|
2023-12-10 14:26:44 -06:00
|
|
|
);
|
|
|
|
|
2023-12-11 02:05:47 -06:00
|
|
|
await urlHandler(link);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
"\x1b[34m%s\x1b[0m",
|
|
|
|
`Succeeded processing link ${link.url} for user ${link.collection.ownerId}.`
|
|
|
|
);
|
2023-12-10 14:26:44 -06:00
|
|
|
} 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}:`,
|
2023-12-10 14:26:44 -06:00
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
2023-12-11 02:05:47 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Process each link in the batch concurrently
|
|
|
|
const processingPromises = links.map((e) => archiveLink(e));
|
|
|
|
|
|
|
|
await Promise.allSettled(processingPromises);
|
2023-12-10 14:26:44 -06:00
|
|
|
}
|
|
|
|
|
2023-12-12 12:26:53 -06:00
|
|
|
const intervalInSeconds = Number(process.env.ARCHIVE_SCRIPT_INTERVAL) || 10;
|
2023-12-10 14:26:44 -06:00
|
|
|
|
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-10 14:26:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 02:05:47 -06:00
|
|
|
init();
|