[gnome-software] systemd-updates: Don't trigger the systemd update every time



commit c75fa0f1f72c5832215cc470fbf1f4b89e937b04
Author: Richard Hughes <richard hughsie com>
Date:   Fri May 19 19:34:38 2017 +0100

    systemd-updates: Don't trigger the systemd update every time
    
    When there are a lot of applications to update this can take several seconds.
    Just keep track of the triggered state to avoid the D-Bus round-trip.

 plugins/packagekit/gs-plugin-systemd-updates.c |   69 +++++++++++++++++++++++-
 1 files changed, 67 insertions(+), 2 deletions(-)
---
diff --git a/plugins/packagekit/gs-plugin-systemd-updates.c b/plugins/packagekit/gs-plugin-systemd-updates.c
index 97cc1b4..a8fe82e 100644
--- a/plugins/packagekit/gs-plugin-systemd-updates.c
+++ b/plugins/packagekit/gs-plugin-systemd-updates.c
@@ -35,7 +35,9 @@
 
 struct GsPluginData {
        GFileMonitor            *monitor;
+       GFileMonitor            *monitor_trigger;
        GPermission             *permission;
+       gboolean                 is_triggered;
 };
 
 void
@@ -50,6 +52,8 @@ gs_plugin_destroy (GsPlugin *plugin)
        GsPluginData *priv = gs_plugin_get_data (plugin);
        if (priv->monitor != NULL)
                g_object_unref (priv->monitor);
+       if (priv->monitor_trigger != NULL)
+               g_object_unref (priv->monitor_trigger);
 }
 
 static void
@@ -75,10 +79,34 @@ gs_plugin_systemd_updates_changed_cb (GFileMonitor *monitor,
        gs_plugin_updates_changed (plugin);
 }
 
+static void
+gs_plugin_systemd_updates_refresh_is_triggered (GsPlugin *plugin, GCancellable *cancellable)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+       g_autoptr(GFile) file_trigger = NULL;
+       file_trigger = g_file_new_for_path ("/system-update");
+       priv->is_triggered = g_file_query_exists (file_trigger, NULL);
+       g_debug ("offline trigger is now %s",
+                priv->is_triggered ? "enabled" : "disabled");
+}
+
+static void
+gs_plugin_systemd_trigger_changed_cb (GFileMonitor *monitor,
+                                     GFile *file, GFile *other_file,
+                                     GFileMonitorEvent event_type,
+                                     gpointer user_data)
+{
+       GsPlugin *plugin = GS_PLUGIN (user_data);
+       gs_plugin_systemd_updates_refresh_is_triggered (plugin, NULL);
+}
+
 gboolean
 gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
+       g_autoptr(GFile) file_trigger = NULL;
+
+       /* watch the prepared file */
        priv->monitor = pk_offline_get_prepared_monitor (cancellable, error);
        if (priv->monitor == NULL) {
                gs_utils_error_convert_gio (error);
@@ -88,6 +116,20 @@ gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
                          G_CALLBACK (gs_plugin_systemd_updates_changed_cb),
                          plugin);
 
+       /* watch the trigger file */
+       file_trigger = g_file_new_for_path ("/system-update");
+       priv->monitor_trigger = g_file_monitor_file (file_trigger,
+                                                    G_FILE_MONITOR_NONE,
+                                                    NULL,
+                                                    error);
+       if (priv->monitor_trigger == NULL) {
+               gs_utils_error_convert_gio (error);
+               return FALSE;
+       }
+       g_signal_connect (priv->monitor_trigger, "changed",
+                         G_CALLBACK (gs_plugin_systemd_trigger_changed_cb),
+                         plugin);
+
        /* check if we have permission to trigger the update */
        priv->permission = gs_utils_get_permission (
                "org.freedesktop.packagekit.trigger-offline-update");
@@ -162,6 +204,8 @@ gs_plugin_update_app (GsPlugin *plugin,
                      GCancellable *cancellable,
                      GError **error)
 {
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
        /* if we can process this online do not require a trigger */
        if (gs_app_get_state (app) != AS_APP_STATE_UPDATABLE)
                return TRUE;
@@ -170,6 +214,10 @@ gs_plugin_update_app (GsPlugin *plugin,
        if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0)
                return TRUE;
 
+       /* already in correct state */
+       if (priv->is_triggered)
+               return TRUE;
+
        /* trigger offline update */
        if (!pk_offline_trigger (PK_OFFLINE_ACTION_REBOOT,
                                 cancellable, error)) {
@@ -177,21 +225,38 @@ gs_plugin_update_app (GsPlugin *plugin,
                return FALSE;
        }
 
+       /* don't rely on the file monitor */
+       gs_plugin_systemd_updates_refresh_is_triggered (plugin, cancellable);
+
        /* success! */
        return TRUE;
 }
 
-
 gboolean
 gs_plugin_update_cancel (GsPlugin *plugin,
                         GsApp *app,
                         GCancellable *cancellable,
                         GError **error)
 {
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
        /* only process this app if was created by this plugin */
        if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0)
                return TRUE;
-       return pk_offline_cancel (NULL, error);
+
+       /* already in correct state */
+       if (!priv->is_triggered)
+               return TRUE;
+
+       /* cancel offline update */
+       if (!pk_offline_cancel (NULL, error))
+               return FALSE;
+
+       /* don't rely on the file monitor */
+       gs_plugin_systemd_updates_refresh_is_triggered (plugin, cancellable);
+
+       /* success! */
+       return TRUE;
 }
 
 gboolean


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]