Example #1
0
        /// <summary>
        ///  Expected return error code:
        ///     200 no error
        ///     400 bad parameter
        ///     500 server error
        /// </summary>
        internal async Task <Stream> MonitorEventAsync(ContainerEventsConfig eventsConfig)
        {
            var requestPath = "events";
            var queryString = QueryParameterHelper.BuildQueryString(eventsConfig);

            var response = await this.client.MakeRequestForHttpResponseAsync(
                HttpMethod.Get,
                requestPath,
                queryString,
                null,
                null,
                Timeout.InfiniteTimeSpan).ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var statusCode = response.StatusCode;

                response.Dispose();

                throw new ContainerApiException(statusCode, null);
            }

            try
            {
                return(await response.Content.ReadAsStreamAsync().ConfigureAwait(false));
            }
            catch (Exception)
            {
                response.Dispose();
                throw;
            }
        }
        internal ContainerEventManager(ContainerActivatorService activator)
        {
            this.activatorService = activator;

            var eventTypeList = new List <string>()
            {
                "stop", "die"
            };

            if (HostingConfig.Config.EnableDockerHealthCheckIntegration)
            {
                eventTypeList.Add("health_status");
            }

            this.eventsConfig = new ContainerEventsConfig
            {
                Filters = new Dictionary <string, IList <string> >()
                {
                    { "type", new List <string> ()
                      {
                          "container"
                      } },
                    { "event", eventTypeList }
                }
            };

            this.maxContinuousFailureCount = HostingConfig.Config.ContainerEventManagerMaxContinuousFailure;
            this.retryDelay = TimeSpan.FromSeconds(HostingConfig.Config.ContainerEventManagerFailureBackoffInSec);

            this.continuousFailureCount = 0;
        }
Example #3
0
        internal static string BuildQueryString(ContainerEventsConfig eventsConfig)
        {
            // TODO: use string.join.

            var sb = new StringBuilder();

            if (!string.IsNullOrEmpty(eventsConfig.Since))
            {
                sb.Append($"since={eventsConfig.Since}");
            }

            if (!string.IsNullOrEmpty(eventsConfig.Until))
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }

                sb.Append($"until={eventsConfig.Until}");
            }

            if (eventsConfig.Filters != null && eventsConfig.Filters.Count > 0)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }

                var filterJsonStr = ContainerServiceClient.JsonSerializer.SerializeObject(eventsConfig.Filters);

                filterJsonStr = Uri.EscapeDataString(filterJsonStr);

                sb.Append($"filters={filterJsonStr}");
            }

            return(sb.ToString());
        }