Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Use vercel cron to call house-keeping (#1929)

Luke Vellab24b58439cc3
diff --git a/.github/workflows/house-keeping.yml b/.github/workflows/house-keeping.yml
deleted file mode 100644
index 2016966e5..000000000
--- a/.github/workflows/house-keeping.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-name: House Keeping
-on:
- workflow_dispatch:
- schedule:
- - cron: "0 6 * * *" # Every day at 6:00am UTC
-jobs:
- clean:
- name: "Clean"
- runs-on: ubuntu-latest
- env:
- API_SECRET: ${{ secrets.API_SECRET }}
- steps:
- - name: Check required variables
- run: |
- if [ -z "${{ vars.APP_BASE_URL }}" ]; then
- echo "Error: APP_BASE_URL is not set. Please set this variable in your GitHub repository settings."
- exit 1
- fi
- echo "BASE_URL=${{ vars.APP_BASE_URL }}" >> $GITHUB_ENV
- - name: Mark inactive polls as deleted
- run: |
- curl -X "POST" --fail "${BASE_URL}/api/house-keeping/delete-inactive-polls" \
- -H "Authorization: Bearer ${API_SECRET}" \
- --max-time 300
- - name: Remove deleted polls
- run: |
- curl -X "POST" --fail "${BASE_URL}/api/house-keeping/remove-deleted-polls" \
- -H "Content-Type: application/json" \
- -H "Authorization: Bearer ${API_SECRET}" \
- --max-time 300
diff --git a/apps/web/.env.test b/apps/web/.env.test
index 1c65a0718..4800289d6 100644
--- a/apps/web/.env.test
+++ b/apps/web/.env.test
@@ -7,5 +7,5 @@ [email protected]
SMTP_HOST=0.0.0.0
SMTP_PORT=1025
QUICK_CREATE_ENABLED=true
-API_SECRET=1234567890abcdef1234567890abcdef1234
+CRON_SECRET=1234567890abcdef1234567890abcdef1234
diff --git a/apps/web/src/app/api/house-keeping/[...method]/route.ts b/apps/web/src/app/api/house-keeping/[...method]/route.ts
new file mode 100644
index 000000000..9fffa5290
--- /dev/null
+++ b/apps/web/src/app/api/house-keeping/[...method]/route.ts
@@ -0,0 +1,127 @@
+import { prisma } from "@rallly/database";
+import { Hono } from "hono";
+import { bearerAuth } from "hono/bearer-auth";
+import { handle } from "hono/vercel";
+
+const BATCH_SIZE = 100;
+
+const app = new Hono().basePath("/api/house-keeping");
+
+app.use("*", async (c, next) => {
+ if (process.env.CRON_SECRET) {
+ return bearerAuth({ token: process.env.CRON_SECRET })(c, next);
+ }
+
+ return c.json(
+ {
+ error: "CRON_SECRET is not set in environment variables",
+ },
+ 500,
+ );
+});
+
+/**
+ * Marks inactive polls as deleted. Polls are inactive if they have not been
+ * touched in the last 30 days and all dates are in the past.
+ * Only marks polls as deleted if they belong to users without an active subscription
+ * or if they don't have a user associated with them.
+ */
+app.get("/delete-inactive-polls", async (c) => {
+ // Define the 30-day threshold once
+ const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
+
+ // Mark inactive polls as deleted in a single query
+ const { count: markedDeleted } = await prisma.poll.updateMany({
+ where: {
+ deleted: false,
+ // All poll dates are in the past
+ options: {
+ none: {
+ startTime: { gt: new Date() },
+ },
+ },
+ // We don't delete polls that belong to a space with an active subscription
+ OR: [
+ { spaceId: null },
+ {
+ space: {
+ subscription: null,
+ },
+ },
+ {
+ space: {
+ subscription: {
+ active: false,
+ },
+ },
+ },
+ ],
+ // Poll is inactive (not touched AND not viewed in the last 30 days)
+ touchedAt: { lt: thirtyDaysAgo },
+ views: {
+ none: {
+ viewedAt: { gte: thirtyDaysAgo },
+ },
+ },
+ },
+ data: {
+ deleted: true,
+ deletedAt: new Date(),
+ },
+ });
+
+ return c.json({
+ success: true,
+ summary: {
+ markedDeleted,
+ },
+ });
+});
+
+/**
+ * Remove polls and corresponding data that have been marked deleted for more than 7 days.
+ */
+app.get("/remove-deleted-polls", async (c) => {
+ // First get the ids of all the polls that have been marked as deleted for at least 7 days
+ let totalDeletedPolls = 0;
+ let hasMore = true;
+
+ const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+
+ while (hasMore) {
+ const batch = await prisma.poll.findMany({
+ where: {
+ deleted: true,
+ deletedAt: {
+ lt: sevenDaysAgo,
+ },
+ },
+ select: { id: true },
+ take: BATCH_SIZE,
+ });
+
+ if (batch.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ const deleted = await prisma.poll.deleteMany({
+ where: {
+ id: { in: batch.map((poll) => poll.id) },
+ },
+ });
+
+ totalDeletedPolls += deleted.count;
+ }
+
+ return c.json({
+ success: true,
+ summary: {
+ deleted: {
+ polls: totalDeletedPolls,
+ },
+ },
+ });
+});
+
+export const GET = handle(app);
diff --git a/apps/web/src/app/api/house-keeping/delete-inactive-polls/route.ts b/apps/web/src/app/api/house-keeping/delete-inactive-polls/route.ts
deleted file mode 100644
index 6d8dc67cb..000000000
--- a/apps/web/src/app/api/house-keeping/delete-inactive-polls/route.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { prisma } from "@rallly/database";
-import { NextResponse } from "next/server";
-
-import { checkApiAuthorization } from "@/utils/api-auth";
-
-/**
- * Marks inactive polls as deleted. Polls are inactive if they have not been
- * touched in the last 30 days and all dates are in the past.
- * Only marks polls as deleted if they belong to users without an active subscription
- * or if they don't have a user associated with them.
- */
-export async function POST() {
- const unauthorized = await checkApiAuthorization();
- if (unauthorized) return unauthorized;
-
- // Define the 30-day threshold once
- const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
-
- // Mark inactive polls as deleted in a single query
- const { count: markedDeleted } = await prisma.poll.updateMany({
- where: {
- deleted: false,
- // All poll dates are in the past
- options: {
- none: {
- startTime: { gt: new Date() },
- },
- },
- // We don't delete polls that belong to a space with an active subscription
- OR: [
- { spaceId: null },
- {
- space: {
- subscription: null,
- },
- },
- {
- space: {
- subscription: {
- active: false,
- },
- },
- },
- ],
- // Poll is inactive (not touched AND not viewed in the last 30 days)
- touchedAt: { lt: thirtyDaysAgo },
- views: {
- none: {
- viewedAt: { gte: thirtyDaysAgo },
- },
- },
- },
- data: {
- deleted: true,
- deletedAt: new Date(),
- },
- });
-
- return NextResponse.json({
- success: true,
- summary: {
- markedDeleted,
- },
- });
-}
diff --git a/apps/web/src/app/api/house-keeping/remove-deleted-polls/route.ts b/apps/web/src/app/api/house-keeping/remove-deleted-polls/route.ts
deleted file mode 100644
index a8959d89f..000000000
--- a/apps/web/src/app/api/house-keeping/remove-deleted-polls/route.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-import { prisma } from "@rallly/database";
-import { NextResponse } from "next/server";
-
-import { checkApiAuthorization } from "@/utils/api-auth";
-
-const BATCH_SIZE = 100;
-
-/**
- * Remove polls and corresponding data that have been marked deleted for more than 7 days.
- */
-export async function POST() {
- const unauthorized = await checkApiAuthorization();
- if (unauthorized) return unauthorized;
-
- // First get the ids of all the polls that have been marked as deleted for at least 7 days
- let totalDeletedPolls = 0;
- let hasMore = true;
-
- const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
-
- while (hasMore) {
- const batch = await prisma.poll.findMany({
- where: {
- deleted: true,
- deletedAt: {
- lt: sevenDaysAgo,
- },
- },
- select: { id: true },
- take: BATCH_SIZE,
- });
-
- if (batch.length === 0) {
- hasMore = false;
- break;
- }
-
- const deleted = await prisma.poll.deleteMany({
- where: {
- id: { in: batch.map((poll) => poll.id) },
- },
- });
-
- totalDeletedPolls += deleted.count;
- }
-
- return NextResponse.json({
- success: true,
- summary: {
- deleted: {
- polls: totalDeletedPolls,
- },
- },
- });
-}
diff --git a/apps/web/src/utils/api-auth.ts b/apps/web/src/utils/api-auth.ts
index fdd4da667..9927262b0 100644
--- a/apps/web/src/utils/api-auth.ts
+++ b/apps/web/src/utils/api-auth.ts
@@ -2,14 +2,14 @@ import { headers } from "next/headers";
import { NextResponse } from "next/server";
/**
- * Checks if the request is authorized using the API secret
+ * Checks if the request is authorized using the cron secret for Vercel cron jobs
* @returns A NextResponse with 401 status if unauthorized, or null if authorized
*/
export async function checkApiAuthorization() {
const headersList = await headers();
const authorization = headersList.get("authorization");
- if (authorization !== `Bearer ${process.env.API_SECRET}`) {
+ if (authorization !== `Bearer ${process.env.CRON_SECRET}`) {
return NextResponse.json(
{ success: false, message: "Unauthorized" },
{ status: 401 },
diff --git a/apps/web/tests/house-keeping.spec.ts b/apps/web/tests/house-keeping.spec.ts
index 8a60d1558..c7055f516 100644
--- a/apps/web/tests/house-keeping.spec.ts
+++ b/apps/web/tests/house-keeping.spec.ts
@@ -14,7 +14,7 @@ test.describe("House-keeping API", () => {
const createdUserIds: string[] = [];
// API Secret for authentication
- const API_SECRET = process.env.API_SECRET;
+ const CRON_SECRET = process.env.CRON_SECRET;
test.beforeAll(async () => {
// Clean up any existing test data
@@ -130,11 +130,11 @@ test.describe("House-keeping API", () => {
createdPollIds.push(oldPollWithRecentViews.id);
// Call the delete-inactive-polls endpoint
- const response = await request.post(
+ const response = await request.get(
`${baseURL}/api/house-keeping/delete-inactive-polls`,
{
headers: {
- Authorization: `Bearer ${API_SECRET}`,
+ Authorization: `Bearer ${CRON_SECRET}`,
},
},
);
@@ -218,13 +218,12 @@ test.describe("House-keeping API", () => {
createdPollIds.push(recentDeletedPoll.id);
// Call the remove-deleted-polls endpoint
- const response = await request.post(
+ const response = await request.get(
`${baseURL}/api/house-keeping/remove-deleted-polls`,
{
headers: {
- Authorization: `Bearer ${API_SECRET}`,
+ Authorization: `Bearer ${CRON_SECRET}`,
},
- data: {}, // Empty JSON body
},
);
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index 3a9809705..d42be6df8 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -1,5 +1,16 @@
{
+ "$schema": "https://openapi.vercel.sh/vercel.json",
"installCommand": "pnpm install",
"buildCommand": "cd ../.. && pnpm db:generate && pnpm build:web && pnpm db:deploy",
- "outputDirectory": ".next"
+ "outputDirectory": ".next",
+ "crons": [
+ {
+ "path": "/api/house-keeping/delete-inactive-polls",
+ "schedule": "0 6 * * *"
+ },
+ {
+ "path": "/api/house-keeping/remove-deleted-polls",
+ "schedule": "30 6 * * *"
+ }
+ ]
}
diff --git a/turbo.json b/turbo.json
index 9838a02ef..2e3b8697c 100644
--- a/turbo.json
+++ b/turbo.json
@@ -63,19 +63,13 @@
"globalEnv": [
"ALLOWED_EMAILS",
"ANALYZE",
- "API_SECRET",
"AWS_ACCESS_KEY_ID",
"AWS_REGION",
"AWS_SECRET_ACCESS_KEY",
- "S3_BUCKET_NAME",
- "S3_ENDPOINT",
- "S3_ACCESS_KEY_ID",
- "S3_SECRET_ACCESS_KEY",
- "S3_REGION",
+ "CRON_SECRET",
"DATABASE_URL",
"DISABLE_LANDING_PAGE",
"EMAIL_PROVIDER",
- "MODERATION_ENABLED",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
"KV_REST_API_URL",
@@ -83,6 +77,7 @@
"MICROSOFT_CLIENT_ID",
"MICROSOFT_CLIENT_SECRET",
"MICROSOFT_TENANT_ID",
+ "MODERATION_ENABLED",
"NEXT_PUBLIC_*",
"NEXT_RUNTIME",
"NODE_ENV",
@@ -90,18 +85,30 @@
"NOREPLY_EMAIL",
"OIDC_CLIENT_ID",
"OIDC_CLIENT_SECRET",
- "OIDC_ISSUER_URL",
"OIDC_DISCOVERY_URL",
"OIDC_EMAIL_CLAIM_PATH",
+ "OIDC_ISSUER_URL",
"OIDC_NAME_CLAIM_PATH",
"OIDC_NAME",
"OIDC_PICTURE_CLAIM_PATH",
"OPENAI_API_KEY",
"PORT",
+ "QSTASH_CURRENT_SIGNING_KEY",
+ "QSTASH_NEXT_SIGNING_KEY",
+ "QSTASH_TOKEN",
+ "QSTASH_URL",
+ "QUICK_CREATE_ENABLED",
+ "S3_ACCESS_KEY_ID",
+ "S3_BUCKET_NAME",
+ "S3_ENDPOINT",
+ "S3_REGION",
+ "S3_SECRET_ACCESS_KEY",
"SECRET_PASSWORD",
"SENTRY_AUTH_TOKEN",
"SENTRY_DSN",
"SENTRY_IGNORE_API_RESOLUTION_ERROR",
+ "SENTRY_ORG",
+ "SENTRY_PROJECT",
"SKIP_ENV_VALIDATION",
"SMTP_HOST",
"SMTP_PORT",
@@ -113,13 +120,6 @@
"STRIPE_SECRET_KEY",
"STRIPE_SIGNING_SECRET",
"STRIPE_YEARLY_PRICE",
- "SENTRY_ORG",
- "SENTRY_PROJECT",
- "SUPPORT_EMAIL",
- "QSTASH_TOKEN",
- "QSTASH_CURRENT_SIGNING_KEY",
- "QSTASH_NEXT_SIGNING_KEY",
- "QSTASH_URL",
- "QUICK_CREATE_ENABLED"
+ "SUPPORT_EMAIL"
]
}