From 8f9afafb7af0ab10b1ee1da424b4df54b281f312 Mon Sep 17 00:00:00 2001 From: Konstantina Chremmou Date: Tue, 11 Jan 2022 13:20:16 +0000 Subject: [PATCH] Use Tick() when setting both the PercentComplete and the Description to avoid firing OnChanged() twice. Signed-off-by: Konstantina Chremmou --- XenModel/Actions/Action.cs | 18 ++++++++++++++---- .../Actions/DR/ShutdownAndDestroyVMsAction.cs | 4 ++-- .../DR/ShutdownAndDestroyVmAppliancesAction.cs | 4 ++-- XenModel/Actions/DR/VdiLoadMetadataAction.cs | 4 ++-- XenModel/Actions/DR/VdiOpenDatabaseAction.cs | 5 ++--- .../Actions/Host/SingleHostStatusAction.cs | 3 +-- .../UpdateIntegratedGpuPassthroughAction.cs | 4 ++-- XenModel/Actions/MultipleAction.cs | 4 ++-- XenModel/Actions/Network/RescanPIFsAction.cs | 3 +-- .../Actions/Network/UnplugPlugNetworkAction.cs | 4 ++-- .../OvfActions/ExportApplianceAction.cs | 3 +-- .../OvfActions/ImportApplianceAction.cs | 6 ++---- .../Actions/OvfActions/ImportImageAction.cs | 6 ++---- XenModel/Actions/PVS/ConfigurePvsSiteAction.cs | 7 +++---- XenModel/Actions/PVS/DeletePvsSiteAction.cs | 3 +-- .../Actions/Pool_Patch/ApplyPatchAction.cs | 7 +++---- XenModel/Actions/VDI/MoveVirtualDiskAction.cs | 3 +-- XenModel/Actions/VM/VMSnapshotRevertAction.cs | 7 +++---- .../VMAppliances/CreateVMApplianceAction.cs | 4 ++-- XenModel/Actions/VMSS/CreateVMPolicy.cs | 5 +++-- 20 files changed, 51 insertions(+), 53 deletions(-) diff --git a/XenModel/Actions/Action.cs b/XenModel/Actions/Action.cs index 9fba13a38..e646b17bc 100644 --- a/XenModel/Actions/Action.cs +++ b/XenModel/Actions/Action.cs @@ -260,9 +260,13 @@ namespace XenAdmin.Actions NewAction?.Invoke(this); } + /// + /// If you want to set the PercentComplete and the Description at the + /// same time, use Tick() in order to avoid firing OnChanged() twice + /// public string Description { - get { return _description; } + get => _description; set { if (_description != value) @@ -277,7 +281,7 @@ namespace XenAdmin.Actions public bool IsCompleted { - get { return _isCompleted; } + get => _isCompleted; set { if (_isCompleted != value) @@ -289,14 +293,18 @@ namespace XenAdmin.Actions } } + /// + /// If you want to set the PercentComplete and the Description at the + /// same time, use Tick() in order to avoid firing OnChanged() twice + /// public virtual int PercentComplete { - get { return _percentComplete; } + get => _percentComplete; set { if (_percentComplete != value) { - System.Diagnostics.Debug.Assert(0 <= value && value <= 100, string.Format("value percent is {0}", value)); + System.Diagnostics.Debug.Assert(0 <= value && value <= 100, $"Percent is {value}"); var percent = value; if (percent < 0) @@ -318,6 +326,8 @@ namespace XenAdmin.Actions { _description = description; + System.Diagnostics.Debug.Assert(0 <= percent && percent <= 100, $"Percent is {percent}"); + if (percent < 0) percent = 0; else if (percent > 100) diff --git a/XenModel/Actions/DR/ShutdownAndDestroyVMsAction.cs b/XenModel/Actions/DR/ShutdownAndDestroyVMsAction.cs index ab84fc2b2..c9637851c 100644 --- a/XenModel/Actions/DR/ShutdownAndDestroyVMsAction.cs +++ b/XenModel/Actions/DR/ShutdownAndDestroyVMsAction.cs @@ -77,8 +77,8 @@ namespace XenAdmin.Actions.DR DestroyVM(Session, vm); PercentComplete += increment; } - Description = Messages.ACTION_VM_DESTROYED; - PercentComplete = 100; + + Tick(100, Messages.ACTION_VM_DESTROYED); } diff --git a/XenModel/Actions/DR/ShutdownAndDestroyVmAppliancesAction.cs b/XenModel/Actions/DR/ShutdownAndDestroyVmAppliancesAction.cs index 7443c1d63..245403b1d 100644 --- a/XenModel/Actions/DR/ShutdownAndDestroyVmAppliancesAction.cs +++ b/XenModel/Actions/DR/ShutdownAndDestroyVmAppliancesAction.cs @@ -101,8 +101,8 @@ namespace XenAdmin.Actions.DR } PercentComplete += increment; } - Description = Messages.DELETED_VM_APPLIANCES; - PercentComplete = 100; + + Tick(100, Messages.DELETED_VM_APPLIANCES); } } } diff --git a/XenModel/Actions/DR/VdiLoadMetadataAction.cs b/XenModel/Actions/DR/VdiLoadMetadataAction.cs index 9c30fcc38..2d3d04cf4 100644 --- a/XenModel/Actions/DR/VdiLoadMetadataAction.cs +++ b/XenModel/Actions/DR/VdiLoadMetadataAction.cs @@ -130,8 +130,8 @@ namespace XenAdmin.Actions.DR // ignored } } - PercentComplete = 100; - Description = Messages.ACTION_VDI_LOAD_METADATA_DONE; + + Tick(100, Messages.ACTION_VDI_LOAD_METADATA_DONE); } } diff --git a/XenModel/Actions/DR/VdiOpenDatabaseAction.cs b/XenModel/Actions/DR/VdiOpenDatabaseAction.cs index 221147a49..07d54204d 100644 --- a/XenModel/Actions/DR/VdiOpenDatabaseAction.cs +++ b/XenModel/Actions/DR/VdiOpenDatabaseAction.cs @@ -71,9 +71,8 @@ namespace XenAdmin.Actions.DR { MetadataSession = Session.get_record(Session, MetadataSessionRef); } - PercentComplete = 100; - - Description = Messages.ACTION_VDI_OPEN_DATABASE_DONE; + + Tick(100, Messages.ACTION_VDI_OPEN_DATABASE_DONE); } } } diff --git a/XenModel/Actions/Host/SingleHostStatusAction.cs b/XenModel/Actions/Host/SingleHostStatusAction.cs index 4acd78f2c..e8b4d5424 100644 --- a/XenModel/Actions/Host/SingleHostStatusAction.cs +++ b/XenModel/Actions/Host/SingleHostStatusAction.cs @@ -101,8 +101,7 @@ namespace XenAdmin.Actions PollToCompletion(); Status = ReportStatus.succeeded; - Description = Messages.COMPLETED; - PercentComplete = 100; + Tick(100, Messages.COMPLETED); } catch (Exception e) { diff --git a/XenModel/Actions/Host/UpdateIntegratedGpuPassthroughAction.cs b/XenModel/Actions/Host/UpdateIntegratedGpuPassthroughAction.cs index 2450c1c36..26c8e1e06 100644 --- a/XenModel/Actions/Host/UpdateIntegratedGpuPassthroughAction.cs +++ b/XenModel/Actions/Host/UpdateIntegratedGpuPassthroughAction.cs @@ -64,8 +64,8 @@ namespace XenAdmin.Actions : PGPU.async_disable_dom0_access(Session, pGpu.opaque_ref); PollToCompletion(50, 100); } - PercentComplete = 100; - Description = string.Format(Messages.UPDATED_PROPERTIES, Helpers.GetName(Host).Ellipsise(50)); + + Tick(100, string.Format(Messages.UPDATED_PROPERTIES, Helpers.GetName(Host).Ellipsise(50))); } } } diff --git a/XenModel/Actions/MultipleAction.cs b/XenModel/Actions/MultipleAction.cs index 7b7879d14..6470cea59 100644 --- a/XenModel/Actions/MultipleAction.cs +++ b/XenModel/Actions/MultipleAction.cs @@ -98,8 +98,8 @@ namespace XenAdmin.Actions RunSubActions(exceptions); - PercentComplete = 100; - Description = endDescription; + Tick(100, endDescription); + if (exceptions.Count > 1) { foreach (Exception e in exceptions) diff --git a/XenModel/Actions/Network/RescanPIFsAction.cs b/XenModel/Actions/Network/RescanPIFsAction.cs index a5c5537e1..62daa46db 100644 --- a/XenModel/Actions/Network/RescanPIFsAction.cs +++ b/XenModel/Actions/Network/RescanPIFsAction.cs @@ -50,8 +50,7 @@ namespace XenAdmin.Actions { PercentComplete = 40; PIF.scan(Session, Host.opaque_ref); - Description = Messages.COMPLETED; - PercentComplete = 100; + Tick(100, Messages.COMPLETED); } } } diff --git a/XenModel/Actions/Network/UnplugPlugNetworkAction.cs b/XenModel/Actions/Network/UnplugPlugNetworkAction.cs index 7fd7f2dd3..b7b158f05 100644 --- a/XenModel/Actions/Network/UnplugPlugNetworkAction.cs +++ b/XenModel/Actions/Network/UnplugPlugNetworkAction.cs @@ -143,8 +143,8 @@ namespace XenAdmin.Actions PercentComplete += percentStep; } - PercentComplete = 100; - Description = error != null ? Messages.COMPLETED_WITH_ERRORS : Messages.COMPLETED; + Tick(100, error != null ? Messages.COMPLETED_WITH_ERRORS : Messages.COMPLETED); + if (error != null) { throw error; diff --git a/XenModel/Actions/OvfActions/ExportApplianceAction.cs b/XenModel/Actions/OvfActions/ExportApplianceAction.cs index 75a5a0706..1bde70ae1 100644 --- a/XenModel/Actions/OvfActions/ExportApplianceAction.cs +++ b/XenModel/Actions/OvfActions/ExportApplianceAction.cs @@ -163,8 +163,7 @@ namespace XenAdmin.Actions.OvfActions ManifestAndSign(env, appFolder, appFile); } - PercentComplete = 100; - Description = Messages.COMPLETED; + Tick(100, Messages.COMPLETED); } private void ManifestAndSign(EnvelopeType ovfEnv, string appFolder, string appFile) diff --git a/XenModel/Actions/OvfActions/ImportApplianceAction.cs b/XenModel/Actions/OvfActions/ImportApplianceAction.cs index 893dc1643..3a912444c 100644 --- a/XenModel/Actions/OvfActions/ImportApplianceAction.cs +++ b/XenModel/Actions/OvfActions/ImportApplianceAction.cs @@ -116,8 +116,7 @@ namespace XenAdmin.Actions.OvfActions } } - PercentComplete = 20; - Description = string.Format(Messages.IMPORT_APPLIANCE_PREPARING, m_package.Name); + Tick(20, string.Format(Messages.IMPORT_APPLIANCE_PREPARING, m_package.Name)); //create a copy of the OVF var envelopes = new List(); @@ -170,8 +169,7 @@ namespace XenAdmin.Actions.OvfActions m_package.CleanUpWorkingDir(); } - PercentComplete = 100; - Description = Messages.COMPLETED; + Tick(100, Messages.COMPLETED); if (_startAutomatically && importedObject is XenRef applianceRef) { diff --git a/XenModel/Actions/OvfActions/ImportImageAction.cs b/XenModel/Actions/OvfActions/ImportImageAction.cs index 22d1697d4..bb357a771 100644 --- a/XenModel/Actions/OvfActions/ImportImageAction.cs +++ b/XenModel/Actions/OvfActions/ImportImageAction.cs @@ -74,8 +74,7 @@ namespace XenAdmin.Actions.OvfActions string systemid = m_vmMappings.Keys.ElementAt(0); var mapping = m_vmMappings.Values.ElementAt(0); - PercentComplete = 20; - Description = Messages.IMPORTING_DISK_IMAGE; + Tick(20, Messages.IMPORTING_DISK_IMAGE); //create a copy of the ovf envelope EnvelopeType[] envs = OVF.Split(m_ovfEnvelope, "system", new object[] {new[] {systemid}}); @@ -100,8 +99,7 @@ namespace XenAdmin.Actions.OvfActions { importedObject = Process(curEnv, m_directory); - PercentComplete = 100; - Description = Messages.COMPLETED; + Tick(100, Messages.COMPLETED); } catch (OperationCanceledException) { diff --git a/XenModel/Actions/PVS/ConfigurePvsSiteAction.cs b/XenModel/Actions/PVS/ConfigurePvsSiteAction.cs index 0cf0e09df..726936533 100644 --- a/XenModel/Actions/PVS/ConfigurePvsSiteAction.cs +++ b/XenModel/Actions/PVS/ConfigurePvsSiteAction.cs @@ -80,8 +80,7 @@ namespace XenAdmin.Actions if (pvsSite == null) { log.InfoFormat("PVS Site '{0}' cannot be configured, because it cannot be found.", siteName); - PercentComplete = 100; - Description = Messages.COMPLETED; + Tick(100, Messages.COMPLETED); return; } @@ -133,8 +132,8 @@ namespace XenAdmin.Actions PercentComplete += inc; } } - PercentComplete = 100; - Description = Messages.ACTION_CONFUGURE_PVS_SITE_DONE; + + Tick(100, Messages.ACTION_CONFUGURE_PVS_SITE_DONE); } } } diff --git a/XenModel/Actions/PVS/DeletePvsSiteAction.cs b/XenModel/Actions/PVS/DeletePvsSiteAction.cs index 59d96377b..4aa2668bd 100644 --- a/XenModel/Actions/PVS/DeletePvsSiteAction.cs +++ b/XenModel/Actions/PVS/DeletePvsSiteAction.cs @@ -66,8 +66,7 @@ namespace XenAdmin.Actions RelatedTask = PVS_site.async_forget(Session, pvsSite.opaque_ref); PollToCompletion(); - Description = Messages.ACTION_DELETE_PVS_SITE_DONE; - PercentComplete = 100; + Tick(100, Messages.ACTION_DELETE_PVS_SITE_DONE); } } } diff --git a/XenModel/Actions/Pool_Patch/ApplyPatchAction.cs b/XenModel/Actions/Pool_Patch/ApplyPatchAction.cs index bf08dda8e..fe0b1ea7f 100644 --- a/XenModel/Actions/Pool_Patch/ApplyPatchAction.cs +++ b/XenModel/Actions/Pool_Patch/ApplyPatchAction.cs @@ -73,10 +73,9 @@ namespace XenAdmin.Actions HTTP_actions.get_pool_patch_download( bytes => { - PercentComplete = (int)(100 * (double)bytes / patch.size); - - Description = string.Format(Messages.DOWNLOADING_PATCH_FROM, patch.Connection.Name, - Util.DiskSizeString(bytes, 1, "F1"), Util.DiskSizeString(patch.size)); + Tick((int)(100 * (double)bytes / patch.size), + string.Format(Messages.DOWNLOADING_PATCH_FROM, patch.Connection.Name, + Util.DiskSizeString(bytes, 1, "F1"), Util.DiskSizeString(patch.size))); }, () => XenAdminConfigManager.Provider.ForcedExiting || GetCancelling(), XenAdminConfigManager.Provider.GetProxyTimeout(true), diff --git a/XenModel/Actions/VDI/MoveVirtualDiskAction.cs b/XenModel/Actions/VDI/MoveVirtualDiskAction.cs index 0de606612..8b3d2405c 100644 --- a/XenModel/Actions/VDI/MoveVirtualDiskAction.cs +++ b/XenModel/Actions/VDI/MoveVirtualDiskAction.cs @@ -122,8 +122,7 @@ namespace XenAdmin.Actions foreach (var newVbd in newVbds) Connection.WaitForCache(VBD.create(Session, newVbd)); - PercentComplete = 100; - Description = Messages.MOVED; + Tick(100, Messages.MOVED); log.DebugFormat("Moved VDI '{0}'", Helpers.GetName(vdi)); } diff --git a/XenModel/Actions/VM/VMSnapshotRevertAction.cs b/XenModel/Actions/VM/VMSnapshotRevertAction.cs index 95963f7e7..045f4c616 100644 --- a/XenModel/Actions/VM/VMSnapshotRevertAction.cs +++ b/XenModel/Actions/VM/VMSnapshotRevertAction.cs @@ -58,8 +58,8 @@ namespace XenAdmin.Actions RelatedTask = XenAPI.VM.async_revert(Session, m_Snapshot.opaque_ref); PollToCompletion(); _finished = true; - PercentComplete = 90; - Description = String.Format(Messages.REVERTING_POWER_STATE, VM.Name()); + Tick(90, string.Format(Messages.REVERTING_POWER_STATE, VM.Name())); + try { RevertPowerState(m_Snapshot, VM); @@ -69,8 +69,7 @@ namespace XenAdmin.Actions // ignored } - PercentComplete = 100; - Description = String.Format(Messages.VM_REVERTED, m_Snapshot.Name()); + Tick(100, string.Format(Messages.VM_REVERTED, m_Snapshot.Name())); } public override int PercentComplete diff --git a/XenModel/Actions/VMAppliances/CreateVMApplianceAction.cs b/XenModel/Actions/VMAppliances/CreateVMApplianceAction.cs index bc63ec067..a164fdec7 100644 --- a/XenModel/Actions/VMAppliances/CreateVMApplianceAction.cs +++ b/XenModel/Actions/VMAppliances/CreateVMApplianceAction.cs @@ -61,8 +61,8 @@ namespace XenAdmin.Actions { VM.set_appliance(Session, selectedVM.opaque_ref, vmApplianceRef.opaque_ref); } - Description = string.Format(Messages.CREATED_VM_APPLIANCE, _record.Name()); - PercentComplete = 100; + + Tick(100, string.Format(Messages.CREATED_VM_APPLIANCE, _record.Name())); } } } diff --git a/XenModel/Actions/VMSS/CreateVMPolicy.cs b/XenModel/Actions/VMSS/CreateVMPolicy.cs index f8bc35c62..c51084056 100644 --- a/XenModel/Actions/VMSS/CreateVMPolicy.cs +++ b/XenModel/Actions/VMSS/CreateVMPolicy.cs @@ -64,8 +64,9 @@ namespace XenAdmin.Actions { VM.set_snapshot_schedule(Session, selectedVM.opaque_ref, vmssref.opaque_ref); } - Description = string.Format(Messages.CREATED_VMSS, _record.Name()); - PercentComplete = 60; + + Tick(60, string.Format(Messages.CREATED_VMSS, _record.Name())); + if (_runNow) VMSS.snapshot_now(Session, vmssref); PercentComplete = 100;