Пример #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting monitor.");
            var result = await _dockerService.GetRunningContainers();

            var containers = result.Where(c => c.Name.MatchesGlob(_options.ContainerGlob)).ToList();

            _logger.LogTrace($"Found {containers.Count} running container{(containers.Count > 1 ? "s" : "")} matching glob '{_options.ContainerGlob}'.");

            foreach (var container in containers)
            {
                AttachNotifiersForContainer(container);
            }

            _monitor         = _dockerService.MonitorContainerEvents();
            _monitor.OnEvent = HandleEvent;
        }
Пример #2
0
        public ContainerEventsMonitor MonitorContainerEvents()
        {
            var tokenSource = new CancellationTokenSource();
            var monitor     = new ContainerEventsMonitor(tokenSource);

            var progress = new Progress <JSONMessage>(async message =>
            {
                var container = await GetContainerById(message.ID);
                var e         = new ContainerMonitorEvent()
                {
                    ID        = message.ID,
                    Type      = message.Status,
                    Container = container
                };

                monitor.OnEvent?.Invoke(e);
            });

            var filters = new Dictionary <string, IDictionary <string, bool> >()
            {
                {
                    "type", new Dictionary <string, bool>()
                    {
                        { "container", true }
                    }
                },
                {
                    "event", new Dictionary <string, bool>()
                    {
                        { "start", true },
                        { "die", true },
                    }
                },
            };

            var eventParams = new ContainerEventsParameters()
            {
                Filters = filters,
            };

            _ = _Client.System.MonitorEventsAsync(eventParams, progress, tokenSource.Token);

            return(monitor);
        }