1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-10-19 05:42:50 +02:00

Compare commits

..

1 Commits

Author SHA1 Message Date
2875f5943b
Refactor integrated server management 2023-12-27 11:44:28 +01:00
6 changed files with 34 additions and 42 deletions

View File

@ -21,7 +21,6 @@
<PackageReference Include="Avalonia.Desktop" Version="11.0.6" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.6" Condition=" '$(Configuration)' == 'Debug' " />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.6" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.6" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.6" />
</ItemGroup>

View File

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using DHT.Desktop.Common;
using DHT.Desktop.Main.Controls;
using DHT.Server;
@ -11,6 +11,7 @@ using DHT.Server.Data;
using DHT.Server.Data.Aggregations;
using DHT.Server.Data.Filters;
using DHT.Server.Database;
using DHT.Server.Download;
using DHT.Utils.Models;
using DHT.Utils.Tasks;
@ -59,8 +60,7 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
private readonly State state;
private readonly AsyncValueComputer<DownloadStatusStatistics>.Single downloadStatisticsComputer;
private IDisposable? finishedItemsSubscription;
private int doneItemsCount;
private int initialFinishedCount;
private int? totalItemsToDownloadCount;
@ -69,18 +69,19 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
public AttachmentsPageModel(State state) {
this.state = state;
FilterModel = new AttachmentFilterPanelModel(state);
downloadStatisticsComputer = AsyncValueComputer<DownloadStatusStatistics>.WithResultProcessor(UpdateStatistics).WithOutdatedResults().BuildWithComputer(state.Db.GetDownloadStatusStatistics);
downloadStatisticsComputer.Recompute();
state.Db.Statistics.PropertyChanged += OnDbStatisticsChanged;
state.Downloader.OnItemFinished += DownloaderOnOnItemFinished;
}
public void Dispose() {
state.Db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
finishedItemsSubscription?.Dispose();
state.Downloader.OnItemFinished -= DownloaderOnOnItemFinished;
FilterModel.Dispose();
}
@ -138,22 +139,19 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
OnPropertyChanged(nameof(DownloadProgress));
}
private void OnItemsFinished(int finishedItemCount) {
doneItemsCount += finishedItemCount;
UpdateDownloadMessage();
private void DownloaderOnOnItemFinished(object? sender, DownloadItem e) {
Interlocked.Increment(ref doneItemsCount);
Dispatcher.UIThread.Invoke(UpdateDownloadMessage);
downloadStatisticsComputer.Recompute();
}
public async Task OnClickToggleDownload() {
IsToggleDownloadButtonEnabled = false;
if (IsDownloading) {
IsToggleDownloadButtonEnabled = false;
await state.Downloader.Stop();
finishedItemsSubscription?.Dispose();
finishedItemsSubscription = null;
downloadStatisticsComputer.Recompute();
IsToggleDownloadButtonEnabled = true;
state.Db.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching);
@ -163,22 +161,13 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
UpdateDownloadMessage();
}
else {
var finishedItems = await state.Downloader.Start();
initialFinishedCount = statisticsDownloaded.Items + statisticsFailed.Items;
finishedItemsSubscription = finishedItems.Select(static _ => true)
.Buffer(TimeSpan.FromMilliseconds(100))
.Select(static items => items.Count)
.Where(static items => items > 0)
.ObserveOn(AvaloniaScheduler.Instance)
.Subscribe(OnItemsFinished);
await state.Downloader.Start();
EnqueueDownloadItems();
}
OnPropertyChanged(nameof(ToggleDownloadButtonText));
OnPropertyChanged(nameof(IsDownloading));
IsToggleDownloadButtonEnabled = true;
}
public void OnClickRetryFailedDownloads() {

View File

@ -9,6 +9,8 @@ public sealed class Downloader {
private DownloaderTask? current;
public bool IsDownloading => current != null;
public event EventHandler<DownloadItem>? OnItemFinished;
private readonly IDatabaseFile db;
private readonly SemaphoreSlim semaphore = new (1, 1);
@ -16,11 +18,13 @@ public sealed class Downloader {
this.db = db;
}
public async Task<IObservable<DownloadItem>> Start() {
public async Task Start() {
await semaphore.WaitAsync();
try {
current ??= new DownloaderTask(db);
return current.FinishedItems;
if (current == null) {
current = new DownloaderTask(db);
current.OnItemFinished += DelegateOnItemFinished;
}
} finally {
semaphore.Release();
}
@ -30,11 +34,16 @@ public sealed class Downloader {
await semaphore.WaitAsync();
try {
if (current != null) {
await current.DisposeAsync();
await current.Stop();
current.OnItemFinished -= DelegateOnItemFinished;
current = null;
}
} finally {
semaphore.Release();
}
}
private void DelegateOnItemFinished(object? sender, DownloadItem e) {
OnItemFinished?.Invoke(this, e);
}
}

View File

@ -1,23 +1,25 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using DHT.Server.Database;
using DHT.Utils.Logging;
using DHT.Utils.Models;
using DHT.Utils.Tasks;
namespace DHT.Server.Download;
sealed class DownloaderTask : IAsyncDisposable {
sealed class DownloaderTask : BaseModel {
private static readonly Log Log = Log.ForType<DownloaderTask>();
private const int DownloadTasks = 4;
private const int QueueSize = 25;
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
internal event EventHandler<DownloadItem>? OnItemFinished;
private readonly Channel<DownloadItem> downloadQueue = Channel.CreateBounded<DownloadItem>(new BoundedChannelOptions(QueueSize) {
SingleReader = false,
SingleWriter = true,
@ -29,16 +31,12 @@ sealed class DownloaderTask : IAsyncDisposable {
private readonly CancellationToken cancellationToken;
private readonly IDatabaseFile db;
private readonly Subject<DownloadItem> finishedItemPublisher = new ();
private readonly Task queueWriterTask;
private readonly Task[] downloadTasks;
public IObservable<DownloadItem> FinishedItems => finishedItemPublisher;
internal DownloaderTask(IDatabaseFile db) {
this.db = db;
this.cancellationToken = cancellationTokenSource.Token;
this.db = db;
this.queueWriterTask = Task.Run(RunQueueWriterTask);
this.downloadTasks = Enumerable.Range(1, DownloadTasks).Select(taskIndex => Task.Run(() => RunDownloadTask(taskIndex))).ToArray();
}
@ -81,7 +79,7 @@ sealed class DownloaderTask : IAsyncDisposable {
log.Error(e);
} finally {
try {
finishedItemPublisher.OnNext(item);
OnItemFinished?.Invoke(this, item);
} catch (Exception e) {
log.Error("Caught exception in event handler: " + e);
}
@ -89,7 +87,7 @@ sealed class DownloaderTask : IAsyncDisposable {
}
}
public async ValueTask DisposeAsync() {
internal async Task Stop() {
try {
await cancellationTokenSource.CancelAsync();
} catch (Exception) {
@ -104,7 +102,6 @@ sealed class DownloaderTask : IAsyncDisposable {
await Task.WhenAll(downloadTasks).WaitIgnoringCancellation();
} finally {
cancellationTokenSource.Dispose();
finishedItemPublisher.OnCompleted();
}
}
}

View File

@ -12,7 +12,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -21,7 +21,6 @@ public sealed class State : IAsyncDisposable {
public async ValueTask DisposeAsync() {
await Downloader.Stop();
await Server.Stop();
Db.Dispose();
}
}