# Checklist Deploy ke VPS

## Perubahan yang Sudah Dilakukan

### ✅ 1. Batch Webhook Support
- Handler loop semua payload dalam array (bukan hanya payload pertama)
- Setiap payload disimpan ke queue terpisah
- Logging: "Batch detected: X payload(s)"

### ✅ 2. Idempotency Check
- Check UUID sebelum INSERT ke queue
- Skip jika UUID sudah ada (duplicate)
- Unique index di SQLite untuk prevent duplicate

### ✅ 3. Logging Lebih Detail
- Log saat mulai forward ke shared hosting
- Log response dari shared hosting (status, duration)
- Log error dengan detail lengkap

### ✅ 4. DFT Filter di Golang
- Skip forward ke shared hosting jika payload adalah DFT
- Status: `completed` dengan error `skipped_draft`
- Menghemat bandwidth dan mengurangi load shared hosting

---

## Langkah Deploy

### 1. Build Binary untuk Linux

**Di Windows (PowerShell):**

```powershell
cd c:\Users\MSahrul\Documents\PROJECT\lims\golang-webhook-service

# Set environment untuk Linux
$env:GOOS = "linux"
$env:GOARCH = "amd64"

# Build
go build -o webhook-service .

# Verify binary
ls webhook-service
```

**Expected:** File `webhook-service` (tanpa .exe) untuk Linux.

---

### 2. Upload ke VPS

**Via SCP:**

```powershell
scp webhook-service root@202.155.95.172:/tmp/
```

**Atau via FileZilla/WinSCP:**
- Upload `webhook-service` ke `/tmp/` di VPS

---

### 3. Di VPS - Backup Binary Lama (Opsional tapi Disarankan)

```bash
# Backup binary lama
sudo cp /opt/webhook-service/webhook-service /opt/webhook-service/webhook-service.backup-$(date +%Y%m%d-%H%M%S)
```

---

### 4. Di VPS - Ganti Binary dan Restart Service

```bash
# Stop service
sudo systemctl stop webhook-service

# Ganti binary
sudo cp /tmp/webhook-service /opt/webhook-service/
sudo chmod +x /opt/webhook-service/webhook-service
sudo chown www-data:www-data /opt/webhook-service/webhook-service

# Start service
sudo systemctl start webhook-service

# Cek status
sudo systemctl status webhook-service
```

**Expected:** Status `active (running)` ✅

---

### 5. Verifikasi Deploy

**A. Cek Log Service:**

```bash
sudo journalctl -u webhook-service -n 50 --no-pager
```

**Yang dicari:**
- ✅ Service start tanpa error
- ✅ SQLite connected
- ✅ Worker started
- ✅ Server listening on port

**B. Test Health Endpoint:**

```bash
curl http://localhost:9000/health
```

**Expected:** `OK`

**C. Monitor Log Real-time:**

```bash
sudo journalctl -u webhook-service -f
```

Tunggu webhook dari Accurate, harusnya muncul:
- `📥 [Webhook] Incoming POST ...`
- `📦 [Webhook] Batch detected: X payload(s)` (jika batch)
- `⏭️ [Queue X] Skipping draft` (jika DFT)
- `🔄 [Queue X] Forwarding ...` (jika bukan DFT)
- `✅ [Queue X] Relay Success` (jika berhasil)

---

### 6. Verifikasi Fitur Baru

**A. Cek Batch Webhook:**

```bash
# Tunggu webhook dari Accurate, lalu cek log
sudo journalctl -u webhook-service --since "5 minutes ago" | grep "Batch detected"
```

**Expected:** Log "Batch detected: 2 payload(s)" jika Accurate kirim batch.

**B. Cek DFT Filter:**

```bash
# Cek log skip DFT
sudo journalctl -u webhook-service --since "5 minutes ago" | grep "Skipping draft"
```

**Expected:** Banyak log skip DFT jika ada banyak draft.

**C. Cek Queue di SQLite:**

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT 
    CASE 
        WHEN error = 'skipped_draft' THEN 'DFT (Skipped)'
        WHEN error IS NULL THEN 'Forwarded'
        ELSE 'Error'
    END AS category,
    COUNT(*) as count
FROM webhook_queue 
WHERE date(created_at) = date('now')
GROUP BY category;
"
```

**Expected:** 
- Banyak `DFT (Skipped)` → Filter bekerja ✅
- Beberapa `Forwarded` → Yang valid di-forward ✅

**D. Cek Idempotency:**

```bash
# Cek apakah ada duplicate UUID
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT uuid, COUNT(*) as count
FROM webhook_queue 
WHERE uuid IS NOT NULL
GROUP BY uuid
HAVING count > 1;
"
```

**Expected:** Tidak ada hasil (semua UUID unique) ✅

---

## Rollback (Jika Ada Masalah)

**Jika service error atau tidak jalan:**

```bash
# Stop service
sudo systemctl stop webhook-service

# Restore backup binary
sudo cp /opt/webhook-service/webhook-service.backup-* /opt/webhook-service/webhook-service
sudo chmod +x /opt/webhook-service/webhook-service
sudo chown www-data:www-data /opt/webhook-service/webhook-service

# Start service
sudo systemctl start webhook-service
sudo systemctl status webhook-service
```

---

## Troubleshooting

### Problem: Service tidak start

**Cek log:**
```bash
sudo journalctl -u webhook-service -n 100
```

**Kemungkinan:**
- Binary tidak executable → `chmod +x webhook-service`
- Permission issue → `chown www-data:www-data webhook-service`
- Port sudah dipakai → Cek dengan `netstat -tulpn | grep 9000`

### Problem: Binary tidak jalan

**Test manual:**
```bash
cd /opt/webhook-service
./webhook-service
```

**Cek error message.**

### Problem: Database error

**Cek:**
```bash
ls -la /opt/webhook-service/data/
```

**Pastikan:**
- Folder `data/` ada dan writable
- File `webhook.db` bisa diakses

---

## Checklist Final

- [ ] Binary sudah di-build untuk Linux
- [ ] Binary sudah di-upload ke VPS
- [ ] Binary lama sudah di-backup
- [ ] Binary baru sudah di-copy ke `/opt/webhook-service/`
- [ ] Permission sudah benar (`chmod +x`, `chown`)
- [ ] Service sudah di-restart
- [ ] Service status `active (running)`
- [ ] Health endpoint return `OK`
- [ ] Log menunjukkan service jalan normal
- [ ] Test dengan webhook dari Accurate
- [ ] Verifikasi batch webhook bekerja
- [ ] Verifikasi DFT filter bekerja
- [ ] Verifikasi idempotency bekerja

---

## Setelah Deploy

**Monitor selama beberapa jam:**
- Cek log untuk error
- Cek queue di SQLite
- Cek apakah webhook masuk ke database MySQL

**Jika semua OK:** Deploy berhasil! ✅

---

**Siap untuk deploy!** 🚀
