예제 #1
0
        private static async Task <bool> runCompetitionInsideContainer(Gene gene, string mapPath, DirectoryInfo rootDirectory, CancellationToken cancellationToken = default)
        {
            await initializeCompeteionDirectory(gene, 7099, mapPath, rootDirectory, cancellationToken : cancellationToken);

            var containerId = await DockerService.RunArenaAsync(Program.DockerImageName, rootDirectory.FullName, cancellationToken);

            var startTime   = DateTime.Now;
            var hasFinished = true;

            while (await DockerService.IsContainerRunningAsync(containerId, cancellationToken))
            {
                if ((DateTime.Now - startTime) > _maximumAllowedRunTime)
                {
                    //Console.WriteLine($"gene {geneId} didn't finish");
                    hasFinished = false;
                    break;
                }
                await Task.Delay(1000, cancellationToken);
            }

            if (hasFinished)
            {
                var defenderClientOutputPath = ClientManager.GetClientOutputPath(rootDirectory, ClientMode.defend);
                var attackerClientOutputPath = ClientManager.GetClientOutputPath(rootDirectory, ClientMode.attack);

                hasFinished = File.Exists(defenderClientOutputPath) && File.Exists(attackerClientOutputPath);
            }

            var containerInfoPath = Path.Combine(rootDirectory.FullName, "container.info");
            await File.AppendAllLinesAsync(containerInfoPath,
                                           await DockerService.GetContainerInfo(containerId, showAll: true, cancellationToken: cancellationToken),
                                           cancellationToken);

            var containerInspect = Path.Combine(rootDirectory.FullName, "container.inspect.json");
            await File.WriteAllTextAsync(containerInspect,
                                         await DockerService.GetContainerInspect(containerId, cancellationToken: cancellationToken),
                                         cancellationToken);

            var containerLogPath = Path.Combine(rootDirectory.FullName, "container.log");
            await File.WriteAllTextAsync(containerLogPath,
                                         await DockerService.ContainerLogAsync(containerId, cancellationToken),
                                         cancellationToken);

            await DockerService.StopAndRemoveContainerAsync(containerId, cancellationToken);

            return(hasFinished);
        }