|
1 | | -# Cloudflare Cron Setup for Article Cleanup |
| 1 | +# Article Cleanup Cron Setup |
2 | 2 |
|
3 | | -This document explains how to set up Cloudflare Cron Triggers to automatically delete expired articles. |
| 3 | +> **Note**: The Cloudflare Worker cron for article cleanup has been retired. |
| 4 | +> Article cleanup is now handled by an **Inngest cron function** that runs daily at 2:00 AM UTC. |
4 | 5 |
|
5 | | -## Overview |
| 6 | +## Current Setup |
6 | 7 |
|
7 | | -The system consists of: |
8 | | -1. **API Route**: `/api/cron/cleanup-articles` - Handles the actual cleanup logic |
9 | | -2. **Cloudflare Worker**: `src/workers/cron-worker.ts` - Cron trigger that calls the API |
10 | | -3. **Backend Service**: `src/backend/services/article-cleanup-service.ts` - Database operations |
| 8 | +Article cleanup runs as an Inngest cron function registered at `/api/inngest`. |
11 | 9 |
|
12 | | -## Setup Instructions |
| 10 | +- **Function ID**: `cleanup-expired-articles` |
| 11 | +- **Schedule**: `0 2 * * *` (daily at 2:00 AM UTC) |
| 12 | +- **Source**: `src/lib/inngest.ts` → `cleanupExpiredArticlesFn` |
| 13 | +- **Logic**: `src/backend/services/article-cleanup-service.ts` → `deleteExpiredArticles()` |
13 | 14 |
|
14 | | -### 1. Install Wrangler CLI |
| 15 | +## Manual Runs |
15 | 16 |
|
16 | | -```bash |
17 | | -npm install -g wrangler |
18 | | -# or |
19 | | -bun install -g wrangler |
20 | | -``` |
21 | | - |
22 | | -### 2. Authenticate with Cloudflare |
23 | | - |
24 | | -```bash |
25 | | -wrangler login |
26 | | -``` |
27 | | - |
28 | | -### 3. Configure Environment Variables |
29 | | - |
30 | | -Update `wrangler.toml` with your actual domain: |
31 | | - |
32 | | -```toml |
33 | | -[vars] |
34 | | -CRON_TARGET_URL = "https://your-actual-domain.com/api/cron/cleanup-articles" |
35 | | -``` |
36 | | - |
37 | | -### 4. Set Secret (Optional but Recommended) |
38 | | - |
39 | | -For additional security, set a secret that the cron worker will send: |
40 | | - |
41 | | -```bash |
42 | | -wrangler secret put CRON_SECRET |
43 | | -# Enter your secret when prompted |
44 | | -``` |
45 | | - |
46 | | -Then add the same secret to your Next.js environment: |
47 | | - |
48 | | -```bash |
49 | | -# .env.local |
50 | | -CRON_SECRET=your-secret-key |
51 | | -``` |
52 | | - |
53 | | -### 5. Deploy the Cron Worker |
54 | | - |
55 | | -```bash |
56 | | -wrangler deploy src/workers/cron-worker.ts |
57 | | -``` |
58 | | - |
59 | | -### 6. Verify Cron Schedule |
60 | | - |
61 | | -The cron is configured to run daily at 2:00 AM UTC. You can modify the schedule in `wrangler.toml`: |
62 | | - |
63 | | -```toml |
64 | | -[triggers] |
65 | | -crons = [ |
66 | | - "0 2 * * *" # Daily at 2:00 AM UTC |
67 | | -] |
68 | | -``` |
69 | | - |
70 | | -Other common schedules: |
71 | | -- `"0 * * * *"` - Every hour |
72 | | -- `"0 2 * * 0"` - Every Sunday at 2:00 AM |
73 | | -- `"0 2 1 * *"` - First day of every month at 2:00 AM |
74 | | - |
75 | | -### 7. Monitor Cron Executions |
76 | | - |
77 | | -You can monitor executions in the Cloudflare dashboard: |
78 | | -1. Go to Workers & Pages |
79 | | -2. Select your worker |
80 | | -3. Check the "Logs" tab for execution logs |
81 | | - |
82 | | -## Manual Testing |
83 | | - |
84 | | -You can test the cleanup manually by calling the API directly: |
85 | | - |
86 | | -```bash |
87 | | -# GET request for testing |
88 | | -curl https://your-domain.com/api/cron/cleanup-articles |
89 | | - |
90 | | -# POST request (mimics cron call) |
91 | | -curl -X POST https://your-domain.com/api/cron/cleanup-articles \ |
92 | | - -H "Content-Type: application/json" \ |
93 | | - -H "x-cron-secret: your-secret-key" |
94 | | -``` |
| 17 | +Trigger a manual run from the [Inngest dashboard](https://app.inngest.com) by invoking the `cleanup-expired-articles` function. |
95 | 18 |
|
96 | 19 | ## How It Works |
97 | 20 |
|
98 | 21 | 1. **Article Scheduling**: Articles can be scheduled for deletion by setting the `delete_scheduled_at` field |
99 | | -2. **Cron Trigger**: Cloudflare runs the worker daily at 2:00 AM UTC |
100 | | -3. **API Call**: Worker calls `/api/cron/cleanup-articles` endpoint |
101 | | -4. **Cleanup Logic**: Service finds articles where `delete_scheduled_at < current_time` and deletes them |
102 | | -5. **Response**: API returns count of deleted articles and their details |
103 | | - |
104 | | -## Database Schema |
105 | | - |
106 | | -Articles are scheduled for deletion using the `delete_scheduled_at` timestamp field: |
107 | | - |
108 | | -```sql |
109 | | --- Example of scheduling an article for deletion in 30 days |
110 | | -UPDATE articles |
111 | | -SET delete_scheduled_at = NOW() + INTERVAL '30 days' |
112 | | -WHERE id = 'article-id'; |
113 | | -``` |
114 | | - |
115 | | -## Additional Functions |
116 | | - |
117 | | -The cleanup service also provides: |
118 | | -- `scheduleArticleForDeletion(articleId, deleteAt)` - Schedule an article |
119 | | -- `cancelScheduledDeletion(articleId)` - Cancel scheduled deletion |
120 | | -- `getScheduledArticles()` - Get all articles scheduled for deletion |
121 | | - |
122 | | -## Troubleshooting |
123 | | - |
124 | | -### Common Issues |
125 | | - |
126 | | -1. **404 Error**: Check that your `CRON_TARGET_URL` is correct |
127 | | -2. **401 Unauthorized**: Verify `CRON_SECRET` matches between worker and API |
128 | | -3. **500 Error**: Check database connection and permissions |
129 | | -4. **No Articles Deleted**: Verify articles have `delete_scheduled_at` set and are past due |
130 | | - |
131 | | -### Debugging |
132 | | - |
133 | | -1. Check Cloudflare Worker logs in the dashboard |
134 | | -2. Check your Next.js application logs |
135 | | -3. Test the API endpoint manually |
136 | | -4. Verify database records with `getScheduledArticles()` |
| 22 | +2. **Inngest Cron**: Inngest triggers the function daily at 2:00 AM UTC |
| 23 | +3. **Cleanup Logic**: `deleteExpiredArticles()` finds articles where `delete_scheduled_at < current_time` and deletes them from the database and MeiliSearch |
137 | 24 |
|
138 | | -## Security Considerations |
| 25 | +## Required Environment Variables |
139 | 26 |
|
140 | | -1. Use `CRON_SECRET` to verify requests come from your worker |
141 | | -2. Consider rate limiting the endpoint |
142 | | -3. Log all cleanup operations for audit trails |
143 | | -4. Ensure proper database permissions for the cleanup operations |
| 27 | +- `INNGEST_EVENT_KEY` — Inngest event key (optional for local dev) |
| 28 | +- `INNGEST_SIGNING_KEY` — Inngest signing key (optional for local dev) |
0 commit comments