# Genie Cron Infrastructure

This cron system is batch-based, lock-protected, idempotent, and scalable to 100k+ products.
It supports Slack alerts (with email fallback), is safe to run daily, and is designed for long-running production workloads.

## Core Guarantees

- CLI-only execution (`php_sapi_name() === 'cli'`)
- Bootstrap-required DB access (`require_once ../bootstrap.php`, with `$pdo`)
- High-capacity runtime settings (`memory_limit=1024M`, unlimited execution time)
- Lock protection with stale-lock cleanup (3-hour stale threshold)
- Transaction-per-batch processing
- Exit code contract (`0` success, `1` failure)

## Config

Defined in `cron_config.php`:

- `BATCH_SIZE` (default `500`)
- `SLACK_WEBHOOK_URL` (default empty)
- `CRON_ALERT_EMAIL` (default `dev@domain.com`)
- `MAX_RUNTIME_SECONDS` (default `14400`, 4 hours)

## Execution Order

`cron_master.php` executes:

1. `cron_product_sync.php`
2. `cron_fitment_worker.php`
3. `cron_universal_guidance.php`
4. `cron_health_check.php`

If any child exits non-zero, `cron_master.php` alerts and exits `1`.

## Script Roles

- `cron_product_sync.php`: Marks products for deterministic reprocessing in batches.
- `cron_fitment_worker.php`: Rebuilds `product_fitment` from `turn14_fitment_raw` in batches.
- `cron_universal_guidance.php`: Generates universal guidance content for dirty universal products in batches.
- `cron_health_check.php`: Monitors backlog thresholds and alerts when they exceed safe limits.

## Daily Cron Entry

```cron
0 2 * * * php /var/www/html/genie/cron/cron_master.php >> /var/log/genie_cron.log 2>&1
```

## Dry-Run Mode

Use `--dry-run` to validate selection counts and pipeline flow without mutating data:

```bash
php /var/www/html/genie/cron/cron_master.php --dry-run
```

In dry-run mode:

- Worker scripts still read batches using live queries.
- No `UPDATE`, `INSERT`, or `DELETE` statements are executed.
- Alerts are suppressed to avoid noisy notifications during validation.
