-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add persistent retry queue for failed telemetry events (#4940) #6567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f34c411
feat: add persistent retry queue for failed telemetry events (#4940)
hannesrudolph 5e3838a
feat: add translations for connection status in all supported languages
hannesrudolph 448a4f0
fix: address PR review comments for telemetry queue implementation
hannesrudolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,8 @@ import { CloudSettingsService } from "./CloudSettingsService" | |||||||||||||||||
| import { StaticSettingsService } from "./StaticSettingsService" | ||||||||||||||||||
| import { TelemetryClient } from "./TelemetryClient" | ||||||||||||||||||
| import { ShareService, TaskNotFoundError } from "./ShareService" | ||||||||||||||||||
| import { ConnectionMonitor } from "./ConnectionMonitor" | ||||||||||||||||||
| import { TelemetryQueueManager } from "./TelemetryQueueManager" | ||||||||||||||||||
|
|
||||||||||||||||||
| type AuthStateChangedPayload = CloudServiceEvents["auth-state-changed"][0] | ||||||||||||||||||
| type AuthUserInfoPayload = CloudServiceEvents["user-info"][0] | ||||||||||||||||||
|
|
@@ -35,6 +37,9 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs | |||||||||||||||||
| private settingsService: SettingsService | null = null | ||||||||||||||||||
| private telemetryClient: TelemetryClient | null = null | ||||||||||||||||||
| private shareService: ShareService | null = null | ||||||||||||||||||
| private connectionMonitor: ConnectionMonitor | null = null | ||||||||||||||||||
| private queueManager: TelemetryQueueManager | null = null | ||||||||||||||||||
| private connectionRestoredDebounceTimer: NodeJS.Timeout | null = null | ||||||||||||||||||
| private isInitialized = false | ||||||||||||||||||
| private log: (...args: unknown[]) => void | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -87,9 +92,60 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs | |||||||||||||||||
| this.settingsService = cloudSettingsService | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| this.telemetryClient = new TelemetryClient(this.authService, this.settingsService) | ||||||||||||||||||
| this.telemetryClient = new TelemetryClient(this.authService, this.settingsService, false, this.log) | ||||||||||||||||||
| this.shareService = new ShareService(this.authService, this.settingsService, this.log) | ||||||||||||||||||
|
|
||||||||||||||||||
| // Initialize connection monitor and queue manager | ||||||||||||||||||
| this.connectionMonitor = new ConnectionMonitor() | ||||||||||||||||||
| this.queueManager = TelemetryQueueManager.getInstance() | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check if telemetry queue is enabled | ||||||||||||||||||
| let isQueueEnabled = true | ||||||||||||||||||
| try { | ||||||||||||||||||
| const { ContextProxy } = await import("../../../src/core/config/ContextProxy") | ||||||||||||||||||
| isQueueEnabled = ContextProxy.instance.getValue("telemetryQueueEnabled") ?? true | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
| // Default to enabled if we can't access settings | ||||||||||||||||||
| this.log("[CloudService] Could not access telemetryQueueEnabled setting:", error) | ||||||||||||||||||
| isQueueEnabled = true | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (isQueueEnabled) { | ||||||||||||||||||
| // Set up connection monitoring with debouncing | ||||||||||||||||||
| const connectionRestoredDebounceDelay = 3000 // 3 seconds | ||||||||||||||||||
|
|
||||||||||||||||||
| this.connectionMonitor.onConnectionRestored(() => { | ||||||||||||||||||
| this.log("[CloudService] Connection restored, scheduling queue processing") | ||||||||||||||||||
|
|
||||||||||||||||||
| // Clear any existing timer | ||||||||||||||||||
| if (this.connectionRestoredDebounceTimer) { | ||||||||||||||||||
| clearTimeout(this.connectionRestoredDebounceTimer) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Schedule queue processing with debounce | ||||||||||||||||||
| this.connectionRestoredDebounceTimer = setTimeout(() => { | ||||||||||||||||||
| this.queueManager | ||||||||||||||||||
| ?.processQueue() | ||||||||||||||||||
| .then(() => { | ||||||||||||||||||
| this.log( | ||||||||||||||||||
| "[CloudService] Successfully processed queued events after connection restored", | ||||||||||||||||||
| ) | ||||||||||||||||||
| }) | ||||||||||||||||||
| .catch((error) => { | ||||||||||||||||||
| this.log("[CloudService] Error processing queue after connection restored:", error) | ||||||||||||||||||
| // Could implement retry logic here if needed in the future | ||||||||||||||||||
| }) | ||||||||||||||||||
| }, connectionRestoredDebounceDelay) | ||||||||||||||||||
| }) | ||||||||||||||||||
|
|
||||||||||||||||||
| // Start monitoring if authenticated | ||||||||||||||||||
| if (this.authService.isAuthenticated()) { | ||||||||||||||||||
| this.connectionMonitor.startMonitoring() | ||||||||||||||||||
| } | ||||||||||||||||||
| } else { | ||||||||||||||||||
| this.log("[CloudService] Telemetry queue is disabled") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| try { | ||||||||||||||||||
| TelemetryService.instance.register(this.telemetryClient) | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
|
|
@@ -222,6 +278,34 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs | |||||||||||||||||
| return this.shareService!.canShareTask() | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Connection Status | ||||||||||||||||||
|
|
||||||||||||||||||
| public isOnline(): boolean { | ||||||||||||||||||
| this.ensureInitialized() | ||||||||||||||||||
| return this.connectionMonitor?.getConnectionStatus() ?? true | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public onConnectionRestored(callback: () => void): void { | ||||||||||||||||||
| this.ensureInitialized() | ||||||||||||||||||
| if (this.connectionMonitor) { | ||||||||||||||||||
| this.connectionMonitor.onConnectionRestored(callback) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public onConnectionLost(callback: () => void): void { | ||||||||||||||||||
| this.ensureInitialized() | ||||||||||||||||||
| if (this.connectionMonitor) { | ||||||||||||||||||
| this.connectionMonitor.onConnectionLost(callback) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public removeConnectionListener(event: "connection-restored" | "connection-lost", callback: () => void): void { | ||||||||||||||||||
| this.ensureInitialized() | ||||||||||||||||||
| if (this.connectionMonitor) { | ||||||||||||||||||
| this.connectionMonitor.removeListener(event, callback) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Lifecycle | ||||||||||||||||||
|
|
||||||||||||||||||
| public dispose(): void { | ||||||||||||||||||
|
|
@@ -235,6 +319,14 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs | |||||||||||||||||
| } | ||||||||||||||||||
| this.settingsService.dispose() | ||||||||||||||||||
| } | ||||||||||||||||||
| if (this.connectionMonitor) { | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Memory leak - The Consider adding cleanup in the dispose method:
Suggested change
|
||||||||||||||||||
| this.connectionMonitor.dispose() | ||||||||||||||||||
| } | ||||||||||||||||||
| // Clean up any pending debounce timer | ||||||||||||||||||
| if (this.connectionRestoredDebounceTimer) { | ||||||||||||||||||
| clearTimeout(this.connectionRestoredDebounceTimer) | ||||||||||||||||||
| this.connectionRestoredDebounceTimer = null | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| this.isInitialized = false | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { EventEmitter } from "events" | ||
| import { getRooCodeApiUrl } from "./Config" | ||
|
|
||
| export class ConnectionMonitor extends EventEmitter { | ||
| private isOnline = true | ||
| private checkInterval: NodeJS.Timeout | null = null | ||
| private readonly healthCheckEndpoint = "/api/health" | ||
| private readonly defaultCheckInterval = 30000 // 30 seconds | ||
| private readonly defaultTimeoutMs = 5000 // 5 seconds | ||
|
|
||
| constructor() { | ||
| super() | ||
| } | ||
|
|
||
| /** | ||
| * Check if the connection to the API is available | ||
| */ | ||
| public async checkConnection(): Promise<boolean> { | ||
| try { | ||
| const controller = new AbortController() | ||
| const timeoutId = setTimeout(() => controller.abort(), this.defaultTimeoutMs) | ||
|
|
||
| const response = await fetch(`${getRooCodeApiUrl()}${this.healthCheckEndpoint}`, { | ||
| method: "GET", | ||
| signal: controller.signal, | ||
| }) | ||
|
|
||
| clearTimeout(timeoutId) | ||
|
|
||
| const wasOffline = !this.isOnline | ||
| this.isOnline = response.ok | ||
|
|
||
| // Emit event if connection status changed from offline to online | ||
| if (wasOffline && this.isOnline) { | ||
| this.emit("connection-restored") | ||
| } | ||
|
|
||
| return this.isOnline | ||
| } catch (_error) { | ||
| const wasOnline = this.isOnline | ||
| this.isOnline = false | ||
|
|
||
| // Emit event if connection status changed from online to offline | ||
| if (wasOnline && !this.isOnline) { | ||
| this.emit("connection-lost") | ||
| } | ||
|
|
||
| return false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get current connection status | ||
| */ | ||
| public getConnectionStatus(): boolean { | ||
| return this.isOnline | ||
| } | ||
|
|
||
| /** | ||
| * Register a callback for when connection is restored | ||
| */ | ||
| public onConnectionRestored(callback: () => void): void { | ||
| this.on("connection-restored", callback) | ||
| } | ||
|
|
||
| /** | ||
| * Register a callback for when connection is lost | ||
| */ | ||
| public onConnectionLost(callback: () => void): void { | ||
| this.on("connection-lost", callback) | ||
| } | ||
|
|
||
| /** | ||
| * Start monitoring the connection | ||
| */ | ||
| public startMonitoring(intervalMs: number = this.defaultCheckInterval): void { | ||
| // Stop any existing monitoring | ||
| this.stopMonitoring() | ||
|
|
||
| // Initial check | ||
| this.checkConnection() | ||
|
|
||
| // Set up periodic checks | ||
| this.checkInterval = setInterval(() => { | ||
| this.checkConnection() | ||
| }, intervalMs) | ||
| } | ||
|
|
||
| /** | ||
| * Stop monitoring the connection | ||
| */ | ||
| public stopMonitoring(): void { | ||
| if (this.checkInterval) { | ||
| clearInterval(this.checkInterval) | ||
| this.checkInterval = null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clean up resources | ||
| */ | ||
| public dispose(): void { | ||
| this.stopMonitoring() | ||
| this.removeAllListeners() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dynamic import for ContextProxy could fail. Consider adding more robust error handling: