Ejemplo n.º 1
0
        public async Task RetrieveResources(SessionHostsStartInfo sessionHostsStartInfo)
        {
            string registryWithImageName = $"{sessionHostsStartInfo.ImageDetails.Registry}/{sessionHostsStartInfo.ImageDetails.ImageName}";
            string imageTag = sessionHostsStartInfo.ImageDetails.ImageTag;
            string username = sessionHostsStartInfo.ImageDetails.Username;
            string password = sessionHostsStartInfo.ImageDetails.Password;

            if (string.IsNullOrEmpty(imageTag))
            {
                imageTag = "latest";
            }

            _logger.LogInformation($"Starting image pull for: {registryWithImageName}:{imageTag}.");
            LogReporter logReporter = new LogReporter(_logger);

            Polly.Retry.AsyncRetryPolicy retryPolicy = Policy
                                                       .Handle <Exception>((Exception e) =>
            {
                _logger.LogError($"Exception encountered when creating image: {e.ToString()}");
                return(true);
            })
                                                       .WaitAndRetryAsync(_maxRetryAttempts, i => TimeSpan.FromMinutes(_createImageRetryTimeMins / _maxRetryAttempts));

            await retryPolicy.ExecuteAsync(async() =>
            {
                await _dockerClient.Images.CreateImageAsync(
                    new ImagesCreateParameters {
                    FromImage = registryWithImageName, Tag = imageTag
                },
                    new AuthConfig()
                {
                    Username = username, Password = password
                },
                    logReporter).ConfigureAwait(false);
            });

            // Making sure that the image was actually downloaded properly
            // We have seen some cases where Docker Registry API returns 'success' on pull while the image has not been properly downloaded
            IEnumerable <ImagesListResponse> images = await _dockerClient.Images.ListImagesAsync(new ImagesListParameters { All = true });

            if (images.All(image => !image.RepoTags.Contains($"{registryWithImageName}:{imageTag}")))
            {
                throw new ApplicationException("CreateImageAsync is completed but the image doesn't exist");
            }

            _logger.LogEvent(MetricConstants.PullImage, null, new Dictionary <string, double>
            {
                { MetricConstants.DownloadDurationInMilliseconds, logReporter.DownloadSummary?.DurationInMilliseconds ?? 0d },
                { MetricConstants.ExtractDurationInMilliseconds, logReporter.ExtractionSummary?.DurationInMilliseconds ?? 0d },
                { MetricConstants.SizeInBytes, logReporter.DownloadSummary?.TotalSizeInBytes ?? 0d }
            }
                             );
        }
Ejemplo n.º 2
0
        public static async Task <T> TimedExecute <T>(Func <Task <T> > action, MultiLogger logger, string eventName = null, string metricName = null, long elapsedThreshold = -1)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            T         result    = await action();

            if (!string.IsNullOrEmpty(eventName))
            {
                long elapsedMs = stopwatch.ElapsedMilliseconds;
                if (elapsedMs > elapsedThreshold)
                {
                    logger.LogEvent(eventName, null, new Dictionary <string, double>()
                    {
                        { metricName, elapsedMs },
                    });
                }
            }

            return(result);
        }