-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInstallQueueWorkObserver.cs
More file actions
90 lines (75 loc) · 2.55 KB
/
InstallQueueWorkObserver.cs
File metadata and controls
90 lines (75 loc) · 2.55 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
using System;
using System.Collections.Generic;
using TidyWindow.Core.Install;
namespace TidyWindow.App.Services;
/// <summary>
/// Mirrors <see cref="InstallQueue"/> activity into the automation work tracker so PulseGuard can reason about active installs.
/// </summary>
public sealed class InstallQueueWorkObserver : IDisposable
{
private readonly InstallQueue _installQueue;
private readonly IAutomationWorkTracker _workTracker;
private readonly Dictionary<Guid, Guid> _activeTokens = new();
private bool _disposed;
public InstallQueueWorkObserver(InstallQueue installQueue, IAutomationWorkTracker workTracker)
{
_installQueue = installQueue ?? throw new ArgumentNullException(nameof(installQueue));
_workTracker = workTracker ?? throw new ArgumentNullException(nameof(workTracker));
foreach (var snapshot in _installQueue.GetSnapshot())
{
TrackSnapshot(snapshot);
}
_installQueue.OperationChanged += OnOperationChanged;
}
private void OnOperationChanged(object? sender, InstallQueueChangedEventArgs e)
{
if (_disposed || e?.Snapshot is null)
{
return;
}
TrackSnapshot(e.Snapshot);
}
private void TrackSnapshot(InstallQueueOperationSnapshot snapshot)
{
lock (_activeTokens)
{
if (snapshot.IsActive)
{
if (_activeTokens.ContainsKey(snapshot.Id))
{
return;
}
var description = snapshot.Status switch
{
InstallQueueStatus.Running => $"Installing {snapshot.Package.Name}",
_ => $"Queued install for {snapshot.Package.Name}"
};
var token = _workTracker.BeginWork(AutomationWorkType.Install, description);
_activeTokens[snapshot.Id] = token;
return;
}
if (_activeTokens.TryGetValue(snapshot.Id, out var existingToken))
{
_workTracker.CompleteWork(existingToken);
_activeTokens.Remove(snapshot.Id);
}
}
}
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_installQueue.OperationChanged -= OnOperationChanged;
lock (_activeTokens)
{
foreach (var token in _activeTokens.Values)
{
_workTracker.CompleteWork(token);
}
_activeTokens.Clear();
}
}
}