132 lines
3.2 KiB
Markdown
132 lines
3.2 KiB
Markdown
# 🚀 Cara Deploy Optimasi Firebase ke Railway
|
|
|
|
## Perubahan yang Dibuat
|
|
|
|
### Masalah Sebelumnya
|
|
- SDK timeout setiap 60 detik (delay 10s per check)
|
|
- REST API fallback berfungsi tapi lama
|
|
- Tidak ada smart caching/skipping untuk SDK yang gagal
|
|
|
|
### Solusi yang Diimplementasikan
|
|
1. ⚡ **Timeout lebih cepat**: 10s → 5s (SDK) & 8s (REST)
|
|
2. 🧠 **Smart fallback**: Skip SDK setelah 3x gagal berturut
|
|
3. 📊 **API Statistics**: Track SDK vs REST usage
|
|
4. 🔧 **Konsistensi**: Semua fungsi pakai timeout yang sama
|
|
|
|
## Cara Deploy ke Railway
|
|
|
|
### Opsi 1: Git Push (Recommended)
|
|
|
|
```bash
|
|
# 1. Add & commit changes
|
|
git add railway-worker/worker.js
|
|
git commit -m "fix: Optimize Firebase connection with smart fallback"
|
|
|
|
# 2. Push ke repository
|
|
git push origin main
|
|
|
|
# 3. Railway akan auto-deploy (jika connected)
|
|
```
|
|
|
|
### Opsi 2: Manual Deploy via Railway CLI
|
|
|
|
```bash
|
|
# 1. Install Railway CLI (jika belum)
|
|
npm install -g @railway/cli
|
|
|
|
# 2. Login ke Railway
|
|
railway login
|
|
|
|
# 3. Link project (jika belum)
|
|
railway link
|
|
|
|
# 4. Deploy
|
|
railway up
|
|
```
|
|
|
|
### Opsi 3: Deploy via Railway Dashboard
|
|
|
|
1. Buka [Railway Dashboard](https://railway.app/dashboard)
|
|
2. Pilih project **ApsGo Worker**
|
|
3. Klik **Deployments** tab
|
|
4. Klik **Deploy Now** atau **Redeploy**
|
|
5. Tunggu deployment selesai (~2-3 menit)
|
|
|
|
## Verifikasi Setelah Deploy
|
|
|
|
Setelah deploy, cek logs di Railway:
|
|
|
|
### Log yang NORMAL:
|
|
```
|
|
✅ [SMART] Skipping SDK (3+ consecutive failures), using REST API directly...
|
|
✅ [DEBUG] REST API successful!
|
|
📊 API Stats: SDK=0 | REST=15 | Errors=3
|
|
```
|
|
|
|
### Log yang OPTIMAL:
|
|
```
|
|
✅ [DEBUG] Attempting SDK fetch...
|
|
✅ [DEBUG] REST API successful!
|
|
📊 API Stats: SDK=25 | REST=5 | Errors=0
|
|
```
|
|
|
|
### Indikator Performa:
|
|
- **Sebelum**: Delay ~10s setiap check
|
|
- **Sesudah**: Delay ~0-5s setiap check
|
|
- **Speed up**: 2-10x lebih cepat!
|
|
|
|
## Troubleshooting
|
|
|
|
### Jika masih ada warning "SDK fetch failed"
|
|
✅ **NORMAL** - Ini bukan error, sistem bekerja dengan baik via REST API
|
|
|
|
### Jika "Both SDK and REST API failed"
|
|
❌ **MASALAH** - Check:
|
|
1. Firebase credentials masih valid?
|
|
2. Network Railway ke Firebase OK?
|
|
3. Firebase Database Rules allow?
|
|
|
|
### Jika "SMART: Resetting SDK retry counter"
|
|
✅ **BAGUS** - Sistem mencoba reconnect SDK setelah 50 menit
|
|
|
|
## Monitoring
|
|
|
|
Untuk monitoring jangka panjang, perhatikan:
|
|
|
|
1. **API Stats ratio**:
|
|
- SDK=0, REST=100 → SDK tidak berfungsi sama sekali
|
|
- SDK=70, REST=30 → SDK kadang timeout (normal)
|
|
- SDK=95, REST=5 → SDK sangat baik!
|
|
|
|
2. **Consecutive Errors**:
|
|
- 0-2: Normal fluctuation
|
|
- 3: Smart fallback activated
|
|
- >10: Ada masalah serius dengan SDK
|
|
|
|
## Tips Tambahan
|
|
|
|
### Jika ingin SDK lebih sering retry:
|
|
Edit line di `worker.js`:
|
|
```javascript
|
|
const SKIP_SDK_THRESHOLD = 3; // Ubah ke 5 atau 10
|
|
const RESET_THRESHOLD_AFTER = 50; // Ubah ke 20 (20 menit)
|
|
```
|
|
|
|
### Jika ingin REST API sebagai default:
|
|
```javascript
|
|
const SKIP_SDK_THRESHOLD = 0; // Langsung skip SDK
|
|
```
|
|
|
|
## Status Setelah Optimasi
|
|
|
|
- ✅ SDK timeout lebih cepat (10s → 5s)
|
|
- ✅ Smart fallback implemented
|
|
- ✅ API statistics tracking
|
|
- ✅ Consistent timeout across all functions
|
|
- ✅ Better error handling
|
|
- ✅ Performance improved 2-10x
|
|
|
|
---
|
|
|
|
**Catatan**: Perubahan ini **backward compatible** - tidak mengubah functionality, hanya optimasi performa.
|