Skip to content

Commit

Permalink
Moved service containers error logs to separate group sections
Browse files Browse the repository at this point in the history
  • Loading branch information
AvaStancu committed Sep 9, 2022
1 parent dfcbe9b commit 0533cbf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/Runner.Worker/Container/DockerCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IDockerCommandManager : IRunnerService
{
string DockerPath { get; }
string DockerInstanceLabel { get; }
IList<ContainerInfo> UnhealthyContainers {get; set; }
Task<DockerVersion> DockerVersion(IExecutionContext context);
Task<int> DockerPull(IExecutionContext context, string image);
Task<int> DockerPull(IExecutionContext context, string image, string configFileDirectory);
Expand All @@ -42,6 +43,8 @@ public class DockerCommandManager : RunnerService, IDockerCommandManager

public string DockerInstanceLabel { get; private set; }

public IList<ContainerInfo> UnhealthyContainers {get; set; }

public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
Expand Down
35 changes: 24 additions & 11 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,29 @@ public async Task StartContainersAsync(IExecutionContext executionContext, objec
}

executionContext.Output("##[group]Waiting for all services to be ready");

bool IsAnyUnhealthy = false;
_dockerManager.UnhealthyContainers = new List<ContainerInfo>();
foreach (var container in containers.Where(c => !c.IsJobContainer))
{

var healthcheck = await Healthcheck(executionContext, container);
if (!string.Equals(healthcheck, "healthy", StringComparison.OrdinalIgnoreCase))
{
IsAnyUnhealthy = true;
}
await ContainerHealthcheckLogs(executionContext, container, healthcheck);
}
if (IsAnyUnhealthy) throw new InvalidOperationException("One or more containers failed to start.");
IsAnyUnhealthy = true;
_dockerManager.UnhealthyContainers.Add(container);
}
}
executionContext.Output("##[endgroup]");

if (IsAnyUnhealthy)
{
foreach (var container in _dockerManager.UnhealthyContainers)
{
executionContext.Output($"##[group]Service container {container.ContainerNetworkAlias} failed.");
await ContainerErrorLogs(executionContext, container);
executionContext.Output("##[endgroup]");
}
throw new InvalidOperationException("One or more containers failed to start.");
}
}

public void printHello()
Expand Down Expand Up @@ -448,13 +457,17 @@ public async Task ContainerHealthcheckLogs(IExecutionContext executionContext, C
}
else
{
executionContext.Output($"Container {container.ContainerImage} failed healthchecks, printing logs:");
await _dockerManager.DockerLogs(context: executionContext, containerId: container.ContainerId);
executionContext.Error($"Failed to initialize container {container.ContainerImage}");
container.IsHealthy = false;
await ContainerErrorLogs(executionContext, container);
}
}

public async Task ContainerErrorLogs(IExecutionContext executionContext, ContainerInfo container)
{
await _dockerManager.DockerLogs(context: executionContext, containerId: container.ContainerId);
executionContext.Error($"Failed to initialize container {container.ContainerImage}");
container.IsHealthy = false;
}

private async Task<string> ContainerRegistryLogin(IExecutionContext executionContext, ContainerInfo container)
{
if (string.IsNullOrEmpty(container.RegistryAuthUsername) || string.IsNullOrEmpty(container.RegistryAuthPassword))
Expand Down

0 comments on commit 0533cbf

Please sign in to comment.