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