using System.Diagnostics; namespace MeetingAssistant.MeetingNotes; internal static class MeetingMetadataDiagnosticRunner { public static async Task RunAsync( string provider, DateTimeOffset startedAt, TimeSpan timeout, Func> lookupAsync, string noMatchError, CancellationToken cancellationToken) { var stopwatch = Stopwatch.StartNew(); try { var lookup = lookupAsync(cancellationToken); var metadata = timeout > TimeSpan.Zero ? await lookup.WaitAsync(timeout, cancellationToken) : await lookup.WaitAsync(cancellationToken); return new MeetingMetadataDiagnosticResult( provider, startedAt, metadata is not null, false, stopwatch.ElapsedMilliseconds, metadata, metadata is null ? noMatchError : null); } catch (TimeoutException exception) { return new MeetingMetadataDiagnosticResult( provider, startedAt, false, true, stopwatch.ElapsedMilliseconds, null, exception.Message); } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { throw; } catch (Exception exception) { return new MeetingMetadataDiagnosticResult( provider, startedAt, false, false, stopwatch.ElapsedMilliseconds, null, exception.Message); } } }