Skip to content

Commit

Permalink
Simplify log source for different resource kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Oct 5, 2023
1 parent 3807769 commit a99554e
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 116 deletions.
54 changes: 24 additions & 30 deletions src/Aspire.Dashboard/Components/Pages/ContainerLogs.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/ContainerLogs/{containerID?}"
@page "/ContainerLogs/{containerid?}"
@using Aspire.Dashboard.Model
@inject IDashboardViewModelService DashboardViewModelService
@inject IJSRuntime JS
Expand All @@ -13,29 +13,28 @@
<FluentStack Orientation="Orientation.Vertical">
<FluentStack Orientation="Orientation.Horizontal" VerticalAlignment="VerticalAlignment.Center">
<FluentSelect TOption="ContainerViewModel"
Items="@Containers"
OptionValue="@(c => c.ContainerID)"
OptionText="GetContainerDisplayText"
@bind-SelectedOption="_selectedContainer"
@bind-SelectedOption:after="HandleSelectedOptionChangedAsync" />
Items="@Containers"
OptionValue="@(c => c.ContainerID)"
OptionText="GetContainerDisplayText"
@bind-SelectedOption="_selectedContainer"
@bind-SelectedOption:after="HandleSelectedOptionChangedAsync" />
<FluentLabel Typo="Typography.Body">@_status</FluentLabel>
</FluentStack>
<LogViewer @ref="_logViewer" />
</FluentStack>
<LogViewer @ref="_logViewer" />
</FluentStack>
</div>
</div>

@code {

[Parameter]
public string? ContainerID { get; set; }
public string? ContainerId { get; set; }

private Dictionary<string, ContainerViewModel> _containerNameMapping = new();
private IEnumerable<ContainerViewModel> Containers => _containerNameMapping.Select(kvp => kvp.Value).OrderBy(c => c.Name);
private ContainerViewModel? _selectedContainer;
private LogViewer? _logViewer;
private CancellationTokenSource _watchContainersTokenSource = new CancellationTokenSource();
private CancellationTokenSource? _watchLogsTokenSource;
private IContainerLogWatcher? _currentWatcher;
private string _status = ContainerLogStatuses.Initializing;

protected override async Task OnInitializedAsync()
Expand All @@ -49,9 +48,9 @@
_containerNameMapping[result.ViewModel.Name] = result.ViewModel;
}

