# Supervisor Setup (Production)

این راهنما برای اجرای صف‌های لاراول و Scheduler با `Supervisor` است.

> بخشی از [deployment.md](deployment.md) — این سند جزئیات بیشتر Supervisor را پوشش می‌دهد.

## 1) پیش‌نیاز

- مسیر پروژه روی سرور: `/var/www/cheraghbot`
- PHP CLI در مسیر: `/usr/bin/php`
- Supervisor نصب باشد.

Ubuntu / Debian:

```bash
sudo apt update
sudo apt install -y supervisor
sudo systemctl enable --now supervisor
```

## 2) قرار دادن کانفیگ

فایل آماده پروژه:

- `deploy/supervisor/cheraghbot.conf`

کپی روی سرور:

```bash
sudo cp /var/www/cheraghbot/deploy/supervisor/cheraghbot.conf /etc/supervisor/conf.d/cheraghbot.conf
```

اگر مسیر پروژه یا کاربر وب‌سرور شما متفاوت است، داخل فایل `cheraghbot.conf` این موارد را تغییر بده:

- `directory=...`
- `user=...`
- `stdout_logfile=...`
- `command=...` (در صورت تفاوت مسیر PHP)

## 3) اعمال و اجرا

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
```

استارت/ری‌استارت:

```bash
sudo supervisorctl start cheraghbot-queue-default:*
sudo supervisorctl start cheraghbot-queue-inventory:*
sudo supervisorctl start cheraghbot-queue-bot-heavy:*
sudo supervisorctl start cheraghbot-queue-bot-webhooks:*
sudo supervisorctl start cheraghbot-queue-ai:*
sudo supervisorctl start cheraghbot-queue-manual-runs:*
sudo supervisorctl start cheraghbot-scheduler
```

یا:

```bash
sudo supervisorctl restart all
```

## 4) بعد از هر Deploy

برای Reload امن workerها:

```bash
php artisan optimize:clear
php artisan queue:restart
sudo supervisorctl restart cheraghbot-queue-default:*
sudo supervisorctl restart cheraghbot-queue-inventory:*
sudo supervisorctl restart cheraghbot-queue-bot-heavy:*
sudo supervisorctl restart cheraghbot-queue-bot-webhooks:*
sudo supervisorctl restart cheraghbot-queue-ai:*
sudo supervisorctl restart cheraghbot-queue-manual-runs:*
```

## 5) صف‌های پروژه

- `default` — عمومی
- `inventory` — آپلود/خروجی Excel
- `bot-heavy` — ارسال پیام، webhook setup، keyboard deploy
- `bot-webhooks` — پردازش ورودی webhook/poll (**حیاتی**)
- `ai` — پاسخ و خلاصه‌سازی AI
- `manual-runs` — اجراهای دستی ادمین

کانفیگ آماده، برای هر صف process جدا دارد تا تسک‌های سنگین `bot-heavy` باعث کندی webhook ingestion نشوند.
