# Debug: Kenapa Hanya 1 Masuk ke Shared Hosting Padahal di Golang Ada Lebih dari 5

## Masalah

- ✅ Di Golang (`webhook_queue`): Ada lebih dari 5 entries
- ❌ Di Shared Hosting (database MySQL): Hanya 1 yang masuk

Ini berarti ada masalah di proses **forward dari Golang ke Shared Hosting**.

---

## Langkah Debugging

### 1. Cek Status Queue di SQLite

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT 
    id,
    type,
    status,
    attempts,
    error,
    datetime(created_at) as created,
    datetime(updated_at) as updated
FROM webhook_queue 
WHERE date(created_at) = date('now')
ORDER BY id DESC
LIMIT 20;
"
```

**Yang dicari:**
- Berapa yang **completed** (berhasil forward)?
- Berapa yang **failed** (gagal forward)?
- Berapa yang **pending** (belum diproses)?
- Apa **error message** untuk yang failed?

---

### 2. Cek Log Golang untuk Error Forward

```bash
sudo journalctl -u webhook-service --since "today" | grep -iE "error|failed|retry|shared hosting|status"
```

**Yang dicari:**
- `⚠️ [Queue X] Retry` → Forward gagal, sedang retry
- `💀 [Queue X] Failed` → Sudah retry maksimal, tetap gagal
- `shared hosting status 500` atau `timeout` → Shared hosting error

---

### 3. Cek Log Golang untuk Success Forward

```bash
sudo journalctl -u webhook-service --since "today" | grep "Relay Success"
```

**Yang dicari:**
- Berapa banyak log `✅ [Queue X] Relay Success`?
- Apakah jumlahnya sama dengan yang **completed** di SQLite?

**Jika jumlah berbeda:** Ada masalah di update status atau log.

---

### 4. Test Manual Forward ke Shared Hosting

**Dari VPS, test apakah shared hosting bisa diakses:**

```bash
curl -X POST https://po.persadalab.com/webhook/receiver.php \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Secret: 8f7b2c5e4d1a9b0c3f5e9d8a7b6c5a4b3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a" \
  -H "X-Webhook-Type: SALES_RECEIPT" \
  -H "X-Webhook-Source: VPS-GOLANG-RELAY" \
  -d '{
    "payload": {
      "type": "SALES_RECEIPT",
      "databaseId": 1293289,
      "uuid": "test-manual-001",
      "data": [{"salesReceiptNo": "TEST-001"}]
    },
    "timestamp": "2026-02-02T10:00:00Z"
  }'
```

**Expected response:** `{"status":"success","message":"Processed"}`

**Jika error/timeout:** Masalah di shared hosting (receiver.php error, network, dll).

---

### 5. Cek Log Shared Hosting (receiver.php)

**Di shared hosting (cPanel error log atau PHP error log):**

Cari log dengan keyword: `[Webhook]`, `[Receipt]`, `[Invoice]`, `CRITICAL ERROR`

**Yang dicari:**
- Apakah webhook sampai ke receiver.php?
- Apakah ada error saat proses (DB error, API error, dll)?
- Apakah semua di-skip karena DFT?

---

### 6. Cek Konfigurasi Shared Hosting URL

**Di VPS, cek `.env`:**

```bash
cat /opt/webhook-service/.env | grep SHARED_HOSTING
```

**Pastikan:**
- `SHARED_HOSTING_URL` benar dan bisa diakses
- `SHARED_HOSTING_SECRET` sesuai dengan yang di receiver.php

---

## Kemungkinan Penyebab

### A. Forward Gagal (Error di Golang)

**Gejala:** Banyak queue entries dengan status **failed** atau **pending**

**Penyebab:**
- Shared hosting URL salah atau tidak bisa diakses
- Network timeout (shared hosting lambat)
- Receiver.php return error (500, 400, dll)
- SSL certificate issue

**Solusi:**
- Test manual dengan `curl` (langkah 4)
- Cek log error di Golang (langkah 2)
- Cek log error di shared hosting (langkah 5)

---

### B. Receiver.php Error atau Skip

**Gejala:** Forward berhasil (status completed), tapi tidak masuk database

**Penyebab:**
- Receiver.php error saat proses (DB error, API error)
- Semua payload di-skip karena DFT (draft)
- Idempotency check skip (sudah pernah diproses)

**Solusi:**
- Cek log error di shared hosting
- Cek apakah payload yang dikirim adalah DFT
- Cek apakah idempotency key sudah ada

---

### C. Retry Mechanism Gagal

**Gejala:** Queue entries dengan `attempts > 0` tapi tetap failed

**Penyebab:**
- Retry sudah maksimal (default 3 atau 5)
- Error persistent (shared hosting selalu error)
- Timeout selalu terjadi

**Solusi:**
- Cek error message di kolom `error` di SQLite
- Fix masalah di shared hosting
- Manual retry dengan update status ke `pending`

---

### D. Worker Pool Penuh atau Lambat

**Gejala:** Banyak queue entries dengan status **pending**

**Penyebab:**
- Worker pool penuh (semua worker sibuk)
- Forward ke shared hosting lambat (timeout 30s)
- Worker tidak cukup (default 5)

**Solusi:**
- Naikkan `WORKER_COUNT` di `.env`
- Cek apakah shared hosting lambat
- Monitor worker pool dengan log

---

## Query SQLite untuk Analisis Lengkap

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT 
    status,
    COUNT(*) as count,
    AVG(attempts) as avg_attempts,
    GROUP_CONCAT(DISTINCT substr(error, 1, 50)) as sample_errors
FROM webhook_queue 
WHERE date(created_at) = date('now')
GROUP BY status;
"
```

**Ini akan menunjukkan:**
- Berapa yang completed vs failed vs pending
- Rata-rata attempts (berapa kali retry)
- Sample error messages

---

## Langkah Perbaikan

**Setelah tahu penyebabnya:**

1. **Jika forward gagal:** Fix shared hosting URL, network, atau receiver.php
2. **Jika receiver.php error:** Fix error di receiver.php (DB, API, dll)
3. **Jika semua DFT:** Ini normal, receiver.php memang skip DFT
4. **Jika retry gagal:** Fix masalah root cause, lalu manual retry failed entries

**Manual retry failed entries:**

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue 
SET status = 'pending', attempts = 0, error = NULL 
WHERE status = 'failed' 
    AND date(created_at) = date('now');
"
```

Lalu restart service atau tunggu worker pickup pending jobs.

---

**Mulai dari langkah 1 dan 2** - itu yang paling penting untuk tahu penyebabnya!
