* 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.
()
| 111 | * known-good disk value. |
| 112 | */ |
| 113 | async function refreshMetricsStatus(): Promise<MetricsStatus> { |
| 114 | const result = await memoizedCheckMetrics() |
| 115 | if (result.hasError) { |
| 116 | return result |
| 117 | } |
| 118 | |
| 119 | const cached = getGlobalConfig().metricsStatusCache |
| 120 | const unchanged = cached !== undefined && cached.enabled === result.enabled |
| 121 | // Skip write when unchanged AND timestamp still fresh — avoids config churn |
| 122 | // when concurrent callers race past a stale disk entry and all try to write. |
| 123 | if (unchanged && Date.now() - cached.timestamp < DISK_CACHE_TTL_MS) { |
| 124 | return result |
| 125 | } |
| 126 | |
| 127 | saveGlobalConfig(current => ({ |
| 128 | ...current, |
| 129 | metricsStatusCache: { |
| 130 | enabled: result.enabled, |
| 131 | timestamp: Date.now(), |
| 132 | }, |
| 133 | })) |
| 134 | return result |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check if metrics are enabled for the current organization. |
no test coverage detected