Skip to content

Commit 19c3a52

Browse files
authored
refactor: Unify OSS update path through OSSUpdateStrategy (#414)
* refactor: unify OSS update path through OSSUpdateStrategy Move client-side OSS logic (version config download, upgrade check, process launch) from LaunchOssAsync() into OSSUpdateStrategy. The strategy now handles both client and upgrade OSS roles internally. - OSSUpdateStrategy.ExecuteAsync(): detects role via GlobalConfigInfoOSS env var, dispatches to ExecuteClientAsync() or ExecuteUpgradeAsync() accordingly - GeneralUpdateBootstrap: AppType.OSS now goes through LaunchWithStrategy(new OSSUpdateStrategy()) — consistent with Client and Upgrade paths - Deleted LaunchOssAsync(), DownloadOssFile(), IsOssUpgrade() from GeneralUpdateBootstrap Closes #409 * refactor: simplify OSS to single-process flow, remove upgrade/client split OSS mode no longer needs a separate upgrade process. The strategy downloads version config, downloads packages from OSS, decompresses, starts the main app, and exits — all in one process. - Removed client/upgrade env-var-based dispatch from OSSUpdateStrategy - Single ExecuteAsync() flow: download config -> download packages -> decompress -> start app -> exit - Removed GlobalConfigInfoOSS env var bridging (no longer needed) Related #409 * refactor: OSS client/upgrade split with upgrade process OSS mode uses a separate upgrade process to download packages and decompress, but does NOT check UpgradeClientVersion (the upgrade process itself never needs upgrading — differs from standard flow). Client side (main app): Download version config → check update → start upgrade process → exit Upgrade side (GeneralUpdate.Upgrade.exe): Read version config → download OSS packages → decompress → start main app Related #409 * refactor: add AppType.OSSClient/OSSUpgrade, remove env-var dispatch Replace AppType.OSS with explicit OSSClient/OSSUpgrade to match the Client/Upgrade pattern. OSSUpdateStrategy now accepts AppType via constructor instead of detecting role via GlobalConfigInfoOSS env var. - AppType enum: OSS = 3 -> OSSClient = 3, OSSUpgrade = 4 - Bootstrap dispatch: OSSClient/OSSUpgrade through LaunchWithStrategy - OSSUpdateStrategy: role dispatched by constructor AppType parameter - Updated tests for new enum values Closes #409 * fix: remove dead GlobalConfigInfoOSS write from OSS client path The upgrade side (ExecuteUpgradeAsync) reads version config directly from the local JSON file — it never reads GlobalConfigInfoOSS env var. Writing it in ExecuteClientAsync was dead code. Removed ossConfig + Environments.SetEnvironmentVariable block. Related #409 * fix: guard against empty UpdateUrl in OSS client download DownloadVersionConfig calls HttpClient which throws InvalidOperationException when UpdateUrl is empty/null. Skip the download when UpdateUrl is not configured, then check for local version config file existence as before. Fixes OssIntegrationTests.OSSUpdateStrategy_RequiresConfig test. Related #409
1 parent 9a1f15e commit 19c3a52

5 files changed

Lines changed: 151 additions & 160 deletions

File tree

src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace GeneralUpdate.Core;
2727
/// <list type="bullet">
2828
/// <item><see cref="AppType.Client"/> — validate versions, download, start upgrade process</item>
2929
/// <item><see cref="AppType.Upgrade"/> — receive ProcessInfo, apply updates, start main app</item>
30-
/// <item><see cref="AppType.OSS"/> — OSS-based cloud storage update</item>
30+
/// <item><see cref="AppType.OSSClient"/> — OSS client: download version config, start upgrade process</item>
31+
/// <item><see cref="AppType.OSSUpgrade"/> — OSS upgrade: download packages from cloud, start main app</item>
3132
/// </list>
3233
/// </summary>
3334
/// <remarks>
@@ -71,7 +72,8 @@ public override async Task<GeneralUpdateBootstrap> LaunchAsync()
7172
{
7273
AppType.Client => await LaunchWithStrategy(new ClientUpdateStrategy()),
7374
AppType.Upgrade => await LaunchWithStrategy(new UpgradeUpdateStrategy()),
74-
AppType.OSS => await LaunchOssAsync(),
75+
AppType.OSSClient => await LaunchWithStrategy(new OSSUpdateStrategy(AppType.OSSClient)),
76+
AppType.OSSUpgrade => await LaunchWithStrategy(new OSSUpdateStrategy(AppType.OSSUpgrade)),
7577
_ => await LaunchWithStrategy(new ClientUpdateStrategy())
7678
};
7779
}
@@ -153,73 +155,6 @@ private async Task<GeneralUpdateBootstrap> LaunchWithStrategy(IStrategy roleStra
153155
return this;
154156
}
155157

