Skip to content

Refactor AppDatabaseUpdater for per-server updates #4224

Merged
bgoncal merged 5 commits intomainfrom
improve-db-update-2
Jan 15, 2026
Merged

Refactor AppDatabaseUpdater for per-server updates #4224
bgoncal merged 5 commits intomainfrom
improve-db-update-2

Conversation

@bgoncal
Copy link
Member

@bgoncal bgoncal commented Jan 15, 2026

Summary

Screenshots

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

Reworks AppDatabaseUpdater to support bounded parallel per-server updates, global and per-server throttling with exponential backoff, and improved cancellation handling. Adds detailed documentation, separates update logic into smaller methods, and ensures batched database writes with differential deletes. This improves update reliability, resource usage, and responsiveness to cancellation.
AppDatabaseUpdater now supports concurrent updates per server instead of a single global update. The update method requires a server parameter, and task management is handled via an actor for thread safety. Call sites in SettingsView and WebViewController are updated to use the new API, and legacy global update logic is removed.
@bgoncal bgoncal self-assigned this Jan 15, 2026
Copilot AI review requested due to automatic review settings January 15, 2026 15:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors AppDatabaseUpdater to support per-server updates instead of updating all servers at once. The updater now ensures only one update per server runs at a time (while allowing different servers to update concurrently), implements per-server throttling with exponential backoff on failures, and improves cancellation handling throughout the update pipeline.

Changes:

  • Introduced a TaskCoordinator actor for thread-safe management of per-server update tasks
  • Implemented per-server throttling (120s base) with exponential backoff (up to 5 minutes) on consecutive failures
  • Enhanced cancellation checks at critical points in the update pipeline to enable early exit
  • Updated API from update() to update(server: Server) and modified all call sites accordingly

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
Sources/Shared/Environment/AppDatabaseUpdater.swift Core refactor: added TaskCoordinator actor, per-server task tracking, throttling/backoff logic, and improved cancellation handling throughout
Sources/App/WebView/WebViewController.swift Updated to call update(server:) for the specific server when connection info changes
Sources/App/Settings/Settings/SettingsView.swift Changed to loop through all servers and call update(server:) for each one
Sources/App/Scenes/WebViewSceneDelegate.swift Removed blanket database update call when scene becomes active (now happens per-server on connection change)

bgoncal and others added 2 commits January 15, 2026 16:58
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Wrap all withCheckedContinuation and withCheckedThrowingContinuation usages with [weak self] to prevent retain cycles and ensure continuations are resumed or cancelled if self is deallocated. This improves memory safety and prevents potential leaks or crashes.
@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

❌ Patch coverage is 3.22581% with 150 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@f7780f6). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...ources/Shared/Environment/AppDatabaseUpdater.swift 3.22% 150 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4224   +/-   ##
=======================================
  Coverage        ?   44.32%           
=======================================
  Files           ?      252           
  Lines           ?    14580           
  Branches        ?        0           
=======================================
  Hits            ?     6462           
  Misses          ?     8118           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bgoncal bgoncal merged commit de371a6 into main Jan 15, 2026
21 of 22 checks passed
@bgoncal bgoncal deleted the improve-db-update-2 branch January 15, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants