> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leviacloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Operasi asinkron

> Resource berpindah status saat disiapkan atau diubah. Pantau dengan GET sampai status akhir.

Membuat VM, database, atau load balancer, dan aksi seperti start, stop, resize, snapshot, atau restore butuh waktu dari beberapa detik sampai beberapa menit. API tidak menunggu sampai selesai:

1. Request diterima dan dijawab `201` (resource baru) atau `202` (aksi diterima), dengan `status` yang menunjukkan proses masih berjalan.
2. Resource berpindah status di belakang.
3. Anda membaca resource dengan `GET` sampai status akhirnya tercapai.

## Status per resource

| Resource | Saat berjalan | Status akhir | Endpoint untuk dipantau |
| - | - | - | - |
| VM baru | `provisioning` | `running` | `GET /vms/{id}` |
| Start / stop / restart / resize VM | `starting`, `stopping`, `rebooting`, `resizing`; `pending_action` terisi | `running` atau `stopped`, `pending_action` kosong | `GET /vms/{id}` |
| Volume baru / restore snapshot | `creating`, `restoring` | `available` | `GET /volumes/{id}` |
| Attach / detach volume | `attaching`, `detaching` | `in-use` / `available` | `GET /volumes/{id}` |
| Snapshot VM atau volume | `provisioning` | `active` | `GET /snapshots/{id}` |
| Database baru / restore ke database baru | `provisioning` | `running` | `GET /databases/{id}` |
| Start / stop / restart database | `starting`, `stopping` | `running` / `stopped` | `GET /databases/{id}` |
| User / database logis | `creating` | `active` | `GET /databases/{id}/users`, `…/logical-databases` |
| Backup database manual | `creating` | `ready` | `GET /databases/{id}/backups` |
| Load balancer baru | `provisioning` | `active` dan `ready: true` | `GET /load-balancers/{id}` |
| Target load balancer | `health_status: unknown` | `healthy` | `GET /load-balancers/{id}/targets` |
| Hapus (semua resource) | `deleting` | hilang: detail menjawab `404` | detail resource |

Status `error` berarti proses gagal. Penjelasannya ada di `status_message` (VM, database, load balancer, snapshot) atau `last_error` (volume).

## Cara polling

* Polling setiap **5–10 detik**. Pembuatan VM dan load balancer biasanya selesai dalam hitungan menit atau kurang; database bisa beberapa menit.
* Pasang **batas waktu** (mis. 15 menit untuk database) dan berhenti bila status menjadi `error`.
* Untuk penghapusan, anggap `404` sebagai selesai.
* Aksi berikutnya pada resource yang sama sebaiknya menunggu aksi sebelumnya selesai. Selama task masih berjalan, platform bisa menjawab `409`; ulangi beberapa detik kemudian.

```bash Bash theme={null}
wait_for() {   # wait_for <path> <jq-filter> <nilai>
  for i in $(seq 1 90); do
    v=$(curl -s "$LEVIA_API$1" -H "Authorization: Bearer $LEVIA_KEY" | jq -r "$2")
    [ "$v" = "$3" ] && return 0
    [ "$v" = "error" ] && { echo "gagal"; return 1; }
    sleep 10
  done
  echo "timeout"; return 1
}

wait_for "/vms/$VM" '.data.status' running
```

```python Python theme={null}
import time, requests

API = "https://api.leviacloud.com/v1"
H = {"Authorization": "Bearer sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

def wait_for(path, done, timeout=900, every=10):
    deadline = time.time() + timeout
    while time.time() < deadline:
        r = requests.get(API + path, headers=H)
        if r.status_code == 404:
            return None                      # sudah terhapus
        r.raise_for_status()
        data = r.json()["data"]
        if data.get("status") == "error":
            raise RuntimeError(data.get("status_message", "gagal"))
        if done(data):
            return data
        time.sleep(every)
    raise TimeoutError(path)

vm = wait_for(f"/vms/{vm_id}", lambda d: d["status"] == "running" and not d.get("pending_action"))
```

## Biaya selama proses

Tagihan mengikuti aturan yang sama dengan dashboard. Misalnya, database yang dimatikan tetap ditagih untuk disknya, load balancer ditagih sejak `active`, dan database atau load balancer berstatus `deleting` tidak ditagih lagi. Rincian per resource: `GET /billing/usage/monthly`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.