diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index cbef9fe633..630c2270b5 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -8083,7 +8083,12 @@ void GUI_App::check_bundle_updates() bool GUI_App::unsubscribe_bundle(const std::string& id) { auto orca_agent = std::dynamic_pointer_cast(m_agent->get_cloud_agent()); - return orca_agent->unsubscribe_bundle(id); + if (!orca_agent->unsubscribe_bundle(id)) + return false; + // The local copy is removed by the sync loop's next subscriptions fetch, which nothing else asks for while the + // Preset Bundle dialog is open. + request_subscriptions_fetch(); + return true; } void GUI_App::start_sync_user_preset(bool with_progress_dlg) @@ -8208,24 +8213,37 @@ void GUI_App::start_sync_user_preset(bool with_progress_dlg) std::unordered_set bundles_synced; std::unordered_set known_available_updates; + // Local to this thread, so a restarted sync (Sync Presets) fetches the subscribed bundles straight away. + OrcaCloudServiceAgent::SubscriptionFetchSchedule subscriptions_fetch; bool update_available = false; - // Sync once immediately, then every 60 seconds. while (!t.expired()) { ++tick_tock; - // Sync once immediately, then every 60s, or right away when a force-push or a preset migration asked - // for it. The request is consumed on every tick, so one made before a scheduled pass is served by it - // instead of running a second pass straight after. + // Push user presets once immediately, then every 60s, or right away when a force-push or a preset + // migration asked for it. The request is consumed on every tick, so one made before a scheduled pass is + // served by it instead of running a second pass straight after. const bool push_now = m_sync_user_presets_now.exchange(false, std::memory_order_acq_rel); - if (tick_tock % 120 == 0 || push_now) { - tick_tock = 0; + const bool push_due = tick_tock % 120 == 0 || push_now; + // Fetch the subscribed bundles on their own schedule (see SubscriptionFetchSchedule). A request made + // while a fetch is running is kept for the next pass. + if (m_subscriptions_fetch_requested.exchange(false, std::memory_order_acq_rel)) + subscriptions_fetch.request(); + const auto now = std::chrono::steady_clock::now(); + // Only due when this pass can serve it: signed out or without a preset bundle the request stays + // pending and the loop sleeps instead of consuming it and spinning. + const bool fetch_due = m_agent && m_agent->is_user_login() && preset_bundle && orca_agent && + subscriptions_fetch.due(now); + if (push_due || fetch_due) { + if (push_due) + tick_tock = 0; if (m_agent) { if (!m_agent->is_user_login()) { continue; } - //sync preset if (!preset_bundle) continue; - + } + //sync preset + if (m_agent && push_due) { int total_count = 0; sync_count = preset_bundle->prints.get_user_presets(preset_bundle, presets_to_sync); @@ -8280,8 +8298,11 @@ void GUI_App::start_sync_user_preset(bool with_progress_dlg) } // sync subscribed bundles, if orca - if (orca_agent) + if (fetch_due) { + // Recorded as the attempt starts, after the checks above: a skipped pass keeps the request, + // and a failed fetch waits for the next slot instead of every half-second tick. + subscriptions_fetch.note_attempt(std::chrono::steady_clock::now()); bundles_to_sync.clear(); bundles_synced.clear(); std::vector not_found; diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index bbd68bf326..43edb65992 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -374,6 +374,7 @@ private: std::atomic m_restart_sync_pending {false}; std::atomic m_sync_user_preset_dlg_active {false}; // a manual "Sync Presets" progress dialog is on screen (see restart_sync_user_preset) std::atomic m_sync_user_presets_now {false}; // request the sync loop to push user presets on its next tick + std::atomic m_subscriptions_fetch_requested {false}; // focus, the cloud pane or an unsubscribe asked the sync loop to fetch the subscribed bundles; requests coalesce and one fetch runs at most a minute bool m_user_preset_pull_pending {false}; // set when a sync thread starts, cleared once its pull is applied; a failed pull or a stopped sync leaves it set bool m_legacy_filament_check_due {false}; // the startup check waits for that pull bool m_legacy_filament_check_running {false}; // a check is in progress, its dialog may be open @@ -662,6 +663,9 @@ public: void start_sync_user_preset(bool with_progress_dlg = false); void stop_sync_user_preset(); void restart_sync_user_preset(); + // Ask the sync loop to fetch the subscribed bundles, so a bundle subscribed on the web or in the cloud pane shows up + // when the user comes back. The loop fetches at most once a minute. + void request_subscriptions_fetch() { m_subscriptions_fetch_requested.store(true, std::memory_order_release); } // Resolve a cloud sync 409 by force-pushing the conflicting preset: clears the "hold" // state the conflict left behind and queues it to be re-uploaded with force=true. void force_push_conflicting_preset(const std::string& setting_id); diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 35a95fe9c6..9d7597f616 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -775,6 +775,8 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, BORDERLESS_FRAME_ Bind(wxEVT_ACTIVATE, [this](wxActivateEvent& event) { if (m_plater != nullptr && event.GetActive()) m_plater->on_activate(); + if (event.GetActive()) + wxGetApp().request_subscriptions_fetch(); event.Skip(); }); @@ -1571,6 +1573,12 @@ void MainFrame::init_tabpanel() { //BBS wxWindow* panel = m_tabpanel->GetCurrentPage(); //wxString page_text = m_tabpanel->GetPageText(sel); + // Leaving the Home tab with the cloud pane open: a bundle subscribed there never took the focus from + // the main window, so ask for it here. The old page comes from the event: the Home page shown at + // startup is never recorded in m_last_selected_tab. + if (const int old_sel = e.GetOldSelection(); old_sel != wxNOT_FOUND && m_tabpanel->GetPageName(size_t(old_sel)) == TAB_ID_HOME) + if (WebViewPanel* home = WebViewPanel::if_built(); home && home->cloud_mode_active()) + wxGetApp().request_subscriptions_fetch(); m_last_selected_tab = m_tabpanel->GetSelectedPageName(); if (panel == m_plater) { if (m_last_selected_tab == TAB_ID_PREPARE) { diff --git a/src/slic3r/GUI/WebViewDialog.cpp b/src/slic3r/GUI/WebViewDialog.cpp index 867c342b30..856c53260d 100644 --- a/src/slic3r/GUI/WebViewDialog.cpp +++ b/src/slic3r/GUI/WebViewDialog.cpp @@ -573,6 +573,8 @@ void WebViewPanel::set_cloud_mode(bool on, const wxString& url, const wxString& // Closing keeps the pane alive with its session, so re-opening does not have to sign in // again (guide ยง10). Nothing is cleared here. m_cloud_browser->Hide(); + // A bundle subscribed in the pane never takes the focus from the main window, so ask for it here. + wxGetApp().request_subscriptions_fetch(); } apply_cloud_layout(on && m_cloud_browser != nullptr); } diff --git a/src/slic3r/Utils/OrcaCloudServiceAgent.hpp b/src/slic3r/Utils/OrcaCloudServiceAgent.hpp index 7768c02f25..4fd4c56914 100644 --- a/src/slic3r/Utils/OrcaCloudServiceAgent.hpp +++ b/src/slic3r/Utils/OrcaCloudServiceAgent.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -335,6 +336,43 @@ public: bool unsubscribe_bundle(const std::string& bundle_id); std::string get_bundle_url(const std::string& bundle_id) const; int get_subscribed_bundles(std::vector>* bundles,std::vector& notfound, std::vector& unauthorized); + // When the preset sync loop polls for the subscribed bundles. It polls on its first pass (no attempt yet), which + // covers startup, sign-in and Sync Presets. After that it polls when asked (window focus, leaving the cloud pane, + // an unsubscribe), at most once a minute, and every 30 minutes for every signed-in user, bundles or not, so a + // subscription made elsewhere still arrives. Plain value state, so the timing is testable without a thread. + struct SubscriptionFetchSchedule + { + // Shortest gap between two requested polls, so a burst of triggers costs one. + static constexpr std::chrono::seconds MIN_REQUEST_INTERVAL{60}; + // Gap at which the loop polls without a request. It bounds the wait before the next attempt, so a failing + // server is not retried on every tick; it says nothing about when a poll last succeeded. + static constexpr std::chrono::seconds FALLBACK_INTERVAL{30 * 60}; + + // A request not served yet. Requests collapse into this flag, and one made during a poll is kept for the next. + bool requested{false}; + // When a poll was last attempted; empty until the first. A pass that cannot poll (signed out, no preset + // bundle) leaves it untouched, so the request stays due for the first pass that can serve it. + std::optional last_attempt; + + // Merges a request; the sync loop calls this when its request flag was set. + void request() { requested = true; } + + bool due(std::chrono::steady_clock::time_point now) const + { + if (!last_attempt) + return true; + const auto since_last_attempt = now - *last_attempt; + return requested ? since_last_attempt >= MIN_REQUEST_INTERVAL : since_last_attempt >= FALLBACK_INTERVAL; + } + + // Records an attempt as it starts, after the signed-in and preset-bundle checks. It clears the request it + // serves and starts the throttle, so a failed poll waits for the next slot rather than every tick. + void note_attempt(std::chrono::steady_clock::time_point now) + { + last_attempt = now; + requested = false; + } + }; int get_shared_bundle(const std::string& bundle_id, std::map>* presets, BundleMetadata* bundle_metadata); // ======================================================================== diff --git a/tests/slic3rutils/test_orca_cloud_agent.cpp b/tests/slic3rutils/test_orca_cloud_agent.cpp index b386a15fd6..e5df906424 100644 --- a/tests/slic3rutils/test_orca_cloud_agent.cpp +++ b/tests/slic3rutils/test_orca_cloud_agent.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -901,3 +902,82 @@ TEST_CASE_METHOD(HealthCheckFixture, "A pull answered 304 keeps the sync state i CHECK(agent->get_sync_state().last_sync_timestamp == 0); CHECK_FALSE(fs::exists(dir.path() / "user" / "user-a" / "sync_state")); } + +namespace { + +const std::chrono::steady_clock::time_point SYNC_T0 = std::chrono::steady_clock::time_point{} + 1000h; + +using SubscriptionFetchSchedule = OrcaCloudServiceAgent::SubscriptionFetchSchedule; + +} // namespace + +TEST_CASE("The preset sync's first pass fetches the subscribed bundles", "[OrcaCloudServiceAgent][Subscriptions]") +{ + // Startup, sign-in and Sync Presets all start a new sync thread, which has not fetched yet. + SubscriptionFetchSchedule schedule; + CHECK(schedule.due(SYNC_T0)); +} + +TEST_CASE("A signed-in user's subscriptions are polled every 30 minutes even with no bundles", "[OrcaCloudServiceAgent][Subscriptions]") +{ + // A subscription made in the cloud pane or on another device is picked up by this poll, whatever the current + // bundle list holds. + SubscriptionFetchSchedule schedule; + schedule.note_attempt(SYNC_T0); + CHECK_FALSE(schedule.due(SYNC_T0 + 29min)); + CHECK(schedule.due(SYNC_T0 + 30min)); +} + +TEST_CASE("A request within a minute of the last poll waits for the minute to pass", "[OrcaCloudServiceAgent][Subscriptions]") +{ + SubscriptionFetchSchedule schedule; + schedule.note_attempt(SYNC_T0); + schedule.request(); + + // Asking whether a fetch is due does not consume the request, so repeated ticks keep it until the minute is up. + CHECK_FALSE(schedule.due(SYNC_T0 + 59s)); + CHECK_FALSE(schedule.due(SYNC_T0 + 59s)); + CHECK(schedule.due(SYNC_T0 + 1min)); +} + +TEST_CASE("A request made while a fetch is running is served by the next pass and then cleared", "[OrcaCloudServiceAgent][Subscriptions]") +{ + SubscriptionFetchSchedule schedule; + schedule.note_attempt(SYNC_T0); // a fetch starts + schedule.request(); // the window gains focus while it runs + + // Not dropped, but still throttled to once a minute. + CHECK_FALSE(schedule.due(SYNC_T0 + 59s)); + CHECK(schedule.due(SYNC_T0 + 1min)); + + // The pass the request asked for runs; any further request would have to be made again. + schedule.note_attempt(SYNC_T0 + 1min); + CHECK_FALSE(schedule.due(SYNC_T0 + 1min + 1s)); + CHECK(schedule.due(SYNC_T0 + 31min)); +} + +TEST_CASE("A pass that cannot fetch records no attempt", "[OrcaCloudServiceAgent][Subscriptions]") +{ + // Signed out or without a preset bundle, the loop evaluates the schedule but skips the fetch, so it never records + // an attempt: the request keeps its place and is served by the first pass that can fetch. + SubscriptionFetchSchedule schedule; + schedule.note_attempt(SYNC_T0); + schedule.request(); + + CHECK_FALSE(schedule.due(SYNC_T0 + 30s)); + REQUIRE(schedule.last_attempt == SYNC_T0); + + // The request survives the skipped pass and is due once the minute since the real attempt is up. + CHECK(schedule.due(SYNC_T0 + 1min)); +} + +TEST_CASE("A failed fetch waits for the next slot instead of retrying on every tick", "[OrcaCloudServiceAgent][Subscriptions]") +{ + // The attempt is recorded as it starts, so a failing server is polled again only after the fallback, not on each + // half-second tick. + SubscriptionFetchSchedule schedule; + schedule.note_attempt(SYNC_T0); + CHECK_FALSE(schedule.due(SYNC_T0 + 1s)); + CHECK_FALSE(schedule.due(SYNC_T0 + 59s)); + CHECK(schedule.due(SYNC_T0 + 30min)); +}