Skip to content

Commit

Permalink
Pull apt updates less frequently (#1790)
Browse files Browse the repository at this point in the history
  • Loading branch information
meisenzahl committed May 9, 2022
1 parent 0cb566c commit 3b11e7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ public class AppCenter.App : Gtk.Application {
main_window = new MainWindow (this);


// Force a cache refresh when the window opens, so we get new apps
// Force a Flatpak cache refresh when the window opens, so we get new apps
#if HOMEPAGE
main_window.homepage_loaded.connect (() => {
client.update_cache.begin (true);
client.update_cache.begin (true, AppCenterCore.Client.CacheUpdateType.FLATPAK);
});
#else
client.update_cache.begin (true);
client.update_cache.begin (true, AppCenterCore.Client.CacheUpdateType.FLATPAK);
#endif

main_window.destroy.connect (() => {
Expand Down Expand Up @@ -332,13 +332,13 @@ public class AppCenter.App : Gtk.Application {
});
}

private void on_cache_update_failed (Error error) {
private void on_cache_update_failed (Error error, AppCenterCore.Client.CacheUpdateType cache_update_type) {
if (main_window == null) {
return;
}

if (update_fail_dialog == null) {
update_fail_dialog = new UpdateFailDialog (format_error_message (error.message));
update_fail_dialog = new UpdateFailDialog (format_error_message (error.message), cache_update_type);
update_fail_dialog.transient_for = main_window;

update_fail_dialog.destroy.connect (() => {
Expand Down
39 changes: 27 additions & 12 deletions src/Core/Client.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class AppCenterCore.Client : Object {
public signal void operation_finished (Package package, Package.State operation, Error? error);
public signal void cache_update_failed (Error error);
public signal void cache_update_failed (Error error, CacheUpdateType cache_update_type);
/**
* This signal is likely to be fired from a non-main thread. Ensure any UI
* logic driven from this runs on the GTK thread
Expand Down Expand Up @@ -103,7 +103,7 @@ public class AppCenterCore.Client : Object {
}
}

public async void update_cache (bool force = false) {
public async void update_cache (bool force = false, CacheUpdateType cache_update_type = CacheUpdateType.ALL) {
cancellable.reset ();

debug ("update cache called %s", force.to_string ());
Expand Down Expand Up @@ -137,8 +137,16 @@ public class AppCenterCore.Client : Object {

refresh_in_progress = true;
try {
success = yield BackendAggregator.get_default ().refresh_cache (cancellable);
if (success) {
switch (cache_update_type) {
case CacheUpdateType.FLATPAK:
success = yield FlatpakBackend.get_default ().refresh_cache (cancellable);
break;
case CacheUpdateType.ALL:
success = yield BackendAggregator.get_default ().refresh_cache (cancellable);
break;
}

if (success && cache_update_type == CacheUpdateType.ALL) {
last_cache_update = new DateTime.now_utc ();
AppCenter.App.settings.set_int64 ("last-refresh-time", last_cache_update.to_unix ());
}
Expand All @@ -147,7 +155,7 @@ public class AppCenterCore.Client : Object {
} catch (Error e) {
if (!(e is GLib.IOError.CANCELLED)) {
critical ("Update_cache: Refesh cache async failed - %s", e.message);
cache_update_failed (e);
cache_update_failed (e, cache_update_type);
}
} finally {
refresh_in_progress = false;
Expand All @@ -157,14 +165,16 @@ public class AppCenterCore.Client : Object {
debug ("Too soon to refresh and not forced");
}

var next_refresh = SECONDS_BETWEEN_REFRESHES - (uint)seconds_since_last_refresh;
debug ("Setting a timeout for a refresh in %f minutes", next_refresh / 60.0f);
update_cache_timeout_id = GLib.Timeout.add_seconds (next_refresh, () => {
update_cache_timeout_id = 0;
update_cache.begin (true);
if (cache_update_type == CacheUpdateType.ALL) {
var next_refresh = SECONDS_BETWEEN_REFRESHES - (uint)seconds_since_last_refresh;
debug ("Setting a timeout for a refresh in %f minutes", next_refresh / 60.0f);
update_cache_timeout_id = GLib.Timeout.add_seconds (next_refresh, () => {
update_cache_timeout_id = 0;
update_cache.begin (true);

return GLib.Source.REMOVE;
});
return GLib.Source.REMOVE;
});
}

if (nm.get_network_available ()) {
if ((force || last_cache_update_is_old) && AppCenter.App.settings.get_boolean ("automatic-updates")) {
Expand Down Expand Up @@ -203,4 +213,9 @@ public class AppCenterCore.Client : Object {
public static unowned Client get_default () {
return instance.once (() => { return new Client (); });
}

public enum CacheUpdateType {
FLATPAK,
ALL
}
}
8 changes: 5 additions & 3 deletions src/Dialogs/UpdateFailDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
public class UpdateFailDialog : Granite.MessageDialog {
private const int TRY_AGAIN_RESPONSE_ID = 1;
public string error_message { get; construct; }
public AppCenterCore.Client.CacheUpdateType cache_update_type { get; construct; }

public UpdateFailDialog (string error_message) {
public UpdateFailDialog (string error_message, AppCenterCore.Client.CacheUpdateType cache_update_type) {
Object (
title: "",
primary_text: _("Failed to Fetch Updates"),
secondary_text: _("This may have been caused by external, manually added software repositories or a corrupted sources file."),
image_icon: new ThemedIcon ("dialog-error"),
buttons: Gtk.ButtonsType.NONE,
error_message: error_message
error_message: error_message,
cache_update_type: cache_update_type
);
}

Expand All @@ -37,7 +39,7 @@ public class UpdateFailDialog : Granite.MessageDialog {

response.connect ((response_id) => {
if (response_id == TRY_AGAIN_RESPONSE_ID) {
AppCenterCore.Client.get_default ().update_cache.begin (true);
AppCenterCore.Client.get_default ().update_cache.begin (true, cache_update_type);
}
destroy ();
});
Expand Down

0 comments on commit 3b11e7b

Please sign in to comment.