# cPanel Cron Setup

This project needs **two cron jobs** to function in production:

| Job                | Frequency       | Why                                                                       |
| ------------------ | --------------- | ------------------------------------------------------------------------- |
| Queue worker       | Every minute    | Picks `AnalyzeSubmission` jobs off the `jobs` table and runs them.        |
| Laravel scheduler  | Every minute    | Fires the monthly billing job (`billing:generate-invoices`) on the 1st.   |

Without these, uploaded analyses sit forever at "Analyse en cours…" and monthly invoices are never generated.

---

## 1. Find your PHP binary

Open **cPanel → Terminal** (or SSH) and run:

```bash
which php
```

Typical paths on cPanel hosts:

- `/usr/local/bin/php` (generic)
- `/usr/local/bin/ea-php82`
- `/opt/cpanel/ea-php82/root/usr/bin/php`
- `/usr/local/php82/bin/php`

Note your path — you'll need it in the cron lines below.

## 2. Find your project path

Still in the terminal:

```bash
pwd
# typically: /home/USERNAME/analyser   (or wherever you uploaded the code)
```

## 3. Add the cron lines

Go to **cPanel → Cron Jobs → Add New Cron Job**.

**Common settings (Linux cron syntax)**

- Minute: `*`
- Hour: `*`
- Day: `*`
- Month: `*`
- Weekday: `*`

This means "every minute, every hour, every day".

### Cron 1 — Queue worker

```bash
* * * * * PHP_BIN=/usr/local/bin/php /home/USERNAME/analyser/scripts/cron/queue-worker.sh
```

Replace `/usr/local/bin/php` with the path from step 1, and `/home/USERNAME/analyser` with the path from step 2.

### Cron 2 — Laravel scheduler

```bash
* * * * * PHP_BIN=/usr/local/bin/php /home/USERNAME/analyser/scripts/cron/schedule-runner.sh
```

## 4. Verify it's working

Wait one minute, then check the worker log:

```bash
tail -f /home/USERNAME/analyser/storage/logs/cron-queue.log
```

Upload a test analysis via the UI. Within a minute you should see the worker pick it up, run it, and the analysis status flip from `pending` → `running` → `done`.

If you see no activity at all:

1. **Check the file is executable**: `chmod +x /home/USERNAME/analyser/scripts/cron/queue-worker.sh`
2. **Check the PHP path works**: `/usr/local/bin/php --version`
3. **Run the script manually**: `/home/USERNAME/analyser/scripts/cron/queue-worker.sh` and read the error.
4. **Check cron mail** in `/home/USERNAME/mail/cur/` — cron mails errors there by default.

## 5. After deploying new code

Force the worker to reload by running once over SSH after a `git pull`:

```bash
cd /home/USERNAME/analyser
php artisan queue:restart
```

Existing workers finish their current job then exit; the next cron tick boots a fresh one with the new code.

---

## Local development

You don't need crons in dev — just run the worker manually in a dedicated terminal:

```bash
php artisan queue:work --queue=analyses,analyses-priority,default --tries=3 --timeout=300
```

Leave it running. Restart it after code changes (Ctrl+C, re-run).
