# Docker Scheduler Setup

## Overview

The scheduler runs as a separate Docker container that continuously executes scheduled tasks every 5 minutes.

## What's Configured

The `docker-compose.yml` now includes a `scheduler` service that:
- Runs `php artisan schedule:work` continuously
- Shares the same codebase as the backend
- Uses the same `.env` configuration
- Automatically restarts if it crashes
- Syncs data from Odoo every 5 minutes

## Docker Compose Configuration

```yaml
scheduler:
  build:
    context: ./afinet-portal-backend
  container_name: afinet-scheduler
  env_file:
    - ./afinet-portal-backend/.env
  restart: unless-stopped
  depends_on:
    - db
    - backend
  command: php artisan schedule:work
```

## Usage

### Start All Services (Including Scheduler)

```bash
docker-compose up -d
```

### Start Only Scheduler

```bash
docker-compose up -d scheduler
```

### Check Scheduler Status

```bash
docker-compose ps scheduler
```

### View Scheduler Logs

```bash
# Follow logs in real-time
docker-compose logs -f scheduler

# View last 100 lines
docker-compose logs --tail=100 scheduler
```

### Restart Scheduler

```bash
docker-compose restart scheduler
```

### Stop Scheduler

```bash
docker-compose stop scheduler
```

### Execute Commands in Scheduler Container

```bash
# Access container shell
docker-compose exec scheduler bash

# Run artisan commands
docker-compose exec scheduler php artisan schedule:list
docker-compose exec scheduler php artisan sync:data quotations
```

## What Gets Scheduled

The scheduler runs these tasks every 5 minutes:
- `php artisan sync:data quotations` - Syncs quotations from Odoo
- `php artisan sync:data invoices` - Syncs invoices from Odoo
- `php artisan sync:data orders` - Syncs sales orders from Odoo

## Verification

### 1. Check if scheduler is running

```bash
docker-compose ps
```

You should see:
```
NAME                 STATUS
afinet-scheduler     Up X minutes
```

### 2. Check logs for activity

```bash
docker-compose logs scheduler
```

You should see logs every 5 minutes showing sync activity.

### 3. Manually trigger sync (for testing)

```bash
docker-compose exec scheduler php artisan sync:data quotations
docker-compose exec scheduler php artisan sync:data invoices
docker-compose exec scheduler php artisan sync:data orders
```

### 4. Check database for updates

```bash
docker-compose exec backend php artisan tinker
>>> \App\Models\Quotation::latest()->first()->updated_at
>>> \App\Models\Invoice::latest()->first()->updated_at
```

## Production Deployment

### 1. Build and Start

```bash
# Build images
docker-compose build

# Start all services
docker-compose up -d

# Check status
docker-compose ps
```

### 2. Monitor

```bash
# View all logs
docker-compose logs -f

# View only scheduler logs
docker-compose logs -f scheduler

# Check resource usage
docker stats afinet-scheduler
```

### 3. Health Check

Create a health check script:

```bash
#!/bin/bash
# check-scheduler-health.sh

if docker-compose ps scheduler | grep -q "Up"; then
    echo "✓ Scheduler is running"
    exit 0
else
    echo "✗ Scheduler is not running!"
    # Send alert
    exit 1
fi
```

Add to cron:
```bash
*/15 * * * * /path/to/check-scheduler-health.sh
```

## Troubleshooting

### Scheduler not starting

```bash
# Check logs
docker-compose logs scheduler

# Check if backend is running
docker-compose ps backend

# Rebuild scheduler
docker-compose build scheduler
docker-compose up -d scheduler
```

### Database connection errors

```bash
# Check if database is running
docker-compose ps db

# Check database connection from scheduler
docker-compose exec scheduler php artisan tinker
>>> DB::connection()->getPdo();
```

### Odoo connection errors

```bash
# Test Odoo connection
docker-compose exec scheduler php artisan tinker
>>> app(\App\Services\OdooService::class)->authenticate();

# Check .env configuration
docker-compose exec scheduler cat .env | grep ODOO
```

### Scheduler not syncing

```bash
# Check schedule configuration
docker-compose exec scheduler php artisan schedule:list

# Manually trigger sync
docker-compose exec scheduler php artisan sync:data quotations --verbose

# Check Laravel logs
docker-compose exec scheduler tail -f storage/logs/laravel.log
```

## Updating

When you update the code:

```bash
# Pull latest code
git pull

# Rebuild and restart
docker-compose build scheduler
docker-compose up -d scheduler

# Or restart all services
docker-compose down
docker-compose up -d
```

## Scaling (Optional)

If you need multiple scheduler instances (not recommended, but possible):

```yaml
scheduler:
  build:
    context: ./afinet-portal-backend
  env_file:
    - ./afinet-portal-backend/.env
  restart: unless-stopped
  depends_on:
    - db
    - backend
  command: php artisan schedule:work
  deploy:
    replicas: 2  # Run 2 instances
```

**Note:** The `withoutOverlapping()` modifier in the schedule prevents duplicate task execution.

## Advantages of Docker Scheduler

✓ **Isolated** - Runs in its own container
✓ **Portable** - Works on any system with Docker
✓ **Easy to manage** - Simple docker-compose commands
✓ **Auto-restart** - Automatically restarts if it crashes
✓ **Consistent** - Same environment as backend
✓ **Scalable** - Easy to add more instances if needed
✓ **No system dependencies** - No need for cron, supervisor, or systemd

## Production Checklist

- [x] Scheduler service added to docker-compose.yml
- [ ] `.env` file configured with correct Odoo credentials
- [ ] Docker containers built: `docker-compose build`
- [ ] All services started: `docker-compose up -d`
- [ ] Scheduler status verified: `docker-compose ps scheduler`
- [ ] Logs checked: `docker-compose logs scheduler`
- [ ] Manual sync tested: `docker-compose exec scheduler php artisan sync:data quotations`
- [ ] Database updates verified
- [ ] Health check script created (optional)
- [ ] Monitoring alerts configured (optional)

## Quick Commands Reference

```bash
# Start
docker-compose up -d scheduler

# Stop
docker-compose stop scheduler

# Restart
docker-compose restart scheduler

# Logs
docker-compose logs -f scheduler

# Status
docker-compose ps scheduler

# Execute command
docker-compose exec scheduler php artisan [command]

# Shell access
docker-compose exec scheduler bash

# Rebuild
docker-compose build scheduler && docker-compose up -d scheduler
```

## Summary

With Docker, the scheduler setup is incredibly simple:
1. Add scheduler service to `docker-compose.yml` ✓ (Done)
2. Run `docker-compose up -d`
3. That's it!

The scheduler will now run continuously, syncing data from Odoo every 5 minutes, and will automatically restart if it crashes.
