-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathListInstancesViewModel.cs
More file actions
165 lines (138 loc) · 5.85 KB
/
Copy pathListInstancesViewModel.cs
File metadata and controls
165 lines (138 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace ServiceControl.Config.UI.ListInstances
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Caliburn.Micro;
using DynamicData;
using Events;
using Framework.Rx;
using InstanceDetails;
using NuGet.Versioning;
using PropertyChanging;
using ServiceControl.Config.Extensions;
using ServiceControlInstaller.Engine.Instances;
class ListInstancesViewModel : RxScreen, IHandle<RefreshInstances>, IHandle<ResetInstances>, IHandle<LicenseUpdated>
{
public ListInstancesViewModel(Func<BaseService, InstanceDetailsViewModel> instanceDetailsFunc)
{
this.instanceDetailsFunc = instanceDetailsFunc;
DisplayName = "DEPLOYED INSTANCES";
Instances = [];
AddAndRemoveInstances();
}
public BindableCollection<InstanceDetailsViewModel> OrderedInstances => [.. Instances.OrderBy(x => x.Name)];
public bool HasConfigurationErrors
{
get
{
var hasErrors = Instances.Any(i => !string.IsNullOrEmpty(i.ConfigurationLoadError));
return hasErrors;
}
}
public string ConfigurationErrorMessage
{
get
{
var errorInstances = Instances.Where(i => !string.IsNullOrEmpty(i.ConfigurationLoadError)).ToList();
if (errorInstances.Count == 0)
{
return null;
}
if (errorInstances.Count == 1)
{
var instance = errorInstances[0];
return $"{instance.Name} instance cannot be loaded due to XML configuration error.";
}
var names = string.Join(", ", errorInstances.Select(i => i.Name));
return $"Multiple instances ({names}) cannot be loaded due to XML configuration errors.";
}
}
public IEnumerable<InstanceDetailsViewModel> InstancesWithConfigErrors => Instances.Where(i => !string.IsNullOrEmpty(i.ConfigurationLoadError));
[AlsoNotifyFor(nameof(OrderedInstances), nameof(HasConfigurationErrors), nameof(ConfigurationErrorMessage), nameof(InstancesWithConfigErrors))]
IList<InstanceDetailsViewModel> Instances { get; }
public Task HandleAsync(LicenseUpdated licenseUpdatedEvent, CancellationToken cancellationToken)
{
// on license change inform each instance to refresh the license (1.23.0 and below don't support this)
foreach (var instance in Instances)
{
if (instance.Version <= new SemanticVersion(1, 23, 0))
{
continue;
}
if (!instance.HasBrowsableUrl)
{
continue;
}
_ = Task.Run(async () =>
{
try
{
using var http = new HttpClient();
http.Timeout = TimeSpan.FromSeconds(2);
await http.GetAsync($"{instance.BrowsableUrl}license?refresh=true");
}
catch
{
// Ignored
}
}, cancellationToken);
}
return Task.CompletedTask;
}
/// <summary>
/// Should be only subscriber for RefreshInstances so that add/removes can happen in the list
/// before the PostRefreshInstances handlers do all their rebinding. That way, deleting an instance
/// in PowerShell won't cause an error from a deleted instance viewmodel trying to refresh itself.
/// </summary>
public async Task HandleAsync(RefreshInstances message, CancellationToken cancellationToken)
{
AddAndRemoveInstances();
await EventAggregator.PublishOnUIThreadAsync(new PostRefreshInstances(), cancellationToken);
}
public async Task HandleAsync(ResetInstances message, CancellationToken cancellationToken)
{
foreach (var instance in Instances)
{
await instance.TryCloseAsync(true);
}
Instances.Clear();
foreach (var item in InstanceFinder.AllInstances().OrderBy(i => i.Name))
{
Instances.Add(instanceDetailsFunc(item));
}
NotifyOfPropertyChange(nameof(Instances));
}
async void AddAndRemoveInstances()
{
var toRemove = Instances.Where(instance => !instance.Exists());
foreach (var instance in toRemove)
{
await instance.TryCloseAsync();
}
Instances.RemoveMany(toRemove);
// Get fresh instances from disk (with updated configurations)
var allFreshInstances = InstanceFinder.AllInstances();
// Update existing instances with fresh configuration data
foreach (var existingInstance in Instances)
{
var freshInstance = allFreshInstances.FirstOrDefault(i => i.Name == existingInstance.Name);
if (freshInstance != null)
{
existingInstance.UpdateServiceInstance(freshInstance);
}
}
var missingInstances = allFreshInstances.Where(i => !Instances.Any(existingInstance => existingInstance.Name == i.Name));
foreach (var item in missingInstances)
{
Instances.Add(instanceDetailsFunc(item));
}
Validations.RefreshInstances();
NotifyOfPropertyChange(nameof(Instances));
}
readonly Func<BaseService, InstanceDetailsViewModel> instanceDetailsFunc;
}
}