-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathUpgradeUpdateStrategy.cs
More file actions
203 lines (174 loc) · 8.29 KB
/
Copy pathUpgradeUpdateStrategy.cs
File metadata and controls
203 lines (174 loc) · 8.29 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using GeneralUpdate.Core.Differential;
using GeneralUpdate.Differential.Abstractions;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.Event;
using GeneralUpdate.Core.Pipeline;
namespace GeneralUpdate.Core.Strategy;
/// <summary>
/// Upgrade-side update strategy. Receives process info from the client side,
/// applies updates via the pipeline, and starts the main application.
/// </summary>
/// <remarks>
/// This is the AppType.Upgrade role strategy. It composes an OS-specific
/// strategy for platform operations (Windows/Linux/Mac).
///
/// <b>Design:</b> Upgrade does NOT validate versions or download packages.
/// The client has already validated versions, downloaded all packages, and
/// passed the results via ProcessInfo. Upgrade only applies updates and
/// starts the main application — zero network.
/// </remarks>
public class UpgradeUpdateStrategy : IStrategy
{
private GlobalConfigInfo? _configInfo;
private IStrategy? _osStrategy;
/// <summary>Lifecycle hooks injected by the bootstrap.</summary>
public Hooks.IUpdateHooks Hooks { get; set; } = new Hooks.NoOpUpdateHooks();
/// <summary>Update status reporter injected by the bootstrap.</summary>
public Download.Reporting.IUpdateReporter Reporter { get; set; } = new Download.Reporting.NoOpUpdateReporter();
public void Create(GlobalConfigInfo parameter)
{
_configInfo = parameter ?? throw new ArgumentNullException(nameof(parameter));
_osStrategy = ResolveOsStrategy();
if (_pendingDirtyStrategy != null && _osStrategy is AbstractStrategy abs)
abs.DirtyStrategy = _pendingDirtyStrategy;
}
public async Task ExecuteAsync()
{
if (_configInfo == null) throw new InvalidOperationException("UpgradeUpdateStrategy not configured.");
var ctx = BuildUpdateContext();
try
{
GeneralTracer.Debug("UpgradeUpdateStrategy.ExecuteAsync start.");
// Hooks: allow cancellation before applying updates
if (!await SafeOnBeforeUpdateAsync(ctx).ConfigureAwait(false))
{
GeneralTracer.Info("UpgradeUpdateStrategy: update cancelled by OnBeforeUpdateAsync hook.");
return;
}
_osStrategy!.Create(_configInfo);
// Apply MainApp updates — Client already applied Upgrade packages, IPC only has MainApp versions
if (_configInfo.UpdateVersions?.Count > 0)
{
GeneralTracer.Info("UpgradeUpdateStrategy: applying " + _configInfo.UpdateVersions.Count + " MainApp update(s).");
await _osStrategy.ExecuteAsync();
}
else
{
GeneralTracer.Info("UpgradeUpdateStrategy: no updates to apply, starting application directly.");
}
// Hooks: after all updates applied
await SafeOnAfterUpdateAsync(ctx).ConfigureAwait(false);
// Report: update applied successfully
await SafeReportUpdateAppliedAsync(ctx).ConfigureAwait(false);
// Hooks: before starting main app (e.g. chmod +x on Linux/macOS)
await SafeOnBeforeStartAppAsync(ctx).ConfigureAwait(false);
await _osStrategy.StartAppAsync();
}
catch (Exception ex)
{
await SafeOnUpdateErrorAsync(ctx, ex).ConfigureAwait(false);
await SafeReportUpdateFailedAsync(ctx, ex).ConfigureAwait(false);
GeneralTracer.Error("UpgradeUpdateStrategy.ExecuteAsync failed.", ex);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
}
}
private IDirtyStrategy? _pendingDirtyStrategy;
/// <summary>Sets the directory-level dirty strategy on the underlying OS-level strategy for differential patch updates.
/// Safe to call before or after Create(). If called before, the strategy is cached and applied when Create() resolves _osStrategy.</summary>
public void SetDirtyStrategy(IDirtyStrategy? dirtyStrategy)
{
if (_osStrategy is AbstractStrategy abs)
abs.DirtyStrategy = dirtyStrategy;
else
_pendingDirtyStrategy = dirtyStrategy;
}
/// <summary>Sets the file-level binary differ on the underlying OS-level strategy.</summary>
public void SetBinaryDiffer(IBinaryDiffer? binaryDiffer)
{
if (_osStrategy is AbstractStrategy abs)
abs.BinaryDiffer = binaryDiffer;
}
/// <summary>Sets the DiffPipeline on the underlying OS-level strategy for parallel patch application.</summary>
public void SetDiffPipeline(DiffPipeline? diffPipeline)
{
if (_osStrategy is AbstractStrategy abs)
abs.DiffPipeline = diffPipeline;
}
public async Task StartAppAsync()
{
if (_osStrategy != null)
await _osStrategy.StartAppAsync();
}
#region Helpers
private static IStrategy ResolveOsStrategy()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return new WindowsStrategy();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return new LinuxStrategy();
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return new MacStrategy();
throw new PlatformNotSupportedException("The current operating system is not supported!");
}
// ════════════════════════════════════════════════════════════════
// Hooks & Reporter safe wrappers
// ════════════════════════════════════════════════════════════════
private Hooks.UpdateContext BuildUpdateContext()
{
return new Hooks.UpdateContext(
_configInfo?.AppName ?? "unknown",
_configInfo?.InstallPath ?? AppDomain.CurrentDomain.BaseDirectory,
_configInfo?.ClientVersion ?? "0.0.0",
_configInfo?.LastVersion,
AppType.Upgrade
);
}
private async Task<bool> SafeOnBeforeUpdateAsync(Hooks.UpdateContext ctx)
{
try { return await Hooks.OnBeforeUpdateAsync(ctx).ConfigureAwait(false); }
catch (Exception ex) { GeneralTracer.Warn($"OnBeforeUpdateAsync hook failed: {ex.Message}"); return true; }
}
private async Task SafeOnAfterUpdateAsync(Hooks.UpdateContext ctx)
{
try { await Hooks.OnAfterUpdateAsync(ctx).ConfigureAwait(false); }
catch (Exception ex) { GeneralTracer.Warn($"OnAfterUpdateAsync hook failed: {ex.Message}"); }
}
private async Task SafeOnBeforeStartAppAsync(Hooks.UpdateContext ctx)
{
try { await Hooks.OnBeforeStartAppAsync(ctx).ConfigureAwait(false); }
catch (Exception ex) { GeneralTracer.Warn($"OnBeforeStartAppAsync hook failed: {ex.Message}"); }
}
private async Task SafeOnUpdateErrorAsync(Hooks.UpdateContext ctx, Exception error)
{
try { await Hooks.OnUpdateErrorAsync(ctx, error).ConfigureAwait(false); }
catch (Exception ex) { GeneralTracer.Warn($"OnUpdateErrorAsync hook failed: {ex.Message}"); }
}
private async Task SafeReportUpdateAppliedAsync(Hooks.UpdateContext ctx)
{
try
{
await Reporter.ReportAsync(new Download.Reporting.UpdateReport(
ctx.AppName, ctx.CurrentVersion, ctx.TargetVersion,
Download.Reporting.UpdateEvent.UpdateApplied, ctx.AppType, DateTimeOffset.UtcNow
)).ConfigureAwait(false);
}
catch (Exception ex) { GeneralTracer.Warn($"Report UpdateApplied failed: {ex.Message}"); }
}
private async Task SafeReportUpdateFailedAsync(Hooks.UpdateContext ctx, Exception error)
{
try
{
await Reporter.ReportAsync(new Download.Reporting.UpdateReport(
ctx.AppName, ctx.CurrentVersion, ctx.TargetVersion,
Download.Reporting.UpdateEvent.UpdateFailed, ctx.AppType, DateTimeOffset.UtcNow,
ErrorMessage: error.Message
)).ConfigureAwait(false);
}
catch (Exception ex) { GeneralTracer.Warn($"Report UpdateFailed failed: {ex.Message}"); }
}
#endregion
}