Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public GeneralUpdateBootstrap()
public override async Task<GeneralUpdateBootstrap> LaunchAsync()
{
int appType = GetOption(UpdateOptions.AppType);

// Silent mode: start background poll and return immediately
if (appType == AppType.ClientApp && GetOption(UpdateOptions.Silent))
{
await LaunchSilentAsync().ConfigureAwait(false);
return this;
}

return appType switch
{
AppType.ClientApp => await LaunchWithStrategy(new ClientUpdateStrategy()),
Expand Down Expand Up @@ -250,6 +258,35 @@ private void ApplyRuntimeOptions()
_configInfo.DownloadTimeOut = GetOption(UpdateOptions.DownloadTimeout) ?? 60;
}

/// <summary>
/// Silent update mode — starts a background poll loop and returns immediately.
/// The orchestrator checks for updates periodically and prepares them.
/// When the host process exits, the prepared update is applied.
/// </summary>
private async Task LaunchSilentAsync()
{
GeneralTracer.Info("GeneralUpdateBootstrap: starting silent update mode.");

var pollMinutes = GetOption(UpdateOptions.SilentPollIntervalMinutes);
var autoInstall = GetOption(UpdateOptions.SilentAutoInstall);

var silentOptions = new Silent.SilentOptions
{
PollInterval = TimeSpan.FromMinutes(pollMinutes),
AutoInstall = autoInstall
};

var hooks = ResolveExtension<Hooks.IUpdateHooks>() ?? new Hooks.NoOpUpdateHooks();
var reporter = ResolveExtension<Download.Reporting.IUpdateReporter>() ?? new Download.Reporting.NoOpUpdateReporter();

var orchestrator = new Silent.SilentPollOrchestrator(_configInfo, silentOptions)
.WithHooks(hooks)
.WithReporter(reporter);

await orchestrator.StartAsync().ConfigureAwait(false);
GeneralTracer.Info("GeneralUpdateBootstrap: silent update mode started, returning to caller.");
}

private void InitBlackList()
{
BlackListManager.Instance.AddBlackFiles(_configInfo.BlackFiles);
Expand Down
1 change: 1 addition & 0 deletions src/c#/GeneralUpdate.Core/Configuration/UpdateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static class UpdateOptions
public static UpdateOption<string?> UpgradeClientVersion { get; } = UpdateOption.ValueOf<string?>("UPGRADECLIENTVERSION", null);
public static UpdateOption<int?> Platform { get; } = UpdateOption.ValueOf<int?>("PLATFORM", null);
public static UpdateOption<bool> SilentAutoInstall { get; } = UpdateOption.ValueOf<bool>("SILENTAUTOINSTALL", false);
public static UpdateOption<int> SilentPollIntervalMinutes { get; } = UpdateOption.ValueOf<int>("SILENTPOLLINTERVALMINUTES", 60);
public static UpdateOption<int> MaxConcurrency { get; } = UpdateOption.ValueOf<int>("MAXCONCURRENCY", 3);
public static UpdateOption<bool> EnableResume { get; } = UpdateOption.ValueOf<bool>("ENABLERESUME", true);
public static UpdateOption<int> RetryCount { get; } = UpdateOption.ValueOf<int>("RETRYCOUNT", 3);
Expand Down
20 changes: 20 additions & 0 deletions src/c#/GeneralUpdate.Core/Download/DownloadPlanBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GeneralUpdate.Core.Download.Abstractions;
using GeneralUpdate.Core.Download.Models;

namespace GeneralUpdate.Core.Download;
Expand Down Expand Up @@ -86,6 +87,25 @@ private static bool IsCompatible(string? minClientVersion, string currentVersion
return cur >= min;
}

/// <summary>Map a PacketDTO to a DownloadAsset. Public for use by download sources.</summary>
public static DownloadAsset MapToAsset(Abstractions.PacketDTO p)
{
return new DownloadAsset(
Name: p.Name ?? p.Version ?? "unknown",
Url: p.Url ?? string.Empty,
Size: p.Size ?? 0,
SHA256: p.Hash,
Version: p.Version ?? "0.0.0",
IsCrossVersion: p.IsCrossVersion == true,
FromVersion: p.FromVersion,
MinClientVersion: p.MinClientVersion,
SourceArchiveHash: p.SourceArchiveHash,
TargetArchiveHash: p.TargetArchiveHash,
IsForcibly: p.IsForcibly == true,
IsFreeze: p.IsFreeze == true
);
}

/// <summary>Parse a version string, returning null on failure.</summary>
private static Version? ParseVersion(string? version)
{
Expand Down
84 changes: 84 additions & 0 deletions src/c#/GeneralUpdate.Core/Download/Sources/HubDownloadSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.Core.Download.Abstractions;
using GeneralUpdate.Core.Download.Models;
using GeneralUpdate.Core.Hubs;
using GeneralUpdate.Core.JsonContext;

namespace GeneralUpdate.Core.Download.Sources;

/// <summary>
/// SignalR Hub download source — receives update push notifications
/// and converts them to DownloadAssets for the orchestrator.
/// </summary>
public class HubDownloadSource : IDownloadSource, IDisposable
{
private readonly string _hubUrl;
private readonly string? _token;
private readonly string? _appKey;
private readonly ConcurrentBag<DownloadAsset> _assets = new();
private readonly TaskCompletionSource<bool> _initializedTcs = new();
private UpgradeHubService? _hub;

public HubDownloadSource(string hubUrl, string? token = null, string? appKey = null)
{
_hubUrl = hubUrl;
_token = token;
_appKey = appKey;
}

/// <summary>Start listening to the SignalR hub.</summary>
public async Task StartAsync()
{
try
{
_hub = new UpgradeHubService(_hubUrl, _token, _appKey);
_hub.AddListenerReceive(OnReceiveMessage);
await _hub.StartAsync().ConfigureAwait(false);
_initializedTcs.TrySetResult(true);
}
catch (Exception ex)
{
_initializedTcs.TrySetException(ex);
}
}

private void OnReceiveMessage(string json)
{
try
{
var packet = System.Text.Json.JsonSerializer.Deserialize<PacketDTO>(json);
if (packet != null)
{
var asset = DownloadPlanBuilder.MapToAsset(packet);
_assets.Add(asset);
}
}
catch (Exception ex)
{
GeneralTracer.Warn($"HubDownloadSource: failed to parse message: {ex.Message}");
}
}

/// <summary>Get accumulated download assets from hub pushes.</summary>
public async Task<IReadOnlyList<DownloadAsset>> ListAsync(CancellationToken token = default)
{
// Wait for hub initialization
await _initializedTcs.Task.ConfigureAwait(false);

// Wait a brief moment for any pending messages to arrive
try { await Task.Delay(100, token).ConfigureAwait(false); }
catch (OperationCanceledException) { }

return _assets.ToList();
}

public void Dispose()
{
_hub?.DisposeAsync().GetAwaiter().GetResult();
}
}
Loading
Loading