Skip to content

Commit

Permalink
Fix: Update Cron Job
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe-serna committed Nov 18, 2024
1 parent 2107ea0 commit c89693e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/BenefitSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function BenefitSection() {
<div id="benefit-video" className="sticky top-[20vh] mb-[30vh] mt-8">
<div
ref={videoRef}
className="relative flex h-[350px] max-h-[800px] min-h-[100px] w-full items-center justify-center rounded-3xl bg-white text-black shadow-xl [aspect-ratio:1_/_1] dark:bg-accent dark:shadow-stone-950 xl:h-[450px]"
className="relative flex h-[350px] max-h-[800px] min-h-[100px] w-full items-center justify-center rounded-3xl bg-[hsl(50,10%,92%)] text-black shadow-xl [aspect-ratio:1_/_1] dark:bg-accent dark:shadow-stone-950 xl:h-[450px]"
>
{isMounted && (
<Image
Expand Down
37 changes: 32 additions & 5 deletions app/audio/MidiEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
AccordionTrigger,
} from "@/components/ui/accordion";
import sliderFrequencyScaling from "@/utils/sliderFrequencyScaling";
import { clearScores } from "@/utils/clearScores";

interface Props {
conversionFlag: Dispatch<SetStateAction<boolean>>;
Expand Down Expand Up @@ -188,17 +189,43 @@ const MidiEditor = forwardRef(
type: "application/octet-stream",
});

// First attempt to create score
try {
const response = await createScore(blob, songName.current);
setFlatScore(response.id);
setShowSheetMusic(true);
} catch (error: any) {
if (error.message === "402") {
message.current =
"Score limit reached. Please try again at the start of the next hour.";
setShowSheetMusic(false);
// Still set flatScore to trigger transition, but with a dummy value
setFlatScore("error-402");
try {
const baseUrl = window.location.origin;
const cleared = await clearScores(baseUrl);
if (!cleared) throw new Error("Failed to clear scores");

// Wait a moment for deletion to complete
await new Promise((resolve) => setTimeout(resolve, 1000));

// Second attempt to create score
try {
const retryResponse = await createScore(blob, songName.current);
setFlatScore(retryResponse.id);
setShowSheetMusic(true);
} catch (retryError: any) {
if (retryError.message === "402") {
message.current =
"Score limit reached. Please try again tomorrow.";
setShowSheetMusic(false);
setFlatScore("error-402");
} else {
throw retryError;
}
}
} catch (cronError) {
console.error("Failed to clear scores:", cronError);
message.current =
"Score limit reached. Please try again tomorrow.";
setShowSheetMusic(false);
setFlatScore("error-402");
}
} else {
throw error;
}
Expand Down
21 changes: 21 additions & 0 deletions utils/clearScores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use server";

export async function clearScores(baseUrl: string) {
try {
const response = await fetch(`${baseUrl}/api/cron`, {
method: "GET",
headers: {
Authorization: `Bearer ${process.env.CRON_SECRET}`,
},
});

if (!response.ok) {
throw new Error(`Failed to clear scores: ${response.statusText}`);
}

return true;
} catch (error) {
console.error("Error clearing scores:", error);
return false;
}
}
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"crons": [
{
"path": "/api/cron",
"schedule": "0 * * * *"
"schedule": "0 5 * * *"
}
]
}

0 comments on commit c89693e

Please sign in to comment.