# Guide Otomatis Cache Production AHSP Di Ubuntu

Panduan ini membuat proses refresh cache production AHSP lebih otomatis setelah update code.

Script yang disediakan:

```bash
scripts/ubuntu_production_refresh.sh
```

Script ini menjalankan:

- `php artisan optimize:clear`
- `php artisan config:cache`
- `php artisan route:cache`
- `php artisan event:cache`
- `php artisan view:cache`
- `php artisan queue:restart`
- reload Apache/Nginx jika aktif
- restart PHP-FPM yang terdeteksi
- restart Supervisor worker `ahsp-worker-default` dan `ahsp-worker-attendance` jika ada

## 1. Jalankan Manual Saat Dibutuhkan

Di server Ubuntu:

```bash
cd /var/www/html/AHSP
chmod +x scripts/ubuntu_production_refresh.sh
./scripts/ubuntu_production_refresh.sh
```

Jika path project atau PHP berbeda:

```bash
APP_DIR=/var/www/html/AHSP PHP_BIN=/usr/bin/php ./scripts/ubuntu_production_refresh.sh
```

## 2. Jadikan Command Global

Opsional, supaya bisa dipanggil dari mana saja:

```bash
sudo ln -sf /var/www/html/AHSP/scripts/ubuntu_production_refresh.sh /usr/local/bin/ahsp-refresh
sudo chmod +x /var/www/html/AHSP/scripts/ubuntu_production_refresh.sh
```

Lalu jalankan:

```bash
ahsp-refresh
```

## 3. Otomatis Setelah Git Pull

Jika deployment server memakai `git pull`, buat git hook:

```bash
cd /var/www/html/AHSP
nano .git/hooks/post-merge
```

Isi:

```bash
#!/usr/bin/env bash
/var/www/html/AHSP/scripts/ubuntu_production_refresh.sh
```

Aktifkan:

```bash
chmod +x .git/hooks/post-merge
```

Mulai sekarang, setelah `git pull` berhasil merge, script refresh cache akan jalan otomatis.

## 4. Otomatis Via CI/CD

Jika deploy memakai pipeline, panggil command ini di step akhir deployment:

```bash
cd /var/www/html/AHSP
./scripts/ubuntu_production_refresh.sh
```

## 5. Catatan Permission Sudo

Script perlu akses untuk:

- `supervisorctl`
- `systemctl reload apache2/nginx`
- `systemctl restart php*-fpm`

Kalau script dijalankan oleh user non-root, command tersebut akan memakai `sudo`. Pastikan user deploy boleh menjalankan command terkait.

Contoh sudoers terbatas:

```bash
sudo visudo
```

Tambahkan, sesuaikan nama user deploy:

```text
deploy ALL=NOPASSWD: /usr/bin/supervisorctl, /usr/bin/systemctl reload apache2, /usr/bin/systemctl reload nginx, /usr/bin/systemctl restart php*-fpm
```

Jika belum ingin mengubah sudoers, jalankan script manual dengan user yang bisa sudo.

## 6. Verifikasi

Setelah script selesai:

- Buka `Setting > Status Server`.
- Bagian `Nginx/Apache Tuning` harus berubah menjadi OK jika `APP_DEBUG=false`, `config cache`, `route cache`, dan `events cache` sudah aktif.
- Bagian `Queue Worker (Supervisor)` harus OK jika worker `default` dan `attendance-review` berjalan dengan timeout panjang.

Jika `route:cache` gagal, cek error yang muncul. Biasanya masih ada route closure yang perlu dipindahkan ke controller.
