Added redis and worker services

This commit is contained in:
blaisadmin
2026-05-02 00:14:56 -04:00
parent 5a3ca9021a
commit 673df353b9
15 changed files with 833 additions and 15 deletions
+63 -1
View File
@@ -18,6 +18,8 @@ FlockPal is a Dockerized TypeScript app for tracking flock health with a clean,
- Vet visit history with notes
- Postgres-backed storage
- React frontend and Express backend
- Separate worker process for scheduled reminders and background tasks
- Redis-backed background queue for scheduled reminders
- Security-minded defaults like Helmet, CORS allow-listing, rate limiting, and input validation
## Planned next steps
@@ -43,6 +45,40 @@ Full API documentation is available in [docs/API_REFERENCE.md](docs/API_REFERENC
The default `docker-compose.yml` is development-only. It mounts source files, installs dev dependencies, and runs the backend and frontend in watch mode.
## Operations
### Backups
Create a compressed Postgres backup from the Docker Compose Postgres service:
```bash
./scripts/backup-postgres.sh
```
By default this uses `docker-compose.prod.yml` and writes to `backups/postgres/`. Override with `COMPOSE_FILE` or `BACKUP_DIR`:
```bash
COMPOSE_FILE=docker-compose.yml BACKUP_DIR=/srv/flockpal-backups ./scripts/backup-postgres.sh
```
Test a backup by restoring it into a temporary database in the same Postgres container:
```bash
./scripts/restore-test-postgres.sh backups/postgres/flockpal-YYYYMMDDTHHMMSSZ.dump
```
The restore test creates a temporary database, runs `pg_restore`, verifies it can query `workspaces`, and then drops the temporary database. It does not overwrite the live database.
### Metrics and logs
The backend writes HTTP access logs through Morgan. Production uses the standard combined log format so Docker, Traefik, or a log shipper can collect it from container stdout.
Admin users can fetch lightweight process/request metrics:
```bash
curl -H "Authorization: Bearer <admin-token>" https://your-host/api/metrics
```
## Production
1. Set production values in your environment or `.env`, especially:
@@ -50,6 +86,8 @@ The default `docker-compose.yml` is development-only. It mounts source files, in
- `FRONTEND_URL`
- `BACKEND_URL`
- `VITE_API_BASE_URL`
- `REDIS_URL`
- `RESCUE_ONBOARDING_WEBHOOK_URL`
2. Build and start the production stack:
```bash
@@ -57,7 +95,31 @@ docker compose -f docker-compose.prod.yml up --build -d
```
3. The production backend runs the compiled Node app from `dist/app.js`.
4. The production frontend is built with Vite and served by Nginx on port `3000`.
4. The production worker runs `dist/worker.js` and owns scheduled reminder execution.
5. The production frontend is built with Vite and served by Nginx on port `3000`.
## Redis
Compose includes a Redis service at `redis://redis:6379` and passes that value through `REDIS_URL` to the backend and worker. Redis uses append-only persistence under `data/redis/`.
Scheduled milestone reminders are enqueued through Redis with a per-date job id, then processed by the worker. This keeps scheduled work out of API containers and prevents duplicate scheduled jobs when the API is scaled horizontally. Redis can also support later shared rate-limit state and short-lived cache entries.
## Worker process
The API container does not run scheduled reminder loops. Background reminders run in the `worker` service so the API can be scaled horizontally without multiple API containers sending duplicate scheduled emails.
Run the worker locally through Compose:
```bash
docker compose up worker
```
Run the compiled worker directly from the backend package:
```bash
npm run build
npm run worker
```
## Auth and flock notes