(reason: SessionActivityReason)
| 92 | * is registered, start a periodic heartbeat timer. |
| 93 | */ |
| 94 | export function startSessionActivity(reason: SessionActivityReason): void { |
| 95 | refcount++ |
| 96 | activeReasons.set(reason, (activeReasons.get(reason) ?? 0) + 1) |
| 97 | if (refcount === 1) { |
| 98 | oldestActivityStartedAt = Date.now() |
| 99 | if (activityCallback !== null && heartbeatTimer === null) { |
| 100 | startHeartbeatTimer() |
| 101 | } |
| 102 | } |
| 103 | if (!cleanupRegistered) { |
| 104 | cleanupRegistered = true |
| 105 | registerCleanup(async () => { |
| 106 | logForDiagnosticsNoPII('info', 'session_activity_at_shutdown', { |
| 107 | refcount, |
| 108 | active: Object.fromEntries(activeReasons), |
| 109 | // Only meaningful while work is in-flight; stale otherwise. |
| 110 | oldest_activity_ms: |
| 111 | refcount > 0 && oldestActivityStartedAt !== null |
| 112 | ? Date.now() - oldestActivityStartedAt |
| 113 | : null, |
| 114 | }) |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Decrement the activity refcount. When it reaches 0, stop the heartbeat timer |
no test coverage detected