Workout Window Cron Setup
This document explains how to set up automated daily chain generation for the Workout Window feature.
Overview
The Workout Window feature requires daily chain generation to match users based on their workout windows and locations. This process should run automatically each day.
Endpoints
Cron Endpoint (Protected)
POST /api/workout-window/cron/generate-chains
This endpoint is protected by a secret token and should be called by your cron service.
Headers:
x-cron-secret: Secret token (set viaWORKOUT_WINDOW_CRON_SECRETenv var)
Body (optional):
{
"date": "2024-01-15" // Optional: specific date, defaults to today and tomorrow
}
Response:
{
"success": true,
"message": "Chains generated for today and tomorrow",
"today": "2024-01-15",
"tomorrow": "2024-01-16"
}
Manual Endpoint (Authenticated)
POST /api/workout-window/generate-chains
This endpoint requires user authentication and can be used for manual testing or admin operations.
Body (optional):
{
"date": "2024-01-15" // Optional: specific date, defaults to today
}
Environment Variables
Add to your backend .env file:
WORKOUT_WINDOW_CRON_SECRET=your-secret-token-here
Cron Service Setup
Vercel Cron
If using Vercel, add to vercel.json:
{
"crons": [
{
"path": "/api/workout-window/cron/generate-chains",
"schedule": "0 0 * * *"
}
]
}
And create a serverless function at apps/backend/src/api/workout-window/cron/generate-chains.ts that calls the endpoint.
Railway Cron
If using Railway, you can use their cron job feature or set up an external cron service.
External Cron Service
You can use services like:
Configure them to make a POST request to:
https://your-backend-url.com/api/workout-window/cron/generate-chains
With header:
x-cron-secret: your-secret-token-here
Recommended Schedule
- Daily at midnight UTC: Generate chains for today and tomorrow
- Or: Run every 6 hours to catch new users who set up their windows
Testing
You can test the cron endpoint manually:
curl -X POST https://your-backend-url.com/api/workout-window/cron/generate-chains \
-H "x-cron-secret: your-secret-token-here" \
-H "Content-Type: application/json"
Or test the authenticated endpoint (requires login):
curl -X POST https://your-backend-url.com/api/workout-window/generate-chains \
-H "Authorization: Bearer your-token-here" \
-H "Content-Type: application/json"
Automatic Chain Generation
Chains are also automatically generated when:
- A user sets up their workout window (triggers generation for today and tomorrow)
- A user updates their workout window
This ensures immediate matching for new users.
Monitoring
Monitor the cron job to ensure:
- Chains are generated daily
- No errors occur during generation
- All active users are matched
Check your backend logs for:
[Daily Chain Generator] Starting chain generation for...[Daily Chain Generator] Successfully generated chains for...- Any error messages