# Retry Webhook yang Gagal (Failed / Stuck Processing)

## Status "Processing" tapi Ada Error (Stuck)

Kalau di queue ada job dengan **status = processing** tapi kolom **error** terisi (misalnya `HTTP request error: Post "": unsupported protocol scheme`), artinya job gagal saat forward tapi status tidak pernah di-reset. Ini sudah diperbaiki di kode: saat gagal dan masih ada jatah retry, status akan di-set ke **pending** (bukan tetap processing).

**Untuk job yang sudah terlanjur stuck (sebelum patch):** reset ke pending lalu restart:

```bash
# Reset processing (yang error) jadi pending agar di-retry saat startup
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue
SET status = 'pending', updated_at = CURRENT_TIMESTAMP
WHERE status = 'processing' AND error IS NOT NULL AND error != '';
"

sudo systemctl restart webhook-service
```

Setelah deploy binary yang baru, job yang gagal forward akan otomatis status-nya jadi **pending** (bukan tetap processing).

---

## Kenapa "Push lagi dari Accurate" Terdeteksi Duplikat?

Ketika Anda **resend** dari Accurate, payload punya **UUID yang sama**. Di Golang kita pakai UUID untuk idempotency:

- UUID sudah ada di `webhook_queue` → kita **tidak insert baru**, return existing queue ID
- Job yang ada itu statusnya **failed** (gagal forward tadi), jadi tidak diproses lagi

Jadi yang perlu dilakukan: **retry job yang sudah ada di queue** (reset status + restart), bukan menunggu kirim ulang dari Accurate.

---

## Cara 1: Reset Failed → Pending, Lalu Restart (Disarankan)

**Di VPS, jalankan:**

```bash
# 1. Lihat dulu berapa yang failed
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT status, COUNT(*) as count
FROM webhook_queue
WHERE status = 'failed'
GROUP BY status;
"

# 2. Reset failed jadi pending (dan reset attempts agar dapat full retry lagi)
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue
SET status = 'pending', attempts = 0, error = NULL
WHERE status = 'failed';
"

# 3. Cek berapa baris yang di-update
sqlite3 /opt/webhook-service/data/webhook.db "
SELECT changes();
"

# 4. Restart service agar startup pickup pending jobs
sudo systemctl restart webhook-service

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

**Setelah restart:** Service akan jalankan "Pickup old pending/failed jobs" dan mengirim ulang job yang tadi failed ke shared hosting.

---

## Cara 2: Hanya Retry yang Error Tertentu (Opsional)

Kalau mau hanya retry yang error **"unsupported protocol scheme"** (bukan yang error lain):

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue
SET status = 'pending', attempts = 0, error = NULL
WHERE status = 'failed'
  AND error LIKE '%unsupported protocol scheme%';
"

sudo systemctl restart webhook-service
```

---

## Cara 3: Retry Manual per ID (Opsional)

Kalau mau retry satu per satu (misalnya cuma ID tertentu):

```bash
# Ganti 123, 124 dengan id yang mau di-retry
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue
SET status = 'pending', attempts = 0, error = NULL
WHERE id IN (123, 124);
"

sudo systemctl restart webhook-service
```

---

## Verifikasi Setelah Retry

**1. Cek log:**

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

Harusnya muncul:
- `📥 [Startup] Picked up old pending/failed jobs`
- `🔄 [Queue X] Forwarding SALES_INVOICE to shared hosting...` (atau SALES_RECEIPT)
- `✅ [Forward] Shared hosting responded OK`
- `✅ [Queue X] Relay Success`

**2. Cek tabel lagi:**

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

Yang tadi failed seharusnya jadi **completed** (dan error kosong).

**3. Cek database MySQL (shared hosting):**

Pastikan data yang tadi gagal sekarang masuk ke `t_log_accurate` / `t_po_pembayaran`.

---

## Ringkasan

| Yang Anda alami | Penjelasan |
|-----------------|------------|
| Push lagi dari Accurate → duplikat | UUID sama, kita tidak buat queue baru, pakai yang lama (yang status failed). |
| Solusi | Reset job yang **failed** jadi **pending**, lalu **restart** service. |
| Setelah restart | Startup baca semua pending, worker proses lagi → forward ke shared hosting. |

**Perintah utama (copy-paste di VPS):**

```bash
sqlite3 /opt/webhook-service/data/webhook.db "
UPDATE webhook_queue
SET status = 'pending', attempts = 0, error = NULL
WHERE status = 'failed';
"
sudo systemctl restart webhook-service
```

Setelah itu, job yang tadi error akan dikirim ulang ke shared hosting.
