private async Task HandleEvents(IContainerRegistrar registrar, CancellationToken token)
        {
            foreach (var progress in _yieldingProgress.GetEnumerable(token))
            {
                using (var transaction = await registrar.BeginTransaction())
                {
                    //Logger.Info($"Handing new event {progress.Status} for container {progress.ContainerId}.");

                    // attach, commit, copy, create, destroy, detach, die, exec_create, exec_detach, exec_start, export, health_status, kill, oom, pause, rename, resize, restart, start, stop, top, unpause, update

                    switch (progress.Status)
                    {
                    case "update":
                    case "start":
                    case "restart":
                        var containerServices = await _client.GetContainerServicesById(progress.ContainerId, token);

                        foreach (var containerService in containerServices)
                        {
                            ConsiderAddedService(transaction, containerService);
                        }
                        break;

                    case "destroy":
                    case "die":
                    case "stop":
                    case "kill":
                        ConsiderRemovedContainer(transaction, progress.ContainerId);
                        break;
                    }
                }
            }
        }
Пример #2
0
        private async Task MaintenanceLoop(IContainerRegistrar registrar, CancellationToken token)
        {
            using (var transaction = await registrar.BeginTransaction())
            {
                var desiredServices = transaction.GetAllContainerServices();
                var consulServices  = await _client.GetRegisteredServicesAndChecks(token);

                var checks = (from desiredService in desiredServices
                              from consulService in consulServices.Where(x => x.ContainerId == desiredService.ContainerId).DefaultIfEmpty()
                              let status = ""
                                           + $"Container: {desiredService.ContainerId.ToShortContainerName()}\n"
                                           + $"    Image: {desiredService.Image}\n"
                                           + $" Creation: {desiredService.ContainerCreationOn}\n"
                                           + $"    State: {desiredService.ContainerState}\n"
                                           + $"   Status: {desiredService.ContainerStatus}"
                                           select new
                {
                    desiredService.ContainerId,
                    Status = status,
                    consulService?.CheckId,
                    Missing = consulService == null
                }).ToList();

                foreach (var check in checks.Where(x => x.Missing))
                {
                    Logger.Warn($"Container [{check.ContainerId.ToShortContainerName()}] disappeared from consul, assuming the container was removed.");
                    transaction.DeleteContainer(check.ContainerId);
                }

                foreach (var check in checks.Where(x => !x.Missing))
                {
                    await _client.MaintainService(check.CheckId, check.Status, token);
                }
            }
        }
        private async Task Poll(IContainerRegistrar registrar, CancellationToken token)
        {
            using (var transaction = await registrar.BeginTransaction())
            {
                var desiredContainers = await _client.GetRunningContainerServices(token);

                var desiredContainerIds = desiredContainers.Select(x => x.ContainerId).ToList();
                var currentContainers   = transaction.GetContainers();

                var newContainers = from containerId in desiredContainerIds.Except(currentContainers).Distinct()
                                    from containerService in desiredContainers.Where(x => x.ContainerId == containerId)
                                    select containerService;
                var updatedContainers = from c in desiredContainers
                                        from v in currentContainers.Where(x => c.ContainerId == x)
                                        select c;
                var extraContainers = currentContainers.Except(desiredContainerIds).Distinct();

                foreach (var service in newContainers)
                {
                    Logger.Info($"Discovered services [{service.ServiceName}] for container [{service.ContainerId.ToShortContainerName()}].");
                    transaction.AddContainerService(service);
                }

                foreach (var service in updatedContainers)
                {
                    transaction.UpdateContainerService(service);
                }

                foreach (var containerId in extraContainers)
                {
                    Logger.Info($"Container [{containerId.ToShortContainerName()}] was removed.");
                    transaction.DeleteContainer(containerId);
                }
            }
        }
Пример #4
0
        private async Task Cleanup(IContainerRegistrar registrar)
        {
            Logger.Info("A shutdown event has been captured, will begin cleanup logic.");

            using (var transaction = await registrar.BeginTransaction())
            {
                foreach (var containerId in transaction.GetContainers())
                {
                    Logger.Info($"Removing container [{containerId.ToShortContainerName()}].");
                    transaction.DeleteContainer(containerId);
                }
            }

            Logger.Info("Cleanup completed.");
        }
Пример #5
0
        public void When_the_a_cancelation_occurs_then_cleanup_existing_containers()
        {
            var transaction = Substitute.For <IContainerRegistrarTransaction>();

            _containerRegistrar.BeginTransaction().Returns(transaction);

            var containers = Autofixture.Create <List <string> >();

            transaction.GetContainers().Returns(containers);

            var source = new CancellationTokenSource();

            _agent.Monitor(_containerRegistrar, source.Token);

            // Act
            source.Cancel();

            // Assert
            foreach (var container in containers)
            {
                transaction.Received().DeleteContainer(container);
            }
        }