Mitra ping: decouple stale-after from app cadence

Splits the single mitra_ping_interval_seconds config (which conflated
"how often the app pings" with "how long until offline" through a
hidden ×3 multiplier) into two orthogonal knobs:

- mitra_stale_after_seconds (CC-tunable, app_config DB row): the
  operator-facing offline threshold. What you set is what you get —
  no multiplier. Default 45s (preserves today's effective grace at
  the legacy 15s ping default).
- MITRA_HEARTBEAT_CADENCE_SECONDS (env var, default 30s): how often
  the mitra app sends a heartbeat. Backend-fixed per deployment;
  surfaced to the mitra app via /api/mitra/status.

Backend:
- config.service: getMitraPingConfig returns the new tuple
  {require_ping, stale_after_seconds, heartbeat_cadence_seconds}.
  Env parser handles blank/non-numeric → 30 fallback.
- mitra-status.service::autoOfflineStaleMitras drops the *3 and uses
  stale_after_seconds directly.
- mitra-status.service::getStatus returns heartbeat_cadence_seconds
  instead of ping_interval_seconds.
- /internal/config/mitra-ping PATCH validates
  stale_after_seconds >= cadence, returns 422 with a clear message
  ("stale_after_seconds must be a number >= heartbeat cadence (30s)").
- migrate.js: adds mitra_stale_after_seconds default 45. The old
  mitra_ping_interval_seconds key is left in place (vestigial) —
  no live code reads it; safe to drop after one release.

Mitra app:
- status_notifier reads heartbeat_cadence_seconds, uses it directly
  as the Timer.periodic interval. Defaults to 30s if missing (older
  backend safety).

Control center:
- SettingsPage: renames "Interval Ping" → "Ambang offline", input
  min={heartbeat_cadence_seconds}, shows the cadence as a read-only
  value with explanation that it's env-controlled.

Verified end-to-end on dev backend:
- GET /api/mitra/status returns {…, heartbeat_cadence_seconds: 30}
- GET /internal/config/mitra-ping returns {require_ping,
  stale_after_seconds: 45, heartbeat_cadence_seconds: 30}
- PATCH with stale_after_seconds=20 → 422 with cadence message
- PATCH with stale_after_seconds=120 → 200, persisted
- Env override (=60, blank, "foo") parses correctly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-21 20:39:59 +08:00
parent 1653482d54
commit a8c20d929e
6 changed files with 80 additions and 22 deletions

View File

@@ -452,7 +452,7 @@ export default function SettingsPage() {
<section style={{ marginBottom: 24 }}>
<h2>Mitra Online Status (Ping)</h2>
<p>Konfigurasi apakah mitra harus mengirim ping (heartbeat) untuk tetap online.</p>
<p>Mitra dianggap online selama heartbeat terakhir berusia ambang batas. Cadence (frekuensi ping aplikasi) di-fix oleh server lewat env var.</p>
<label style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
<input
type="checkbox"
@@ -465,21 +465,27 @@ export default function SettingsPage() {
<p style={{ fontSize: 12, color: '#666', marginBottom: 8 }}>
Jika dinonaktifkan, mitra akan tetap online tanpa perlu mengirim ping. QC bertanggung jawab atas kualitas layanan mitra.
</p>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<label>Interval Ping:</label>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 6 }}>
<label>Ambang offline (heartbeat terakhir lebih lama dari):</label>
<input
type="number"
min="5"
value={mpData?.ping_interval_seconds ?? 15}
min={mpData?.heartbeat_cadence_seconds ?? 30}
value={mpData?.stale_after_seconds ?? 45}
onChange={e => {
const val = parseInt(e.target.value, 10)
if (val >= 5) mpMutation.mutate({ ping_interval_seconds: val })
const floor = mpData?.heartbeat_cadence_seconds ?? 30
if (Number.isFinite(val) && val >= floor) {
mpMutation.mutate({ stale_after_seconds: val })
}
}}
disabled={mpMutation.isPending}
style={{ width: 80 }}
/>
<span>detik</span>
</div>
<p style={{ fontSize: 12, color: '#666', marginBottom: 0 }}>
Cadence ping mitra: <strong>{mpData?.heartbeat_cadence_seconds ?? 30} detik</strong> (server-set via MITRA_HEARTBEAT_CADENCE_SECONDS env). Nilai ambang minimum mengikuti cadence tidak bisa lebih rendah.
</p>
{mpMutation.isError && <p style={{ color: 'red' }}>Gagal menyimpan.</p>}
</section>