diff --git a/src/App.tsx b/src/App.tsx index 37a21ef..b3d8cf7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import { Layout, Typography } from "antd"; -import { getJobStatus } from "./utils/firebase"; +import { getJobStatus, updateJobStatusTimestamp } from "./utils/firebase"; import { getFirebaseRecipe, jsonToString } from "./utils/recipeLoader"; import { getSubmitPackingUrl, JOB_STATUS } from "./constants/aws"; import { @@ -68,14 +68,15 @@ function App() { start = Date.now(); const response = await fetch(request); setJobStatus(JOB_STATUS.SUBMITTED); - const data = await response.json(); if (response.ok) { + const data = await response.json(); setJobId(data.jobId); setJobStatus(JOB_STATUS.STARTING); return data.jobId; } else { + const errorText = await response.text(); setJobStatus(JOB_STATUS.FAILED); - setJobLogs(JSON.stringify(data)); + setJobLogs(errorText); } }; @@ -92,6 +93,9 @@ function App() { const checkStatus = async (jobIdFromSubmit: string) => { const id = jobIdFromSubmit || jobId; let localJobStatus = await getJobStatus(id); + if (localJobStatus) { + setJobStatus(localJobStatus.status); + } while ( localJobStatus?.status !== JOB_STATUS.DONE && localJobStatus?.status !== JOB_STATUS.FAILED @@ -106,6 +110,11 @@ function App() { setJobStatus(newJobStatus.status); } } + + // Update the job status timestamp after reading the final status to + // ensure we have the most recent timestamp for retention policy + await updateJobStatusTimestamp(id); + const range = (Date.now() - start) / 1000; if (localJobStatus.status == JOB_STATUS.DONE) { setPackingResults({ diff --git a/src/constants/firebase.ts b/src/constants/firebase.ts index 7f5bff4..a06200b 100644 --- a/src/constants/firebase.ts +++ b/src/constants/firebase.ts @@ -44,7 +44,7 @@ export const FIRESTORE_FIELDS = { export const RETENTION_POLICY = { RETENTION_PERIODS: { RECIPES_EDITED: 24 * 60 * 60 * 1000, // 24 hours - JOB_STATUS: 24 * 60 * 60 * 1000, // 24 hours + JOB_STATUS: 30 * 24 * 60 * 60 * 1000, // 30 days }, TIMESTAMP_FIELD: "timestamp", diff --git a/src/utils/firebase.ts b/src/utils/firebase.ts index d6c630c..caf24d9 100644 --- a/src/utils/firebase.ts +++ b/src/utils/firebase.ts @@ -11,6 +11,7 @@ import { doc, Timestamp, deleteDoc, + updateDoc } from "firebase/firestore"; import { sortBy } from "lodash-es"; import { @@ -125,6 +126,13 @@ const getJobStatus = async ( return docs[0] || undefined; }; +const updateJobStatusTimestamp = async (jobId: string) => { + const data = { + timestamp: Timestamp.now(), + }; + await updateDoc(doc(db, FIRESTORE_COLLECTIONS.JOB_STATUS, jobId), data); +}; + const getOutputsDirectory = async (jobId: string) => { const querySnapshot = await queryDocumentById( FIRESTORE_COLLECTIONS.JOB_STATUS, @@ -274,4 +282,5 @@ export { getRecipeManifestFromFirebase, getRecipeDataFromFirebase, getOutputsDirectory, + updateJobStatusTimestamp, };