Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for worker completion when cancellation is requested #19

Merged
merged 1 commit into from
May 25, 2024
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
41 changes: 21 additions & 20 deletions WaybackDownloader/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using Spectre.Console.Cli;
using WaybackDownloader.Logging;
using WaybackDownloader.Services;
using WaybackDownloader.Spectre;
using Settings = WaybackDownloader.DefaultCommand.Settings;
Expand All @@ -21,10 +20,12 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
PrintSettings(settings);

ServiceProvider? serviceProvider = null;
Task? pageWokerRunnerTask = null;
Task? pageWorkerRunnerTask = null;
Task? uiTask = null;

using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.Token.Register(() => AnsiConsole.WriteLine("Shutting down..."));
cancellationToken.Register(() => AnsiConsole.WriteLine("Shutting down..."));
using var workerCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var uiCts = new CancellationTokenSource();

try
{
Expand Down Expand Up @@ -58,10 +59,9 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se

var downloaderService = serviceProvider.GetRequiredService<DownloaderService>();
var pageWorkerRunner = serviceProvider.GetRequiredService<PageWorkerRunner>();
var loggingMessagesAccessor = serviceProvider.GetRequiredService<CollectedLogMessages>();
var ui = serviceProvider.GetRequiredService<Ui>();

var uiTask = ui.DrawUiAsync(cts.Token);
uiTask = ui.DrawUiAsync(uiCts.Token);

var outputDir = settings.OutputDir;
outputDir.Create();
Expand All @@ -71,25 +71,17 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
return 0;
}

var downloaderTask = downloaderService.StartDownloadAsync(settings.MatchUrl, settings.MatchType, settings.ParsedFilters, settings.LimitPages, cts.Token);
pageWorkerRunner.StartTasks(outputDir.FullName, settings.RateLimit, cts.Token);
var downloaderTask = downloaderService.StartDownloadAsync(settings.MatchUrl, settings.MatchType, settings.ParsedFilters, settings.LimitPages, workerCts.Token);
pageWorkerRunner.StartTasks(outputDir.FullName, settings.RateLimit, workerCts.Token);
pageWorkerRunnerTask = pageWorkerRunner.WaitForCompletionAsync();
await downloaderTask.ConfigureAwait(false);
pageWokerRunnerTask = pageWorkerRunner.WaitForCompletionAsync();
await pageWokerRunnerTask.ConfigureAwait(false);
await pageWorkerRunnerTask.ConfigureAwait(false);

await cts.CancelAsync().ConfigureAwait(false);
await workerCts.CancelAsync().ConfigureAwait(false);
await uiCts.CancelAsync().ConfigureAwait(false);

await uiTask.ConfigureAwait(false);
await serviceProvider.DisposeAsync().ConfigureAwait(false);

