public Task MonitorEventsAsync(ContainerEventsParameters parameters, IProgress <JSONMessage> progress, CancellationToken cancellationToken = default(CancellationToken)) { return(StreamUtil.MonitorStreamForMessagesAsync( MonitorEventsAsync(parameters, cancellationToken), this._client, cancellationToken, progress)); }
public Task <Stream> MonitorEventsAsync(ContainerEventsParameters parameters, CancellationToken cancellationToken) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } IQueryString queryParameters = new QueryString <ContainerEventsParameters>(parameters); return(this._client.MakeRequestForStreamAsync(this._client.NoErrorHandlers, HttpMethod.Get, "events", queryParameters, cancellationToken)); }
public Task MonitorEventsAsync(ContainerEventsParameters parameters, IProgress <Message> progress, CancellationToken cancellationToken = default(CancellationToken)) { if (progress == null) { throw new ArgumentNullException(nameof(progress)); } return(StreamUtil.MonitorStreamForMessagesAsync( MonitorEventsAsync(parameters, cancellationToken), this._client, cancellationToken, progress)); }
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); }
public async Task MonitorEventsFiltered_Succeeds() { const string repository = "hello-world"; var newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}"; var progressJSONMessage = new ProgressJSONMessage { _onJSONMessageCalled = (m) => { } }; await _client.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = repository }, null, progressJSONMessage); var progressCalledCounter = 0; var eventsParams = new ContainerEventsParameters() { Filters = new Dictionary <string, IDictionary <string, bool> >() { { "event", new Dictionary <string, bool>() { { "tag", true }, { "untag", true } } }, { "type", new Dictionary <string, bool>() { { "image", true } } } } }; var progress = new ProgressMessage() { _onMessageCalled = (m) => { Console.WriteLine($"{System.Reflection.MethodInfo.GetCurrentMethod().Module}->{System.Reflection.MethodInfo.GetCurrentMethod().Name}: _onMessageCalled received: {m.Action} - {m.Status} {m.From} - {m.Type}"); Assert.True(m.Status == "tag" || m.Status == "untag"); progressCalledCounter++; } }; using var cts = new CancellationTokenSource(); var task = Task.Run(() => _client.System.MonitorEventsAsync(eventsParams, progress, cts.Token)); await _client.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = repository }, null, progressJSONMessage); await _client.Images.TagImageAsync(repository, new ImageTagParameters { RepositoryName = repository, Tag = newTag }); await _client.Images.DeleteImageAsync($"{repository}:{newTag}", new ImageDeleteParameters()); var newContainerId = _client.Containers.CreateContainerAsync(new CreateContainerParameters { Image = repository }).Result.ID; await _client.Containers.RemoveContainerAsync(newContainerId, new ContainerRemoveParameters(), cts.Token); cts.Cancel(); bool taskIsCancelled = false; try { await task; } catch (OperationCanceledException) { taskIsCancelled = true; } // On local develop machine task is completed. // On CI/CD Pipeline exception is thrown, not always Assert.True(task.IsCompleted || taskIsCancelled); Assert.Equal(2, progressCalledCounter); }
public async Task MonitorEventsFiltered_Succeeds() { string newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}"; string newImageRespositoryName = Guid.NewGuid().ToString(); await _client.Images.TagImageAsync( $"{_repositoryName}:{_tag}", new ImageTagParameters { RepositoryName = newImageRespositoryName, Tag = newTag }, _cts.Token ); ImageInspectResponse image = await _client.Images.InspectImageAsync( $"{newImageRespositoryName}:{newTag}", _cts.Token ); var progressCalledCounter = 0; var eventsParams = new ContainerEventsParameters() { Filters = new Dictionary <string, IDictionary <string, bool> >() { { "event", new Dictionary <string, bool>() { { "tag", true }, { "untag", true } } }, { "type", new Dictionary <string, bool>() { { "image", true } } }, { "image", new Dictionary <string, bool>() { { image.ID, true } } } } }; var progress = new Progress <Message>((m) => { progressCalledCounter++; Assert.True(m.Status == "tag" || m.Status == "untag"); _output.WriteLine($"MonitorEventsFiltered_Succeeds: Message received: {m.Action} - {m.Status} {m.From} - {m.Type}"); }); using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token); var task = Task.Run(() => _client.System.MonitorEventsAsync(eventsParams, progress, cts.Token)); await _client.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = $"{_repositoryName}:{_tag}" }, null, new Progress <JSONMessage>()); await _client.Images.TagImageAsync($"{_repositoryName}:{_tag}", new ImageTagParameters { RepositoryName = _repositoryName, Tag = newTag }); await _client.Images.DeleteImageAsync($"{_repositoryName}:{newTag}", new ImageDeleteParameters()); var createContainerResponse = await _client.Containers.CreateContainerAsync(new CreateContainerParameters { Image = $"{_repositoryName}:{_tag}" }); await _client.Containers.RemoveContainerAsync(createContainerResponse.ID, new ContainerRemoveParameters(), cts.Token); await Task.Delay(TimeSpan.FromSeconds(1)); cts.Cancel(); await Assert.ThrowsAsync <TaskCanceledException>(() => task); Assert.Equal(2, progressCalledCounter); Assert.True(task.IsCanceled); }
/// <summary> /// Create a new <see cref="MonitorContainerEvents"/> message. /// </summary> /// <param name="parameters"> /// Optional <see cref="ContainerEventsParameters"/> used to control operation behaviour. /// </param> /// <param name="correlationId"> /// An optional message correlation Id. /// </param> public MonitorContainerEvents(ContainerEventsParameters parameters = null, string correlationId = null) : base(correlationId) { Parameters = parameters ?? new ContainerEventsParameters(); }
static void Main(string[] args) { if (Environment.GetEnvironmentVariable("debug") != null) { _debug = true; Console.WriteLine("Starting Windows hosts writer"); } var progress = new Progress <Message>(message => { if (message.Action == "connect") { if (_debug) { Console.WriteLine(EVENT_MSG, "connect", message.Actor.Attributes["container"]); } HandleHosts(true, message); } else if (message.Action == "disconnect") { if (_debug) { Console.WriteLine(EVENT_MSG, "disconnect", message.Actor.Attributes["container"]); } HandleHosts(false, message); } }); var containerEventsParams = new ContainerEventsParameters() { Filters = new Dictionary <string, IDictionary <string, bool> >() { { "event", new Dictionary <string, bool>() { { "connect", true }, { "disconnect", true } } }, { "type", new Dictionary <string, bool>() { { "network", true } } } } }; try { GetClient().System.MonitorEventsAsync(containerEventsParams, progress, default(CancellationToken)).Wait(); } catch (Exception ex) { Console.WriteLine("Something went wrong. Likely the Docker engine is not listening at " + GetClient().Configuration.EndpointBaseUri.ToString() + " inside of the container."); Console.WriteLine("You can change that path through environment variable " + ENV_ENDPOINT); if (_debug) { Console.WriteLine("Exception is " + ex.Message); Console.WriteLine(ex.StackTrace); if (ex.InnerException != null) { Console.WriteLine(); Console.WriteLine("InnerException is " + ex.InnerException.Message); Console.WriteLine(ex.InnerException.StackTrace); } } } }