Skip to content

Commit

Permalink
Improve how package managers handle first failures. Add a AttemptFast…
Browse files Browse the repository at this point in the history
…Repair method (fix #2920)
  • Loading branch information
marticliment committed Nov 14, 2024
1 parent 8c938db commit 15d564a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/UniGetUI.PAckageEngine.Interfaces/IPackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public interface IPackageManager : ISourceProvider, IPackageDetailsProvider, IOp
/// </summary>
public void RefreshPackageIndexes();

/// <summary>
/// This method should attempt to resolve issues
/// such as COM API disconnect or similar issues that
/// can easily be resolved by reconnecting to a client and/or
/// clearing some internal caches. See the WinGet implementation for
/// an example
/// </summary>
public void AttemptFastRepair();

public IManagerSource GetSourceOrDefault(string SourceName);
public IManagerSource? GetSourceIfExists(string SourceName);
}
Expand Down
22 changes: 22 additions & 0 deletions src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ private void ReRegisterCOMServer()
NativePackageHandler.Clear();
}

public override void AttemptFastRepair()
{
try
{
if (WinGetHelper.Instance is NativeWinGetHelper)
{
Logger.ImportantInfo("Attempting to reconnec to WinGet COM Server...");
ReRegisterCOMServer();
NO_PACKAGES_HAVE_BEEN_LOADED = false;
}
else
{
Logger.Warn("Attempted to reconnect to COM Server but Bundled WinGet is being used.");
}
} catch (Exception ex)
{
Logger.Error("An error ocurred while attempting to reconnect to COM Server");
Logger.Error(ex);
}
}



public override void RefreshPackageIndexes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public OperationVeredict GetOperationResult(IPackage package, OperationType oper
{
throw new NotImplementedException();
}

public void AttemptFastRepair()
{
throw new NotImplementedException();
}
}

internal sealed class NullSourceProvider : BaseSourceProvider<IPackageManager>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ private IEnumerable<IPackage> _findPackages(string query, bool SecondAttempt)
catch (Exception e)
{
if (!SecondAttempt)
{
while (e is AggregateException) e = e.InnerException ?? new("How did we get here?");
Logger.Warn($"Manager {DisplayName} failed to find packages with exception {e.GetType().Name}: {e.Message}");
Logger.Warn($"Since this was the first attempt, {Name}.AttemptFastRepair() will be called and the procedure will be restarted");
AttemptFastRepair();
return _findPackages(query, true);
}
else
{
Logger.Error("Error finding packages on manager " + Name + " with query " + query);
Expand All @@ -199,9 +205,6 @@ public IEnumerable<IPackage> GetAvailableUpdates()
=> _getAvailableUpdates(false);

private IEnumerable<IPackage> _getAvailableUpdates(bool SecondAttempt)
/* => TaskRecycler<IEnumerable<IPackage>>.RunOrAttach(_getAvailableUpdates, Properties.Name.GetHashCode());
private IEnumerable<IPackage> _getAvailableUpdates(int _uselessParam)*/
{
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetAvailableUpdates was called"); return []; }
try
Expand All @@ -225,7 +228,13 @@ private IEnumerable<IPackage> _getAvailableUpdates(int _uselessParam)*/
catch (Exception e)
{
if (!SecondAttempt)
{
while (e is AggregateException) e = e.InnerException ?? new("How did we get here?");
Logger.Warn($"Manager {DisplayName} failed to list available updates with exception {e.GetType().Name}: {e.Message}");
Logger.Warn($"Since this was the first attempt, {Name}.AttemptFastRepair() will be called and the procedure will be restarted");
AttemptFastRepair();
return _getAvailableUpdates(true);
}
else
{
Logger.Error("Error finding updates on manager " + Name);
Expand Down Expand Up @@ -264,7 +273,13 @@ private IEnumerable<IPackage> _getInstalledPackages(bool SecondAttempt)
catch (Exception e)
{
if (!SecondAttempt)
{
while (e is AggregateException) e = e.InnerException ?? new("How did we get here?");
Logger.Warn($"Manager {DisplayName} failed to list installed packages with exception {e.GetType().Name}: {e.Message}");
Logger.Warn($"Since this was the first attempt, {Name}.AttemptFastRepair() will be called and the procedure will be restarted");
AttemptFastRepair();
return _getInstalledPackages(true);
}
else
{
Logger.Error("Error finding installed packages on manager " + Name);
Expand Down Expand Up @@ -306,6 +321,12 @@ public virtual async void RefreshPackageIndexes()
await Task.CompletedTask;
}

public virtual void AttemptFastRepair()
{
// Implementing this method is optional
}


// BEGIN SOURCE-RELATED METHODS

/// <summary>
Expand Down

0 comments on commit 15d564a

Please sign in to comment.