Overview
The domain sync worker automatically fetches domain and DNS information from Cloudflare every 5 minutes and stores it in the database. This provides fast page loads and reduces API calls. Uses Redis/BullMQ for job scheduling.
Features
- ✅ Syncs all Cloudflare domains every 5 minutes
- ✅ Fetches DNS records (A, AAAA, CNAME, MX, TXT, NS, etc.)
- ✅ Extracts and stores nameservers
- ✅ Automatically finds and stores Cloudflare zone IDs
- ✅ Tenant-aware - links domains to customers and tenants
- ✅ Manual sync trigger via API (root tenant only)
- ✅ Uses Redis/BullMQ for reliable job scheduling
Starting the Worker
Development
Production (PM2)
pm2 start domainSyncWorker.js --name "domain-sync"
pm2 save
Check Status
pm2 status domain-sync
pm2 logs domain-sync
Manual Sync Trigger
Root tenant users can manually trigger a sync from the UI:
- Go to Services → Domains tab
- Click the "Sync Domains" button (visible only to root users)
Or via API:
curl -X POST https://your-api.com/domains/sync \
-H "Authorization: Bearer YOUR_ROOT_TOKEN"
Database Structure
The worker updates these fields in the domains table:
- dns_records (JSONB) - Array of DNS records
- nameservers (JSONB) - Array of nameserver hostnames
- registrar_domain_id (VARCHAR) - Cloudflare zone ID
- updated_at (TIMESTAMP) - Last sync time
Requirements
Environment variables needed:
- CLOUDFLARE_API_TOKEN - Cloudflare API token with DNS read permissions
- REDIS_HOST - Redis server host (Digital Ocean droplet)
- REDIS_PORT - Redis server port (default: 6379)
- REDIS_USERNAME - Redis username (if ACL enabled)
- REDIS_PASSWORD - Redis password
- REDIS_DB - Redis database number (default: 0)
How It Works
- BullMQ Worker connects to Redis on the Digital Ocean droplet
- Every 5 minutes, a recurring job is triggered
- The job queries the database for all Cloudflare domains
- For each domain:
- Checks if it has a zone_id stored
- If not, fetches zone_id from Cloudflare API
- Fetches all DNS records for that zone
- Extracts nameservers from NS records
- Updates the database with the latest information
- Job completion/failure is logged
- Redis handles job scheduling, retries, and failure recovery
Troubleshooting
No domains syncing?
- Check that domains have ‘registrar = 'cloudflare’ in database
- Ensure CLOUDFLARE_API_TOKEN is set
- Check worker logs: pm2 logs domain-sync`
- Verify Redis connection is working
Zone ID not found?
- Domain may not exist in Cloudflare yet
- Check domain is in your Cloudflare account
- Verify API token has correct permissions
DNS records not updating?
- Check Cloudflare API rate limits
- Verify zone_id is correct in database
- Check worker logs for errors
Redis connection issues?
- Verify REDIS_HOST and REDIS_PORT are correct
- Check Redis is running on Digital Ocean droplet
- Test connection: redis-cli -h $REDIS_HOST -p $REDIS_PORT ping
- Verify ACL credentials if Redis authentication is enabled