Skip to content

Commit

Permalink
chore[VERCEL_CRON-JOBS]: Added cron jobs for removing unused data
Browse files Browse the repository at this point in the history
Data will be automatically removed from the server if its unused
  • Loading branch information
parazeeknova committed Oct 16, 2024
1 parent f858c28 commit 829fc70
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ We use <strong>pnpm</strong> and <strong>turbo</strong> for managing the depende

> [!IMPORTANT]
> Make sure you have the prerequisites installed before running the following commands and have `.env` file in the root directory.
> `.env.example` file is provided as an example uses Vercel Postgres and Uploadthing.
> Use the format in `.env.example` file to create a `.env` file in the root directory in `apps/web/` as its a monorepo.
> Get an example / temp postgre database from vercel for `.env` file and place it in `packages/db`.
> Get your storage bucket from uploadthing and add it to `.env` file, Will be replaced with amazon s3 bucket later.
> Add a random string in CRON_SECRET in `.env` file for vercel cron jobs (Only works in production).
```bash
# Clone the repository
Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ This repository participates in Hacktoberfest! We welcome contributions througho
3. **Set up development environment**

```bash
# Get an example / temp database from vercel for `.env` file and place it in `packages/db`.
# Use the format in `.env.example` file to create a `.env` file in the root directory in `apps/web/` as its a monorepo.
# Get an example / temp postgre database from vercel for `.env` file and place it in `packages/db`.
# Get your storage bucket from uploadthing and add it to `.env` file, Will be replaced with amazon s3 bucket later.
# Format is in `.env.example` file
# Add a random string in CRON_SECRET in `.env` file for vercel cron jobs (Only works in production).
```

4. **Run the development servers**
Expand Down
3 changes: 3 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
UPLOADTHING_TOKEN=<uploadthing_token>
UPLOADTHING_SECRET=<uploadthing_secret>
NEXT_PUBLIC_UPLOADTHING_APP_ID=<uploadthing_app_id>

# Other - Can be any random string
CRON_SECRET="<cron_secret>"
52 changes: 52 additions & 0 deletions apps/web/src/app/api/clear-uploads/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { prisma } from "@zephyr/db";
import { UTApi } from "uploadthing/server";

export async function GET(req: Request) {
try {
const authHeader = req.headers.get("Authorization");

if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
return Response.json(
{ message: "Invalid authorization header" },
{ status: 401 }
);
}

const unusedMedia = await prisma.media.findMany({
where: {
postId: null,
...(process.env.NODE_ENV === "production"
? {
createdAt: {
lte: new Date(Date.now() - 1000 * 60 * 60 * 24)
}
}
: {})
},
select: {
id: true,
url: true
}
});

new UTApi().deleteFiles(
unusedMedia.map(
(m) =>
m.url.split(`/a/${process.env.NEXT_PUBLIC_UPLOADTHING_APP_ID}/`)[1]
)
);

await prisma.media.deleteMany({
where: {
id: {
in: unusedMedia.map((m) => m.id)
}
}
});

return new Response();
} catch (error) {
console.error(error);
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}
8 changes: 8 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"crons": [
{
"path": "/api/clear-uploads",
"schedule": "0 2 * * *"
}
]
}

0 comments on commit 829fc70

Please sign in to comment.