Skip to content

Commit

Permalink
Update Microsoft Store service to allow callers to set their own pack…
Browse files Browse the repository at this point in the history
…age install timeout (#3897)

* remove store timeout

* add changes based on comments
  • Loading branch information
bbonaby authored Oct 1, 2024
1 parent 5e75f86 commit 8cc7492
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
12 changes: 6 additions & 6 deletions extensions/WSLExtension/Services/WslManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class WslManager : IWslManager, IDisposable

private readonly TimeSpan _oneMinutePollingInterval = TimeSpan.FromMinutes(1);

private readonly TimeSpan _packageInstallTimeOut = TimeSpan.FromMinutes(30);

private readonly WslRegisteredDistributionFactory _wslRegisteredDistributionFactory;

private readonly IWslServicesMediator _wslServicesMediator;
Expand Down Expand Up @@ -176,10 +178,7 @@ public async Task InstallDistributionPackageAsync(
// Install it from the store.
statusUpdateCallback?.Invoke(_stringResource.GetLocalized("DistributionPackageInstallationStart", definition.FriendlyName));
cancellationToken.ThrowIfCancellationRequested();
if (!await _microsoftStoreService.TryInstallPackageAsync(definition.StoreAppId))
{
throw new InvalidDataException($"Failed to install the {definition.Name} package");
}
await _microsoftStoreService.InstallPackageAsync(definition.StoreAppId, _packageInstallTimeOut);
}
else
{
Expand Down Expand Up @@ -225,9 +224,10 @@ public async Task InstallWslKernelPackageAsync(Action<string>? statusUpdateCallb
{
// If not installed, we'll install it from the store.
statusUpdateCallback?.Invoke(_stringResource.GetLocalized("InstallingWslKernelPackage"));

cancellationToken.ThrowIfCancellationRequested();
if (!await _microsoftStoreService.TryInstallPackageAsync(WslKernelPackageStoreId))
await _microsoftStoreService.InstallPackageAsync(WslKernelPackageStoreId, _packageInstallTimeOut);

if (!_packageHelper.IsPackageInstalled(WSLPackageFamilyName))
{
throw new InvalidDataException("Failed to install the Wsl kernel package");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Store.Preview.InstallControl;
using Windows.Foundation;
Expand Down Expand Up @@ -37,4 +38,11 @@ public interface IMicrosoftStoreService
/// <param name="packageId">Target package id</param>
/// <returns>True if package was installed, false otherwise</returns>
public Task<bool> TryInstallPackageAsync(string packageId);

/// <summary>
/// Install a package from the store.
/// </summary>
/// <param name="packageId">Target package id</param>
/// <param name="timeout">Amount of time the operation should wait before cancelling the task.</param>
public Task InstallPackageAsync(string packageId, TimeSpan timeout);
}
31 changes: 18 additions & 13 deletions services/DevHome.Services.Core/Services/MicrosoftStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,8 @@ public async Task<bool> TryInstallPackageAsync(string packageId)
{
try
{
var installTask = InstallPackageAsync(packageId);

// Wait for a maximum of StoreInstallTimeout (60 seconds).
var completedTask = await Task.WhenAny(installTask, Task.Delay(_storeInstallTimeout));

if (completedTask.Exception != null)
{
throw completedTask.Exception;
}

if (completedTask != installTask)
{
throw new TimeoutException("Store Install task did not finish in time.");
}
await InstallPackageAsync(packageId, _storeInstallTimeout);

return true;
}
Expand All @@ -105,6 +93,23 @@ public async Task<bool> TryInstallPackageAsync(string packageId)
return false;
}

public async Task InstallPackageAsync(string packageId, TimeSpan timeout)
{
var installTask = InstallPackageAsync(packageId);

var completedTask = await Task.WhenAny(installTask, Task.Delay(timeout));

if (completedTask.Exception != null)
{
throw completedTask.Exception;
}

if (completedTask != installTask)
{
throw new TimeoutException("Store Install task did not finish in time.");
}
}

private async Task InstallPackageAsync(string packageId)
{
await Task.Run(() =>
Expand Down

0 comments on commit 8cc7492

Please sign in to comment.