foreach (var item in loggingMessagesAccessor.DrainMessages())
{
AnsiConsole.WriteLine(item.Message);
if (item.Exception is not null)
{
AnsiConsole.WriteException(item.Exception);
}
}
return 0;
}
#pragma warning disable CA1031 // Do not catch general exception types
Expand All @@ -116,6 +108,15 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
}
else
{
if (pageWorkerRunnerTask is not null)
{
await pageWorkerRunnerTask.ConfigureAwait(false);
}
await uiCts.CancelAsync().ConfigureAwait(false);
if (uiTask is not null)
{
await uiTask.ConfigureAwait(false);
}
var disposeException = await DisposeProviderAsync(serviceProvider).ConfigureAwait(false);
if (disposeException is not null)
{
Expand Down
3 changes: 2 additions & 1 deletion WaybackDownloader/Logging/CollectedLogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ internal sealed class CollectedLogMessages
public ImmutableArray<LogMessage> DrainMessages()
{
//Cannot use LINQ here - see https://github.com/dotnet/runtime/issues/101641
var array = ImmutableArray.CreateBuilder<LogMessage>(_messages.Count);
//Do not use .Count - this locks the ConcurrentQueue and is very slow
var array = ImmutableArray.CreateBuilder<LogMessage>(20);
while (_messages.TryDequeue(out var m))
{
array.Add(m);
Expand Down
6 changes: 3 additions & 3 deletions WaybackDownloader/MockDataHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ private static async Task<HttpResponseMessage> CreateCdxResponseAsync(Cancellati
return a;
}

//private static int _year = 1001;
//private static int GetYear() => Interlocked.Increment(ref _year);
private static int GetYear() => 2000;
private static int _year = 1001;
private static int GetYear() => Interlocked.Increment(ref _year);
//private static int GetYear() => 2000;

private static async Task<HttpResponseMessage> CreateHtmlResponseAsync(CancellationToken cancellationToken)
{
Expand Down
2 changes: 1 addition & 1 deletion WaybackDownloader/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"WaybackDownloader": {
"commandName": "Project",
"commandLineArgs": "archive.org \"./pages\" -m exact --limitPages 10000 -p SomeString1 -r 1 --historyLogDir=./logFileLocation --useMockHandler"
"commandLineArgs": "archive.org \"./pages\" -m exact --limitPages 10000 -p SomeString1 -r 10 --historyLogDir=./logFileLocation --useMockHandler --clearHistory"
}
}
}
50 changes: 27 additions & 23 deletions WaybackDownloader/Services/PageWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,39 @@ public async Task StartAsync(string outputDir, CancellationToken cancellationTok
{
var shouldExit = false;
cancellationToken.Register(() => shouldExit = true);

while (!shouldExit && await _reader.WaitToReadAsync(default).ConfigureAwait(false))
try
{
while (!shouldExit && !cancellationToken.IsCancellationRequested && _reader.TryRead(out var record))
while (!shouldExit && await _reader.WaitToReadAsync(default).ConfigureAwait(false))
{
if (!PathUtilities.TryGetNormalizedFilePath(record.Original, out var normalizedPath))
{
logger.UrlCouldNotBeConverted(record.Original);
Counters.FilesSkipped.Increment();
continue;
}

logger.UrlTransformed(record.Original, normalizedPath);

var writePath = Path.Combine(outputDir, normalizedPath);
var pageKey = new PageKey(record.UrlKey, normalizedPath);
var foundPage = pagesStore.TryGetDownloadedPageTimestamp(pageKey.Value, out var timestamp);
if (!foundPage || timestamp < record.Timestamp)
while (!shouldExit && !cancellationToken.IsCancellationRequested && _reader.TryRead(out var record))
{
await TryWritePageAsync(record, writePath, pageKey, foundPage, CancellationToken.None).ConfigureAwait(false);
}
else
{
logger.UrlAlreadyDownloaded(record.Original);
Counters.FilesSkipped.Increment();
if (!PathUtilities.TryGetNormalizedFilePath(record.Original, out var normalizedPath))
{
logger.UrlCouldNotBeConverted(record.Original);
Counters.FilesSkipped.Increment();
continue;
}

logger.UrlTransformed(record.Original, normalizedPath);

var writePath = Path.Combine(outputDir, normalizedPath);
var pageKey = new PageKey(record.UrlKey, normalizedPath);
var foundPage = pagesStore.TryGetDownloadedPageTimestamp(pageKey.Value, out var timestamp);
if (!foundPage || timestamp < record.Timestamp)
{
await TryWritePageAsync(record, writePath, pageKey, foundPage, CancellationToken.None).ConfigureAwait(false);
}
else
{
logger.UrlAlreadyDownloaded(record.Original);
Counters.FilesSkipped.Increment();
}
}
}
}

catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
}
logger.ExitingWorker();
}

Expand Down
2 changes: 1 addition & 1 deletion WaybackDownloader/Services/PageWorkerRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private Task StartAsync(string outputDir, CancellationToken cancellationToken)
private int _numberOfEvaluationsAtRequiredSpeed;
private async Task EvaluateLimitAsync(string outputDir, int requestedDownloadLimit, CancellationToken cancellationToken)
{
const int SegmentDurationSeconds = 5;
const int SegmentDurationSeconds = 2;
const float MinimumThreshold = 0.9f;
await Task.Yield();
while (!cancellationToken.IsCancellationRequested)
Expand Down
Loading