# Cron & Connection Stats - Test Instructions

## Setup

### Step 1: All Services Must Be Stopped
```bash
php bin/stop-all-services.php
```

### Step 2: Start Cron Test (2-minute interval)
```bash
# PowerShell (Recommended - colored output)
.\bin\test-cron-2min.ps1

# Or CMD
.\bin\test-cron-3sec.bat
```

---

## What This Tests

### 1. **Cron Functionality**
- Auto-restart of crashed services every 2 minutes
- Service health monitoring
- Lock file management

### 2. **Connection Stats (Original Hourly)**
- Connections accumulate over time
- Stats log at the TOP OF EACH HOUR (e.g., 15:00, 16:00)
- View with: `grep "[DB][STATS]" storage/logs/2026-01-25/market-data.log`

---

## Expected Behavior

### First Run (Iteration #1)
```
[14:40:00] Iteration #1
🚀 Market Data Runner... ✅ Started
🚀 Trade Monitor... ✅ Started
🚀 Telegram Bot... ✅ Started
🚀 Order Status Poller... ✅ Started
Next run at: 14:42:00
```

### Subsequent Runs (Iteration #2, #3, etc.)
```
[14:42:00] Iteration #2
✅ market-data-runner.php is running (PID: 12345)
✅ trade-monitor-runner.php is running (PID: 12346)
✅ telegram-bot-runner.php is running (PID: 12347)
✅ order-status-poller.php is running (PID: 12348)
Next run at: 14:44:00
```

### If a Service Crashes
```
[14:44:00] Iteration #3
❌ market-data-runner.php is NOT running
🚀 Market Data Runner... ✅ Started (Restarted)
✅ Other services still running...
```

---

## Connection Stats

### Current Setup (Hourly Logging)
Stats will appear in logs at the top of each hour:

```bash
# Wait for next hour (e.g., 15:00) then check:
grep "\[DB\]\[STATS\]" storage/logs/2026-01-25/market-data.log
```

**Example Output:**
```
[2026-01-25 15:00:00] [DB][STATS] Hourly summary {
    "hour": "2026-01-25 15:00",
    "connections": 245,
    "reconnects": 1,
    "closes": 244,
    "net_open": 1
}
```

### Test Version (5-Minute Logging)
If you want faster feedback (every 5 minutes):

1. **Backup original:**
   ```bash
   copy app\Support\Database.php app\Support\Database_ORIGINAL.php
   ```

2. **Use test version:**
   ```bash
   copy app\Support\Database_TEST.php app\Support\Database.php
   ```

3. **Run test, then check logs:**
   ```bash
   # Stats will appear every 5 minutes (at :00, :05, :10, :15, etc.)
   grep "\[DB\]\[STATS\]" storage/logs/2026-01-25/market-data.log
   ```

4. **After testing, restore original:**
   ```bash
   copy app\Support\Database_ORIGINAL.php app\Support\Database.php
   ```

---

## Monitoring

### Real-time Log Watching
```bash
# All activity
tail -f storage/logs/2026-01-25/market-data.log

# Only connection stats
tail -f storage/logs/2026-01-25/market-data.log | grep STATS

# Only cron activity  
tail -f storage/logs/2026-01-25/market-data.log | grep "Started\|running"
```

### Check Service Status
```bash
# In another terminal
php bin/check-services-status.php
```

---

## How Long to Test

### Minimum Test Duration
- **15-20 minutes** (7-10 cron cycles at 2-min interval)
- This ensures:
  - Services stay alive
  - Auto-restart works
  - Connections accumulate
  - No memory leaks

### For Connection Stats
- **Wait until next hour** to see hourly stats
- OR use test version (5-min intervals)

---

## What to Look For

### ✅ Good Signs
- Services start successfully on first run
- Services stay "running" on subsequent checks
- Auto-restart works if you manually kill a process
- Memory usage stable
- No connection errors

### ❌ Bad Signs
- Services crash repeatedly
- Lock file errors
- Connection errors
- Memory growing constantly

---

## Stop Test

**Press `Ctrl+C`** in the cron test window

Then verify all services are stopped:
```bash
php bin/check-services-status.php
```

If needed, force stop:
```bash
php bin/stop-all-services.php
```

---

## After Testing

### 1. Review Results
```bash
# Connection stats (if using test version)
grep "[DB][STATS]" storage/logs/2026-01-25/market-data.log

# Any errors
grep "ERROR" storage/logs/2026-01-25/*.log
```

### 2. If Test Version Was Used
```bash
# Restore original Database.php
copy app\Support\Database_ORIGINAL.php app\Support\Database.php
```

### 3. Return to Normal Operation
```bash
# Stop test cron (Ctrl+C)
# Start services normally
php bin/start-all-services.php

# Or use production cron (every 1 minute)
# Add to Windows Task Scheduler
```

---

## Files Created

- `bin/test-cron-2min.ps1` - PowerShell test script (2 min interval)
- `bin/test-cron-3sec.bat` - Batch test script (fallback)
- `app/Support/Database_TEST.php` - 5-minute stats version (optional)
- `THIS_FILE.md` - Test instructions

---

**Status:** Ready to test! 🚀