156-
/// <summary>OSS workflow: download packages from cloud storage, apply updates.</summary>
157-
private async Task<GeneralUpdateBootstrap> LaunchOssAsync()
158-
{
159-
try
160-
{
161-
GeneralTracer.Debug("LaunchOssAsync start.");
162-
163-
var json = Environments.GetEnvironmentVariable("GlobalConfigInfoOSS");
164-
if (!string.IsNullOrWhiteSpace(json))
165-
{
166-
var strategy = new OSSUpdateStrategy();
167-
strategy.Create(_configInfo);
168-
await strategy.ExecuteAsync();
169-
return this;
170-
}
171-
172-
// Client-side OSS
173-
var basePath = AppDomain.CurrentDomain.BaseDirectory;
174-
var versionFileName = $"{_configInfo.MainAppName ?? _configInfo.AppName}_versions.json";
175-
var versionsFilePath = Path.Combine(basePath, versionFileName);
176-
177-
DownloadOssFile(_configInfo.UpdateUrl, versionsFilePath);
178-
if (!File.Exists(versionsFilePath)) return this;
179-
180-
var versions = StorageManager.GetJson<List<VersionOSS>>(versionsFilePath,
181-
VersionOSSJsonContext.Default.ListVersionOSS);
182-
if (versions == null || versions.Count == 0) return this;
183-
184-
versions = versions.OrderByDescending(x => x.PubTime).ToList();
185-
var newVersion = versions.First();
186-
187-
if (!IsOssUpgrade(_configInfo.ClientVersion, newVersion.Version))
188-
{
189-
GeneralTracer.Info("LaunchOssAsync: no upgrade needed.");
190-
return this;
191-
}
192-
193-
// Use user-configured AppName, fall back to default updater name
194-
var upgradeAppName = !string.IsNullOrWhiteSpace(_configInfo.AppName) && _configInfo.AppName != "Update.exe"
195-
? _configInfo.AppName
196-
: "GeneralUpdate.Upgrade.exe";
197-
var appPath = Path.Combine(basePath, upgradeAppName);
198-
if (!File.Exists(appPath))
199-
throw new Exception($"Upgrade application not found: {upgradeAppName}");
200-
201-
var ossConfig = new GlobalConfigInfoOSS
202-
{
203-
AppName = _configInfo.MainAppName ?? _configInfo.AppName,
204-
CurrentVersion = _configInfo.ClientVersion,
205-
VersionFileName = versionFileName,
206-
Encoding = (_configInfo.Encoding?.CodePage ?? Encoding.UTF8.CodePage).ToString(),
207-
Url = _configInfo.UpdateUrl
208-
};
209-
210-
var serialized = JsonSerializer.Serialize(ossConfig,
211-
GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
212-
Environments.SetEnvironmentVariable("GlobalConfigInfoOSS", serialized);
213-
Process.Start(appPath);
214-
await GracefulExit.CurrentProcessAsync().ConfigureAwait(false);
215-
}
216-
catch (Exception ex)
217-
{
218-
GeneralTracer.Error("LaunchOssAsync failed.", ex);
219-
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
220-
}
221-
return this;
222-
}
223158

224159
// ════════════════════════════════════════════════════════════════
225160
// Configuration
@@ -373,26 +308,6 @@ private async Task CallSmallBowlHomeAsync(string processName)
373308
}
374309
}
375310

376-
private static void DownloadOssFile(string url, string path)
377-
{
378-
if (File.Exists(path))
379-
{
380-
File.SetAttributes(path, FileAttributes.Normal);
381-
File.Delete(path);
382-
}
383-
using var webClient = new System.Net.WebClient();
384-
webClient.DownloadFile(new Uri(url), path);
385-
}
386-
387-
private static bool IsOssUpgrade(string clientVersion, string serverVersion)
388-
{
389-
if (string.IsNullOrWhiteSpace(clientVersion) || string.IsNullOrWhiteSpace(serverVersion))
390-
return false;
391-
return Version.TryParse(clientVersion, out var cv)
392-
&& Version.TryParse(serverVersion, out var sv)
393-
&& cv < sv;
394-
}
395-
396311
// ════════════════════════════════════════════════════════════════
397312
// Strategy & Events
398313
// ════════════════════════════════════════════════════════════════

src/c#/GeneralUpdate.Core/Configuration/AppType.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public enum AppType
1111
/// <summary>Upgrade application — applies downloaded update packages, starts main app.</summary>
1212
Upgrade = 2,
1313

14-
/// <summary>OSS (Object Storage Service) update mode — downloads from cloud storage.</summary>
15-
OSS = 3
14+
/// <summary>OSS client mode — checks version config, starts upgrade process.</summary>
15+
OSSClient = 3,
16+
17+
/// <summary>OSS upgrade mode — downloads packages from OSS, deploys to client.</summary>
18+
OSSUpgrade = 4
1619
}

0 commit comments

Comments
 (0)