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 7a992f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 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
44 changes: 24 additions & 20 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,33 @@ 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;
IsAnyUnhealthy = true;
_dockerManager.UnhealthyContainers.Add(container);
}
await ContainerHealthcheckLogs(executionContext, container, healthcheck);
}
if (IsAnyUnhealthy) throw new InvalidOperationException("One or more containers failed to start.");
else
{
executionContext.Output($"{container.ContainerNetworkAlias} service is healthy.");
}
}
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 @@ -323,7 +336,7 @@ private async Task StopContainerAsync(IExecutionContext executionContext, Contai
if (string.Equals(healthcheck, "healthy", StringComparison.OrdinalIgnoreCase))
{
// Print logs for service container jobs (not the "action" job itself b/c that's already logged).
// Print them only if the service was healthy, else they were already logged via ContainerHealthCheckLogs.
// Print them only if the service was healthy, else they were already logged in the initialize containers section.
executionContext.Output($"Print service container logs: {container.ContainerDisplayName}");

int logsExitCode = await _dockerManager.DockerLogs(executionContext, container.ContainerId);
Expand Down Expand Up @@ -439,20 +452,11 @@ public async Task<string> Healthcheck(IExecutionContext executionContext, Contai
return serviceHealth;
}

public async Task ContainerHealthcheckLogs(IExecutionContext executionContext, ContainerInfo container, string serviceHealth)
public async Task ContainerErrorLogs(IExecutionContext executionContext, ContainerInfo container)
{

if (string.Equals(serviceHealth, "healthy", StringComparison.OrdinalIgnoreCase))
{
executionContext.Output($"{container.ContainerNetworkAlias} service is healthy.");
}
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 _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)
Expand Down

0 comments on commit 7a992f8

Please sign in to comment.