5.6 KiB
5.6 KiB
VS Code Debug Configuration for cleanOrphanedAppDetails.js
Overview
This document explains how to use the VS Code debug configurations for the cleanOrphanedAppDetails.js script.
Launch Configurations Added
1. Debug Clean Orphaned AppDetails - Safe Test
- Purpose: Safest option for testing and development
- Environment: Uses
environment.env - Mode:
--dry-run --check-only(no actual deletions) - Scope: Single year (2024) with estimation
- Memory: 2GB allocation
- Best for: Initial testing, development, debugging logic
2. Debug Clean Orphaned AppDetails - Production Run
- Purpose: Production-ready execution with optimizations
- Environment: Uses
environment_prod.env - Mode: Real execution (will delete orphaned records)
- Scope: Full year (2024) with skip counting strategy
- Memory: 4GB allocation
- Best for: Production cleanup operations
3. Debug Clean Orphaned AppDetails - Date Range
- Purpose: Process specific date ranges
- Environment: Uses
environment.env - Mode:
--dry-runwith date range (Q1 2024) - Scope: Custom date range with estimation
- Best for: Testing specific time periods, troubleshooting date ranges
4. Debug Clean Orphaned AppDetails - Custom Env Vars
- Purpose: Maximum flexibility using environment variables
- Environment: Uses
environment.env+ custom env vars in launch config - Mode: Fully configurable via environment variables
- Best for: Custom configurations, parameter testing
Environment Files
Primary Files
environment.env: Development/test databaseenvironment_prod.env: Production database.env.debug: Custom debug file (created for you)
Creating Your Debug Environment
-
Copy existing environment file:
cp environment.env .env.debug -
Modify
.env.debugfor your needs:# Safe defaults for debugging DRY_RUN=true CHECK_ONLY=true COUNTING_STRATEGY=estimate SPECIFIC_YEAR=2024 DEBUG=agm:clean-orphaned-details
How to Debug
Step 1: Choose Configuration
- Open VS Code
- Go to Run and Debug (Ctrl+Shift+D)
- Select one of the "Debug Clean Orphaned AppDetails" configurations
Step 2: Set Breakpoints
- Line 361: Before counting strategy decision
- Line 390: In
estimateDocumentCount()function - Line 450: In batch processing loop
- Line 520: In
deleteBatch()function
Step 3: Start Debugging
- Press F5 or click the play button
- Monitor the integrated terminal for output
- Use VS Code debugging features (variables, call stack, etc.)
Configuration Details
Command Line Arguments vs Environment Variables
// Priority order (highest to lowest):
1. Command line arguments (--dry-run, --specific-year=2024)
2. Environment variables in launch config "env" section
3. Environment variables from envFile
4. Default values in script
Memory Configuration
- Development: 2GB (
--max-old-space-size=2048) - Production: 4GB (
--max-old-space-size=4096) - Adjust based on your dataset size
Debug Output Filtering
# See only script output
DEBUG=agm:clean-orphaned-details
# See all AGM debug output
DEBUG=agm:*
# See specific modules
DEBUG=agm:clean-orphaned-details,agm:db
Debugging Scenarios
Scenario 1: Test New Counting Strategy
{
"name": "Test Counting Strategy",
"args": ["--counting-strategy=estimate", "--specific-year=2024", "--dry-run"]
}
Scenario 2: Debug Memory Issues
{
"runtimeArgs": [
"--expose-gc",
"--max-old-space-size=4096",
"--trace-gc"
]
}
Scenario 3: Debug Database Connection
{
"env": {
"DEBUG": "agm:*",
"DB_MAX_POOLSIZE": "4"
}
}
Troubleshooting
Common Issues
-
"DB_HOSTS environment variable is required"
- Check your envFile path
- Ensure environment file exists and has DB_HOSTS
-
Out of Memory Errors
- Increase
--max-old-space-size - Use smaller batch sizes:
--batch-size=500
- Increase
-
Connection Timeouts
- Check database connectivity
- Verify environment file has correct DB credentials
-
No Debug Output
- Ensure
DEBUG=agm:clean-orphaned-detailsis set - Check VS Code debug console vs integrated terminal
- Ensure
Performance Debugging
-
Set breakpoints at key functions:
loadAppFileIds(): Memory loadingfindOrphanedAppDetailsForPeriod(): Main processingestimateDocumentCount(): Counting estimation
-
Monitor variables:
existingFileIds.size: AppFile cache sizeprocessed: Records processed countorphanedRecords.length: Orphaned records found
-
Watch expressions:
process.memoryUsage()Date.now() - periodStartTime.getTime()Math.floor(processed / (elapsedSeconds || 1))
Example Debugging Session
- Start with Safe Test configuration
- Set breakpoint at line 390 (counting strategy)
- Step through counting logic
- Check
totalAppDetailsvalue - Verify progress reporting works
- Test different counting strategies
Safety Notes
- Always start with
--dry-run --check-only - Test on a subset first (
--specific-yearor date range) - Monitor memory usage during large operations
- Use development database first
- Backup production data before real cleanup
Customization
You can create additional launch configurations by copying and modifying existing ones. Common modifications:
{
"name": "My Custom Debug Config",
"args": ["--start-date=2024-06-01", "--end-date=2024-06-30"],
"env": {
"BATCH_SIZE": "2000",
"COUNTING_STRATEGY": "full"
}
}