# Laravel Scheduler Configuration untuk Watch History

## 🕐 Scheduled Tasks yang Ditambahkan

### 1. **Daily Cleanup** (Setiap Hari jam 2 pagi)
```php
Schedule::command('watch-history:cleanup')
    ->daily()
    ->at('02:00')
    ->withoutOverlapping()
    ->runInBackground()
```

### 2. **Weekly Statistics** (Setiap Minggu jam 3 pagi)
```php
Schedule::command('watch-history:cleanup --stats')
    ->weekly()
    ->sundays()
    ->at('03:00')
```

## ⚙️ Setup Cron Job di Server

### For Ubuntu/CentOS:
```bash
# Edit crontab
crontab -e

# Tambahkan line ini (ganti path sesuai project):
* * * * * cd /path/to/dracin && php artisan schedule:run >> /dev/null 2>&1
```

### For Shared Hosting:
```bash
# Di cPanel atau control panel hosting
* * * * * /usr/local/bin/php /home/username/public_html/artisan schedule:run
```

## 🧪 Testing Scheduler

### 1. **Manual Test**
```bash
# Test command langsung
php artisan watch-history:cleanup --dry-run

# Test scheduler
php artisan schedule:run

# Lihat daftar scheduled tasks
php artisan schedule:list
```

### 2. **Development Testing**
```bash
# Force run daily cleanup
php artisan schedule:run --verbose

# Test dengan artisan tinker
php artisan tinker
>>> Artisan::call('watch-history:cleanup', ['--dry-run' => true]);
```

## 📊 Monitoring & Logging

### Log Files yang Dipantau:
- `storage/logs/laravel.log` - Untuk success/failure logs
- `storage/logs/scheduler.log` - Khusus scheduler (optional)

### Log Messages:
- ✅ `Watch history cleanup completed successfully`
- ❌ `Watch history cleanup failed`

## 🔧 Konfigurasi Advanced

### Custom Schedule Times:
```php
// Cleanup setiap 6 jam
Schedule::command('watch-history:cleanup')->everySixHours();

// Cleanup hanya weekdays
Schedule::command('watch-history:cleanup')
    ->daily()
    ->weekdays()
    ->at('02:00');

// Cleanup dengan kondisi
Schedule::command('watch-history:cleanup')
    ->daily()
    ->when(function () {
        return config('drama.watch_history.enable_cleanup');
    });
```

### Environment-Specific:
```php
// Hanya production
Schedule::command('watch-history:cleanup')
    ->daily()
    ->environments(['production']);

// Skip di maintenance mode
Schedule::command('watch-history:cleanup')
    ->daily()
    ->skip(function () {
        return app()->isDownForMaintenance();
    });
```

## 🚨 Important Notes

1. **Cron Job Required**: Laravel scheduler membutuhkan cron job yang running setiap menit
2. **Timezone**: Pastikan server timezone sesuai dengan `config/app.php`
3. **Permissions**: Pastikan user cron memiliki akses ke project directory
4. **Logs**: Monitor logs untuk memastikan cleanup berjalan dengan baik
5. **Background**: Tasks berjalan di background untuk menghindari blocking

## ✅ Verification

Untuk memastikan scheduler bekerja:

```bash
# Check scheduled tasks
php artisan schedule:list

# Run scheduler manually
php artisan schedule:run

# Check logs
tail -f storage/logs/laravel.log
```

Schedule ini akan:
- 🕐 **Daily**: Cleanup otomatis data lama
- 📊 **Weekly**: Generate statistics report  
- 🔄 **Background**: Tidak mengganggu user experience
- 📝 **Logged**: Semua aktivitas tercatat

Perfect untuk production environment! 🚀