Helped solve a bug where it would get stuck

This commit is contained in:
Tiago Ribeiro
2024-03-24 03:18:00 +00:00
parent 22f2b43692
commit ee26b50cf6
3 changed files with 44 additions and 3 deletions

View File

@@ -14,6 +14,10 @@ interface Body {
id: string;
}
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -29,7 +33,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const backendRequest = await evaluate(req.body as Body);
console.log("🌱 - Process complete");
const correspondingStat = (await getDoc(doc(db, "stats", req.body.id))).data() as Stat;
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
await setDoc(
doc(db, "stats", (req.body as Body).id),
@@ -47,6 +52,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log("🌱 - Updated the DB");
}
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
console.log(`🌱 - Try number ${index} - ${id}`);
const correspondingStat = await getDoc(doc(db, "stats", id));
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
await delay(3 * 10000);
return getCorrespondingStat(id, index + 1);
}
async function evaluate(body: Body): Promise<AxiosResponse> {
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task2`, body as Body, {
headers: {