Skip to content

Commit

Permalink
Fixed crash report analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Nov 26, 2023
1 parent e18857e commit 8db4834
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public async Task Execute(IJobExecutionContext context)
var client = scope.ServiceProvider.GetRequiredService<CrashReporterClient>();
await using var crashReportBatchedHandler = scope.ServiceProvider.GetRequiredService<CrashReportBatchedHandler>();

await foreach (var batch in client.GetNewCrashReportMetadatasAsync(DateTime.UtcNow.AddDays(-2), ct).OfType<CrashReportFileMetadata>().ChunkAsync(1000).WithCancellation(ct))
await foreach (var batch in client.GetNewCrashReportMetadatasAsync(DateTime.UtcNow.AddDays(-20), ct).OfType<CrashReportFileMetadata>().ChunkAsync(1000).WithCancellation(ct))
{
await crashReportBatchedHandler.HandleBatchAsync(batch, ct);
processed += batch.Count;
processed += await crashReportBatchedHandler.HandleBatchAsync(batch, ct);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,31 @@ public CrashReportBatchedHandler(ILogger<CrashReportBatchedHandler> logger, IOpt
_client = client;
}

public async Task HandleBatchAsync(IEnumerable<CrashReportFileMetadata> requests, CancellationToken ct)
public async Task<int> HandleBatchAsync(IEnumerable<CrashReportFileMetadata> requests, CancellationToken ct)
{
await _lock.WaitAsync(ct);

_toDownloadChannel = Channel.CreateBounded<CrashReportFileMetadata>(ParallelCount * 2);
_httpResultChannel = Channel.CreateBounded<HttpResultEntry>(ParallelCount * 2);
_linkedCrashReportsChannel = Channel.CreateUnbounded<CrashReportToFileIdEntity>();
_ignoredCrashReportsChannel = Channel.CreateUnbounded<CrashReportIgnoredFileEntity>();
try
{
_toDownloadChannel = Channel.CreateBounded<CrashReportFileMetadata>(ParallelCount * 2);
_httpResultChannel = Channel.CreateBounded<HttpResultEntry>(ParallelCount * 2);
_linkedCrashReportsChannel = Channel.CreateUnbounded<CrashReportToFileIdEntity>();
_ignoredCrashReportsChannel = Channel.CreateUnbounded<CrashReportIgnoredFileEntity>();

var filterTask = FilterCrashReportsAsync(requests, ct);
var downloadTask = DownloadCrashReportsAsync(ct);
var writeTask = WriteCrashReportsToDatabaseAsync(ct);
await Task.WhenAll(filterTask, downloadTask, writeTask);
var filterTask = FilterCrashReportsAsync(requests, ct);
var downloadTask = DownloadCrashReportsAsync(ct);
var writeTask = WriteCrashReportsToDatabaseAsync(ct);
await Task.WhenAll(filterTask, downloadTask, writeTask);

if (filterTask.IsFaulted || downloadTask.IsFaulted || writeTask.IsFaulted)
throw new AggregateException(new[] { filterTask.Exception, downloadTask.Exception, writeTask.Exception }.OfType<Exception>());
if (filterTask.IsFaulted || downloadTask.IsFaulted || writeTask.IsFaulted)
throw new AggregateException(new[] { filterTask.Exception, downloadTask.Exception, writeTask.Exception }.OfType<Exception>());

_lock.Release();
return await writeTask;
}
finally
{
_lock.Release();
}
}

private async Task FilterCrashReportsAsync(IEnumerable<CrashReportFileMetadata> crashReports, CancellationToken ct)
Expand Down Expand Up @@ -192,7 +199,7 @@ await Parallel.ForEachAsync(_toDownloadChannel.Reader.ReadAllAsync(ct), options,
}
}

private async Task WriteCrashReportsToDatabaseAsync(CancellationToken ct)
private async Task<int> WriteCrashReportsToDatabaseAsync(CancellationToken ct)
{
var tenant = _tenantContextAccessor.Current;

Expand All @@ -213,7 +220,7 @@ private async Task WriteCrashReportsToDatabaseAsync(CancellationToken ct)
if (report is null)
{
failedCrashReportFileIds.Add(fileId);
return;
continue;
}

var crashReportId = CrashReportId.From(report.Id);
Expand Down Expand Up @@ -284,6 +291,8 @@ private async Task WriteCrashReportsToDatabaseAsync(CancellationToken ct)
await dbContextWrite.CrashReportToFileIds.UpsertOnSaveAsync(linkedCrashReports);
await dbContextWrite.CrashReportIgnoredFileIds.UpsertOnSaveAsync(ignoredCrashReports);
// Disposing the DBContext will save the data

return crashReportsBuilder.Count;
}

public ValueTask DisposeAsync()
Expand Down

0 comments on commit 8db4834

Please sign in to comment.