|
1 | 1 | using System.Net.Http.Json; |
2 | 2 | using FwLiteShared.Events; |
3 | 3 | using LexCore.Entities; |
| 4 | +using Microsoft.Extensions.Caching.Memory; |
4 | 5 | using Microsoft.Extensions.Hosting; |
5 | 6 | using Microsoft.Extensions.Logging; |
6 | 7 | using Microsoft.Extensions.Options; |
7 | 8 |
|
8 | 9 | namespace FwLiteShared.AppUpdate; |
9 | 10 |
|
| 11 | +public record AvailableUpdate(FwLiteRelease Release, bool SupportsAutoUpdate); |
| 12 | + |
10 | 13 | public class UpdateChecker( |
11 | 14 | IHttpClientFactory httpClientFactory, |
12 | 15 | ILogger<UpdateChecker> logger, |
13 | 16 | IOptions<FwLiteConfig> config, |
14 | 17 | GlobalEventBus eventBus, |
15 | | - IPlatformUpdateService platformUpdateService): BackgroundService |
| 18 | + IPlatformUpdateService platformUpdateService, |
| 19 | + IMemoryCache cache) : BackgroundService |
16 | 20 | { |
| 21 | + private const string CacheKey = "ManualUpdateCheck"; |
| 22 | + private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(2); |
| 23 | + |
17 | 24 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
18 | 25 | { |
19 | 26 | await TryUpdate(); |
20 | 27 | } |
21 | 28 |
|
22 | | - public async Task TryUpdate(bool forceCheck = false) |
| 29 | + public async Task<UpdateResult?> TryUpdate() |
| 30 | + { |
| 31 | + if (!ShouldCheckForUpdate()) return null; |
| 32 | + var update = await CheckForUpdate(); |
| 33 | + if (update is null) return null; |
| 34 | + return await ApplyUpdate(update.Release); |
| 35 | + } |
| 36 | + |
| 37 | + public async Task<AvailableUpdate?> CheckForUpdate() |
23 | 38 | { |
24 | | - if (!ShouldCheckForUpdate() && !forceCheck) return; |
25 | | - var response = await ShouldUpdateAsync(); |
| 39 | + return await cache.GetOrCreateAsync(CacheKey, async entry => |
| 40 | + { |
| 41 | + entry.AbsoluteExpirationRelativeToNow = CacheDuration; |
| 42 | + var response = await ShouldUpdateAsync(); |
| 43 | + platformUpdateService.LastUpdateCheck = DateTime.UtcNow; |
| 44 | + return response.Update |
| 45 | + ? new AvailableUpdate(response.Release, platformUpdateService.SupportsAutoUpdate) |
| 46 | + : null; |
| 47 | + }); |
| 48 | + } |
26 | 49 |
|
27 | | - platformUpdateService.LastUpdateCheck = DateTime.UtcNow; |
28 | | - if (!response.Update) return; |
| 50 | + public async Task<UpdateResult> ApplyUpdate(FwLiteRelease release) |
| 51 | + { |
29 | 52 | if (ShouldPromptBeforeUpdate() && |
30 | | - !await platformUpdateService.RequestPermissionToUpdate(response.Release)) |
| 53 | + !await platformUpdateService.RequestPermissionToUpdate(release)) |
31 | 54 | { |
32 | | - return; |
| 55 | + return UpdateResult.Disallowed; |
33 | 56 | } |
34 | 57 |
|
35 | | - UpdateResult updateResult = UpdateResult.ManualUpdateRequired; |
| 58 | + var updateResult = UpdateResult.ManualUpdateRequired; |
36 | 59 | if (platformUpdateService.SupportsAutoUpdate) |
37 | 60 | { |
38 | | - updateResult = await platformUpdateService.ApplyUpdate(response.Release); |
| 61 | + updateResult = await platformUpdateService.ApplyUpdate(release); |
39 | 62 | } |
40 | 63 |
|
41 | | - NotifyResult(updateResult); |
| 64 | + NotifyResult(updateResult, release); |
| 65 | + return updateResult; |
42 | 66 | } |
43 | 67 |
|
44 | | - private void NotifyResult(UpdateResult result) |
| 68 | + private void NotifyResult(UpdateResult result, FwLiteRelease release) |
45 | 69 | { |
46 | | - eventBus.PublishEvent(new AppUpdateEvent(result)); |
| 70 | + eventBus.PublishEvent(new AppUpdateEvent(result, release)); |
47 | 71 | } |
48 | 72 |
|
49 | 73 | private bool ShouldCheckForUpdate() |
|
0 commit comments