Public
Like
glimpse2-runbook-view-glimpse-save-login-react
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v40View latest version
Scheduled task handlers for automated system maintenance.
- Purpose: Safety net for viewing session cleanup when normal session ending fails
- Schedule: Every minute
- Threshold: Sessions older than 60 seconds without heartbeat updates
- Function: Marks stale sessions as not viewing and syncs to Notion
Multi-Layer Session Ending System:
The viewing system uses a three-tier approach to ensure sessions are properly ended:
-
Primary (Immediate):
beforeunload
event withsendBeacon
- Triggers when user closes tab, navigates away, or browser crashes
- Uses
navigator.sendBeacon()
for maximum reliability during page unload - Falls back to synchronous
fetch
ifsendBeacon
fails - Most reliable for normal user behavior
-
Secondary (Component Cleanup): React
useEffect
cleanup- Triggers when user navigates within the app (SPA navigation)
- Runs when React component unmounts normally
- Uses standard
fetch
with full error handling - Reliable for in-app navigation
-
Tertiary (Safety Net): This cron job
- Catches any sessions missed by the above methods
- Handles edge cases: network failures, browser force-quit, system crashes
- Runs every minute to find sessions with no heartbeat for 60+ seconds
- Guaranteed cleanup for all scenarios
Processing Flow:
- Lists all viewing blobs with prefix
glimpse--viewing--
- Checks each blob's
lastUpdate
timestamp - Identifies sessions where:
sessionStart
is not null (actively viewing)lastUpdate
is older than 60 seconds (stale)
- Updates blob: sets
sessionStart
to null, updateslastUpdate
- Immediately syncs to Notion: Sets "Session start" property to null
- Logs cleanup statistics and any errors
Why This Safety Net is Essential:
- Network failures: Primary/secondary methods may fail to reach server
- Browser crashes: No opportunity for graceful cleanup
- System sleep/hibernate: Processes may be suspended mid-request
- Force quit scenarios: No cleanup functions execute
- Mobile browser behavior: Aggressive tab suspension/termination
- Development/debugging: Stopping processes during testing
Notion Integration:
- Maintains data consistency between blob storage and Notion
- Ensures "Session start" property accurately reflects active sessions
- Prevents "ghost" sessions that appear active but aren't
- Provides reliable session duration tracking
- Purpose: Cleans up orphaned agent blobs
- Schedule: Every minute (configurable in Val Town UI)
- Function: Deletes agent blobs where the agent ID doesn't match the demo's "Glimpse agents" relation in Notion
- Read-Only Notion: Only reads Notion data, never modifies it
- Blob Cleanup: Removes mismatched/orphaned blobs to keep storage synchronized with Notion
Cron schedules are managed through the Val Town web interface. Click on each cron file to modify its schedule as needed.