if (ContainerID is not null)
if (ContainerId is not null)
{
_selectedContainer = initialList?.FirstOrDefault(c => string.Equals(ContainerID, c.ViewModel.ContainerID, StringComparison.Ordinal))?.ViewModel;
_selectedContainer = initialList?.FirstOrDefault(c => string.Equals(ContainerId, c.ViewModel.ContainerID, StringComparison.Ordinal))?.ViewModel;
}
else if (initialList?.Count > 0)
{
Expand All @@ -72,7 +71,7 @@
private Task ClearLogsAsync()
=> _logViewer is not null ? _logViewer.ClearLogsAsync() : Task.CompletedTask;

private async Task LoadLogsAsync()
private async ValueTask LoadLogsAsync()
{
if (_selectedContainer is null)
{
Expand All @@ -85,24 +84,23 @@
else
{
_watchLogsTokenSource = new CancellationTokenSource();
_currentWatcher = _selectedContainer.LogSource.GetWatcher();

if (await _currentWatcher.InitWatchAsync(_watchLogsTokenSource.Token))
if (await _selectedContainer.LogSource.StartAsync(_watchLogsTokenSource.Token))
{
_ = Task.Run(async () =>
{
await _logViewer.WatchLogsAsync(() => _currentWatcher.WatchOutputLogsAsync(_watchLogsTokenSource.Token), LogEntryType.Default);
await _logViewer.WatchLogsAsync(() => _selectedContainer.LogSource.WatchOutputLogAsync(_watchLogsTokenSource.Token), LogEntryType.Default);
});

_ = Task.Run(async () =>
{
await _logViewer.WatchLogsAsync(() => _currentWatcher.WatchErrorLogsAsync(_watchLogsTokenSource.Token), LogEntryType.Error);
await _logViewer.WatchLogsAsync(() => _selectedContainer.LogSource.WatchErrorLogAsync(_watchLogsTokenSource.Token), LogEntryType.Error);
});

_status = ContainerLogStatuses.WatchingLogs;
}
else
{
_watchLogsTokenSource = null;
_status = ContainerLogStatuses.FailedToInitialize;
}
}
Expand All @@ -114,8 +112,7 @@
{
// Change the URL
NavigationManager.NavigateTo($"/containerLogs/{_selectedContainer.ContainerID}");
await DisposeCurrentWatcher();
await DisposeWatchLogsTokenSource();
await StopWatchingLogsAsync();
await ClearLogsAsync();
await LoadLogsAsync();
}
Expand All @@ -129,7 +126,7 @@

if (_selectedContainer is null)
{
if (string.IsNullOrEmpty(ContainerID) || string.Equals(ContainerID, containerViewModel.ContainerID, StringComparison.Ordinal))
if (string.IsNullOrEmpty(ContainerId) || string.Equals(ContainerId, containerViewModel.ContainerID, StringComparison.Ordinal))
{
_selectedContainer = containerViewModel;
await LoadLogsAsync();
Expand All @@ -148,6 +145,7 @@
if (_containerNameMapping.Count > 0)
{
_selectedContainer = Containers.First();
await HandleSelectedOptionChangedAsync();
}
}
}
Expand All @@ -172,27 +170,23 @@
public async ValueTask DisposeAsync()
{
await DisposeWatchContainersTokenSource();
await DisposeWatchLogsTokenSource();
await DisposeCurrentWatcher();
await StopWatchingLogsAsync();
}

private ValueTask DisposeCurrentWatcher()
=> _currentWatcher is null
? ValueTask.CompletedTask
: _currentWatcher.DisposeAsync();

private async Task DisposeWatchContainersTokenSource()
{
await _watchContainersTokenSource.CancelAsync();
_watchContainersTokenSource.Dispose();
}

private async Task DisposeWatchLogsTokenSource()
private async Task StopWatchingLogsAsync()
{
if (_watchLogsTokenSource is not null)
{
await _watchLogsTokenSource.CancelAsync();
_watchLogsTokenSource.Dispose();
// The token source only gets created if selected container is not null
await _selectedContainer!.LogSource.StopAsync();
_watchLogsTokenSource = null;
}
}
Expand Down
50 changes: 28 additions & 22 deletions src/Aspire.Dashboard/Components/Pages/ExecutableLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
_selectedExecutable = initialList[0].ViewModel;
}

LoadLogs();
await LoadLogsAsync();

_ = Task.Run(async () =>
{
Expand All @@ -71,35 +71,38 @@
private Task ClearLogsAsync()
=> _logViewer is not null ? _logViewer.ClearLogsAsync() : Task.CompletedTask;

private void LoadLogs()
private async ValueTask LoadLogsAsync()
{
if (_selectedExecutable is null)
{
_status = ExecutableLogStatuses.NoExecutableSelected;
}
else if (_selectedExecutable?.LogSource?.Available != true)
{
_status = ExecutableLogStatuses.LogsNotYetAvailable;
}
else if (_logViewer is null)
{
_status = ExecutableLogStatuses.InitializingLogViewer;
}
else
{
_watchLogsTokenSource = new CancellationTokenSource();

_ = Task.Run(async () =>
if (await _selectedExecutable.LogSource.StartAsync(_watchLogsTokenSource.Token))
{
await _logViewer.WatchLogsAsync(() => _selectedExecutable.LogSource.WatchOutputLogAsync(_watchLogsTokenSource.Token), LogEntryType.Default);
});
_ = Task.Run(async () =>
{
await _logViewer.WatchLogsAsync(() => _selectedExecutable.LogSource.WatchOutputLogAsync(_watchLogsTokenSource.Token), LogEntryType.Default);
});

_ = Task.Run(async () =>
{
await _logViewer.WatchLogsAsync(() => _selectedExecutable.LogSource.WatchOutputLogAsync(_watchLogsTokenSource.Token), LogEntryType.Error);
});
_ = Task.Run(async () =>
{
await _logViewer.WatchLogsAsync(() => _selectedExecutable.LogSource.WatchOutputLogAsync(_watchLogsTokenSource.Token), LogEntryType.Error);
});

_status = ExecutableLogStatuses.WatchingLogs;
_status = ExecutableLogStatuses.WatchingLogs;
}
else
{
_watchLogsTokenSource = null;
_status = ExecutableLogStatuses.LogsNotYetAvailable;
}
}
}

Expand All @@ -108,9 +111,9 @@
if (_selectedExecutable is not null)
{
NavigationManager.NavigateTo($"/ExecutableLogs/{_selectedExecutable.Name}");
await DisposeWatchLogsTokenSource();
await StopWatchingLogsAsync();
await ClearLogsAsync();
LoadLogs();
await LoadLogsAsync();
}
}

Expand All @@ -125,7 +128,7 @@
if (string.IsNullOrEmpty(ExecutableName) || string.Equals(ExecutableName, ExecutableViewModel.Name, StringComparison.Ordinal))
{
_selectedExecutable = ExecutableViewModel;
LoadLogs();
await LoadLogsAsync();
}
}
}
Expand All @@ -138,9 +141,9 @@
var originalSelection = _selectedExecutable;
_selectedExecutable = ExecutableViewModel;

if (!originalSelection!.LogSource.Available && _selectedExecutable.LogSource.Available)
if (_watchLogsTokenSource is null)
{
LoadLogs();
await LoadLogsAsync();
}
}
}
Expand All @@ -152,6 +155,7 @@
if (_executableNameMapping.Count > 0)
{
_selectedExecutable = Executables.First();
await HandleSelectedOptionChangedAsync();
}
}
}
Expand All @@ -176,7 +180,7 @@
public async ValueTask DisposeAsync()
{
await DisposeWatchExecutablesTokenSource();
await DisposeWatchLogsTokenSource();
await StopWatchingLogsAsync();
}

private async Task DisposeWatchExecutablesTokenSource()
Expand All @@ -185,12 +189,14 @@
_watchExecutablesTokenSource.Dispose();
}

private async Task DisposeWatchLogsTokenSource()
private async Task StopWatchingLogsAsync()
{
if (_watchLogsTokenSource is not null)
{
await _watchLogsTokenSource.CancelAsync();
_watchLogsTokenSource.Dispose();
// The token source only gets created if selected executable is not null
await _selectedExecutable!.LogSource.StopAsync();
_watchLogsTokenSource = null;
}
}
Expand Down
Loading

0 comments on commit a99554e

Please sign in to comment.