Skip to content

Commit

Permalink
⏰ Add cron job to delete expired pro entires (#yoginth/eng-101-add-cr…
Browse files Browse the repository at this point in the history
…on-job-to-delete-expired-pro-entires)

Summary: Added a cron job to delete expired pro entries.

Highlights:

• Created `cleanExpiredPro.ts` to delete expired pro entries using Prisma.
• Integrated `cleanExpiredPro` into the cron job scheduler in `index.ts` to run every 5 minutes.

Read more: https://pierre.co/hey/hey/yoginth/eng-101-add-cron-job-to-delete-expired-pro-entires
  • Loading branch information
Yoginth authored and Pierre committed Sep 27, 2024
1 parent 7364e36 commit c6e04e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apps/cron/src/cleanExpiredPro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import prisma from "@hey/db/prisma/db/client";
import logger from "@hey/helpers/logger";

const cleanExpiredPro = async () => {
try {
await prisma.pro.deleteMany({
where: { expiresAt: { lt: new Date() } }
});

logger.info("[Cron] cleanExpiredPro - Cleaned up expired pro");
} catch (error) {
logger.error("[Cron] cleanExpiredPro - Error cleaning expired pro", error);
}
};

export default cleanExpiredPro;
6 changes: 6 additions & 0 deletions apps/cron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import batchProcessEvents from "./batchProcessEvents";
import batchProcessImpressions from "./batchProcessImpressions";
import cleanClickhouse from "./cleanClickhouse";
import cleanEmailTokens from "./cleanEmailTokens";
import cleanExpiredPro from "./cleanExpiredPro";
import cleanPreferences from "./cleanPreferences";
import dbVacuum from "./dbVacuum";
import heartbeat from "./heartbeat";
Expand Down Expand Up @@ -39,6 +40,11 @@ const startCronJobs = () => {
return;
});

cron.schedule("*/5 * * * *", async () => {
await cleanExpiredPro();
return;
});

cron.schedule("0 */6 * * *", async () => {
await dbVacuum();
return;
Expand Down

0 comments on commit c6e04e2

Please sign in to comment.