await db.transaction(
async (tx) => {
const urlResult = await tx.query.url.findFirst({
where: { scraped: false },
});
if (!urlResult) {
console.log("No unscraped URL found, retrying...");
await new Promise(resolve => setTimeout(resolve, 5000));
return;
}
try {
// Marking as currently being scraped
await tx.update(url)
.set({ scraped: null })
.where({ gmPartNo: urlResult.gmPartNo });
// Process scraping here
await processScraping(urlResult);
// Mark as scraped. Inside of processscraping actually, but showing here
// for clarity
await tx.update(url)
.set({ scraped: true })
.where({ gmPartNo: urlResult.gmPartNo });
} catch (error) {
console.error("Error processing URL:", error);
// Attempt to revert or handle error
await tx.update(url)
.set({ scraped: false })
.where({ gmPartNo: urlResult.gmPartNo });
}
},
{
isolationLevel: "serializable",
accessMode: "read write",
deferrable: true,
}
);
await db.transaction(
async (tx) => {
const urlResult = await tx.query.url.findFirst({
where: { scraped: false },
});
if (!urlResult) {
console.log("No unscraped URL found, retrying...");
await new Promise(resolve => setTimeout(resolve, 5000));
return;
}
try {
// Marking as currently being scraped
await tx.update(url)
.set({ scraped: null })
.where({ gmPartNo: urlResult.gmPartNo });
// Process scraping here
await processScraping(urlResult);
// Mark as scraped. Inside of processscraping actually, but showing here
// for clarity
await tx.update(url)
.set({ scraped: true })
.where({ gmPartNo: urlResult.gmPartNo });
} catch (error) {
console.error("Error processing URL:", error);
// Attempt to revert or handle error
await tx.update(url)
.set({ scraped: false })
.where({ gmPartNo: urlResult.gmPartNo });
}
},
{
isolationLevel: "serializable",
accessMode: "read write",
deferrable: true,
}
);