* Fetch (in-memory memoized) and persist to disk on change. * Errors are not persisted — a transient failure should not overwrite a * known-good disk value.
()
| 92 | * known-good disk value. |
| 93 | */ |
| 94 | async function refreshMetricsStatus(): Promise<MetricsStatus> { |
| 95 | const result = await memoizedCheckMetrics() |
| 96 | if (result.hasError) { |
| 97 | return result |
| 98 | } |
| 99 | |
| 100 | const cached = getGlobalConfig().metricsStatusCache |
| 101 | const unchanged = cached !== undefined && cached.enabled === result.enabled |
| 102 | // Skip write when unchanged AND timestamp still fresh — avoids config churn |
| 103 | // when concurrent callers race past a stale disk entry and all try to write. |
| 104 | if (unchanged && Date.now() - cached.timestamp < DISK_CACHE_TTL_MS) { |
| 105 | return result |
| 106 | } |
| 107 | |
| 108 | saveGlobalConfig(current => ({ |
| 109 | ...current, |
| 110 | metricsStatusCache: { |
| 111 | enabled: result.enabled, |
| 112 | timestamp: Date.now(), |
| 113 | }, |
| 114 | })) |
| 115 | return result |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if metrics are enabled for the current organization. |
no test coverage detected