# AGENTS.md — deploy/

Production deploy script and the process-supervision units for the WhatsApp CRM.
Exists because the recurring "works for a day then stops syncing" outage is always
one of: a dead queue worker, stale opcode/worker code after a deploy, or a dropped
Evolution session. Shell + systemd/supervisor — not PHP.

## Stack
- Bash (`set -euo pipefail`) for `deploy.sh`
- systemd units: `whatsapp-worker.service` (queue), `whatsapp-reverb.service` (WebSocket)
- supervisor alternative: `supervisor-whatsapp-worker.conf`
- Target host: cpanel, `ea-php83` PHP binary, app at `/home/raitoai/public_html/whatsapp`, git branch `master`
- Services driven: php-fpm (opcache), `whatsapp-worker`, `whatsapp-reverb`

## Structure
- `deploy.sh` — the one-shot deploy: `git pull` → `composer install --no-dev` → `migrate --force` → `optimize:clear` → restart php-fpm → restart worker → restart Reverb → print status. Edit here when a deploy step changes.
- `whatsapp-worker.service` — systemd unit for `queue:work`; recycles hourly via `--max-time=3600`, `Restart=always`.
- `supervisor-whatsapp-worker.conf` — the supervisor equivalent; use one supervisor, never both.
- `whatsapp-reverb.service` — systemd unit for `artisan reverb:start`; binds `REVERB_SERVER_HOST:REVERB_SERVER_PORT`.
- `README.md` — why supervision is required and how to install either option.
- `REVERB_SETUP.md` — end-to-end real-time wiring (env, service, proxy, client).

## Conventions
- Unit files ship as templates with the three host-specific lines (`User`, `WorkingDirectory`, `ExecStart`) marked for editing; they are copied to `/etc/systemd/system/`, not symlinked.
- Both worker definitions carry the identical `queue:work` flag set — change one, change the other.
- `Restart=always` + `RestartSec=3` on both services; the worker relies on `--max-time` recycling rather than a memory limit.
- Comment blocks at the top of each unit hold the install commands; keep that habit for new units.
- `deploy.sh` tolerates a failing `composer install` (prints a hint and continues) but never a failing migration.

## Commands
- Deploy: `bash deploy/deploy.sh`
- Install worker (systemd): `sudo cp deploy/whatsapp-worker.service /etc/systemd/system/` → `sudo systemctl daemon-reload` → `sudo systemctl enable --now whatsapp-worker`
- Install worker (supervisor): `sudo cp deploy/supervisor-whatsapp-worker.conf /etc/supervisor/conf.d/whatsapp-worker.conf` → `sudo supervisorctl reread && sudo supervisorctl update`
- Kill a stray manual worker first: `pkill -f "queue:work"`
- Install Reverb service: `sudo cp deploy/whatsapp-reverb.service /etc/systemd/system/` → `sudo systemctl daemon-reload` → `sudo systemctl enable --now whatsapp-reverb`
- Restart services: `systemctl restart whatsapp-worker` / `systemctl restart whatsapp-reverb`
- Restart php-fpm (cpanel): `/usr/local/cpanel/scripts/restartsrv_apache_php_fpm`
- Status: `systemctl is-active whatsapp-worker whatsapp-reverb` / `sudo supervisorctl status whatsapp-worker:*`
- Worker logs: `journalctl -u whatsapp-worker -n 20 --no-pager`
- Verify Reverb is listening: `ss -lntp | grep 12001`
- After changing env: `php artisan optimize:clear`
- CI / automated deploy: غير محدد (deploy is manual over SSH)

## Rules
- **R-8 restart discipline** — the single most important rule here: job/service/provider changes require `systemctl restart whatsapp-worker`; HTTP-path changes require a php-fpm restart (opcache); broadcast/event changes require `systemctl restart whatsapp-reverb`. Skipping any of these makes an already-fixed bug reappear because the old code is still resident.
- The queue worker must always run with `--queue=webhooks,default`. Without it webhook jobs are never processed and no new WhatsApp messages appear.
- The worker must run under systemd **or** supervisor — never `php artisan queue:work &`, which dies with the SSH session. Never run two supervisors or two workers at once.
- Migrations on production run with `--force`; never run an interactive artisan command from the deploy path.
- Never put secrets in these files or in the unit `Environment=` lines; production config lives in the server's `.env`.
- Do not change the app's listening port or Reverb port here without updating the reverse proxy and the mobile client's `REVERB_PORT`.
- Take a backup before deploying (`ops/backup-predeploy.sh`) and verify afterwards (`ops/post-deploy-check.sh`).

## References
- `README.md` (this folder) — the outage this folder prevents, plus both supervision install paths.
- `REVERB_SETUP.md` — required `REVERB_*` env names, service, proxy and client wiring; read before touching real-time.
- `../docs/deployment.md` — production `.env` checklist and queue requirements.
- `../docs/POST_DEPLOY_OBSERVATION_CHECKLIST.md` — what to watch immediately after a deploy.
- `../HTTPS_REVERSE_PROXY_SETUP.md` — the HTTPS proxy in front of the app (trusted proxies, mic access, wss).
- `../ops/AGENTS.md` — host watchdog, backups and read-only diagnostics.
- `../AGENTS.md` — backend stack, commands and conventions.
