← Back

Infrastructure Fixes — ALLOWED_HOSTS, PM2 & Data Health Monitor

deployment infrastructure monitoring pm2

errors

  • ALLOWED_HOSTS invalid — Django reject semua request HTTP karena ada https:// prefix di mrcuan.codenest.id. Browser dapat 400 Bad Request, seluruh web app tidak bisa diakses.

Actual error from Django log: [2026-06-22 00:29:14,160] ERROR django.security.DisallowedHost Invalid HTTP_HOST header: 'mrcuan.codenest.id'. You may need to add 'mrcuan.codenest.id' to ALLOWED_HOSTS. Traceback (most recent call last): File ".../django/core/handlers/exception.py", line 55, in inner django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'mrcuan.codenest.id'

  • JS syntax error di ecosystem.config.js line 116 — PM2 gagal restart service, error SyntaxError: Unexpected token. Waktu dicek, expression di line 116 invalid.

Actual PM2 error: [PM2] Error while loading ecosystem.config.js SyntaxError: Unexpected token ...

  • Tidak ada auto-restart — Kalau server kena reboot (maintenance/down), semua 6 PM2 service mati total. Tidak ada pm2 startup yang di-configure, harus start manual satu-satu.
  • Tidak ada data health monitoring — Tidak ada sistem yang ngecek apakah data OHLC masih fresh atau stale. Kalau fetcher gagal, baru ketahuan besok pas laporan.

context

Server production di codenest.id, semua service jalan via PM2 (sniper-api, sniper-bot, sniper-fetcher, radar-analysis, radar-analysis-quick, web-dashboard). Setelah beberapa kali deploy dan config changes, infrastruktur mulai rapuh. ALLOWED_HOSTS di settings.py pakai env variable yang isinya salah dari awal. PM2 config pernah di-edit manual dan不小心 introduce syntax error. Belum ada setup startup hook untuk systemd.

problem

Infrastruktur punya 4 celah kritis: (1) konfigurasi ALLOWED_HOSTS salah format bikin web down, (2) file ecosystem.config.js corrupt syntax bikin PM2 management gagal, (3) server reboot = semua service mati permanen sampai admin login manual, (4) nggak ada visibility ke health data pipeline — kalau data stale berhari-hari nggak ketahuan.

investigation

  • ALLOWED_HOSTS: dicek di settings.py, ternyata value dari .env adalah https://mrcuan.codenest.id — Django nggak expect protocol prefix. Compare dengan dokumen Django ALLOWED_HOSTS.

Confirmed via curl: bash $ curl -H "Host: mrcuan.codenest.id" http://localhost:9002/ # Response: 400 Bad Request — DisallowedHost ✅ (confirm error)

  • PM2 syntax: jalankan pm2 restart all dapat error. Buka ecosystem.config.js, inspect line 116 — ternyata ada expression JS yang nggak valid, kemungkinan hasil copy-paste atau edit manual yang nggak sengaja.

Validated via Node.js parser: bash $ node -c ecosystem.config.js # Output: SyntaxError: Unexpected token '...' on line 116

  • Auto-restart: cek pm2 startup status — belum pernah di-run. Cek pm2 save — belum ada dump process list.
  • Health monitoring: tidak ada script atau cron yang ngecek integritas data. Cuma andal pada log error manual.

root-cause

  1. ALLOWED_HOSTS — .env file ditulis dengan ALLOWED_HOSTS=https://mrcuan.codenest.id (termasuk protocol). Seharusnya mrcuan.codenest.id aja. Django secara eksplisit nggak accept URL dengan scheme.

python # ALLOWED_HOSTS di settings.py isinya 'https://mrcuan.codenest.id' # Django mengharapkan hostname saja tanpa protokol. # 'https://' prefix bikin string match gagal.

  1. PM2 syntax — Line 116 di ecosystem.config.js ada expression ALLOWED_HOSTS: process.env.ALLOWED_HOSTS || 'mrcuan.codenest.id' yang ditulis tanpa koma atau dengan karakter invalid. Kemungkinan waktu nambah env variable ke config.

javascript // JS syntax error: expression '["mrcuan.codenest.id"]' ditulis sebagai string literal, // bukan array literal. Node.js gagal parse ecosystem.config.js.

  1. No startup — pm2 startup belum pernah dijalankan, jadi systemd unit nggak ada. pm2 save juga belum, jadi process list nggak di-dump.
  2. No monitoring — Belum ada yang mikir untuk bikin data health check. Anggapannya fetcher selalu jalan bener.

solution

  1. ALLOWED_HOSTS: hapus https:// prefix dari value di settings.py. Pindahkan domain ke .env dengan format bener: mrcuan.codenest.id. Django langsung accept request setelah fix.

python # settings.py — fix ALLOWED_HOSTS ALLOWED_HOSTS = [ 'localhost', '127.0.0.1', 'mrcuan.codenest.id', # sebelumnya: 'https://mrcuan.codenest.id' ❌ ]

  1. PM2 syntax error: buka ecosystem.config.js line 116, hapus karakter penyebab syntax error. Validasi dengan node -c ecosystem.config.js.

javascript // ecosystem.config.js — PM2 config seharusnya plain JS object, bukan expression // ❌ Sebelum (salah): // ALLOWED_HOSTS: "['mrcuan.codenest.id']" // string, bukan array // ✅ Sesudah (benar): // ALLOWED_HOSTS: ['mrcuan.codenest.id']

  1. Auto-restart: jalankan pm2 startup — ini bikin systemd unit. Lanjut pm2 save — dump semua process list. Testing: reboot server, service nyala otomatis.
  2. Data health monitor: bikin data_health_monitor.py — script python yang ngecek row count + max date per timeframe (1d, 1h, 5m). Deteksi stale: cek apakah fetcher udah jalan (via pgrep). Kalau belum: start incremental fetch. Schedule via PM2 cron tiap jam.

insight

  • Konfigurasi infrastruktur yang salah bisa bikin seluruh system down — ALLOWED_HOSTS prefix typo adalah contoh klasik. Selalu validasi config sebelum deploy.
  • PM2 itu powerful tapi butuh setup yang benar: pm2 startup + pm2 save adalah dua hal yang WAJIB dilakukan setelah setup awal. Tanpa ini, server reboot = service mati.
  • Monitoring itu bukan fitur opsional — data health monitor harusnya dibuat dari hari pertama. Lebih baik preventif daripada reaktif.
  • Satu line syntax error di file config bisa bikin semua service restart gagal. Selalu node -c atau jq validasi file config.

gotchas

  • ALLOWED_HOSTS di Django TIDAK boleh pake protocol (http:// / https://), port, atau trailing slash. Cukup domain name aja. Kalau perlu multiple hosts, pisah dengan koma (string) atau list (setting Python).
  • pm2 startup butuh sudo untuk bikin systemd unit. Outputnya bakal ngasih command yang harus di-copy-paste dan di-run dengan sudo — jangan skip这一步.
  • pm2 save HARUS dijalankan SETELAH semua service udah running dengan config yang bener. Kalau dijalanin pas ada service error, nanti auto-restart juga error.
  • pgrep bisa false positive kalau ada process lain dengan nama mirip. Kombinasikan dengan pm2 pid untuk deteksi yang lebih akurat.
  • Data health monitor threshold harus realistis: 100% coverage untuk 1d itu hampir impossible karena ada saham tidur yang emang jarang trading.

Last updated 31 Jul 2026 00:15

Comments