示例#1
0
        /// <summary>
        /// Starts the resource reaper if it is enabled
        /// </summary>
        /// <param name="dockerClient">Docker client to use</param>
        /// <param name="loggerFactory">Optional loggerFactory to log progress</param>
        /// <returns>Task that completes when reaper starts successfully</returns>
        public static async Task StartAsync(IDockerClient dockerClient, ILoggerFactory loggerFactory = null)
        {
            var logger = loggerFactory?.CreateLogger(typeof(ResourceReaper));

            var disabled = Environment.GetEnvironmentVariable("REAPER_DISABLED");

            if (!string.IsNullOrWhiteSpace(disabled) &&
                (disabled.Equals("1") || disabled.ToLower().Equals("true")))
            {
                logger?.LogInformation("Reaper is disabled via $REAPER_DISABLED environment variable");
                return;
            }

            var ryukImage = Environment.GetEnvironmentVariable("REAPER_IMAGE");

            if (string.IsNullOrWhiteSpace(ryukImage))
            {
                ryukImage = DefaultRyukImage;
            }

            if (_ryukStartupTaskCompletionSource == null)
            {
                await InitLock.WaitAsync();

                try
                {
                    if (_ryukStartupTaskCompletionSource == null)
                    {
                        logger?.LogDebug("Starting ryuk container ...");

                        _ryukStartupTaskCompletionSource = new TaskCompletionSource <bool>();
                        _ryukContainer = new RyukContainer(ryukImage, dockerClient,
                                                           loggerFactory ?? NullLoggerFactory.Instance);

                        await _ryukContainer.StartAsync();

                        _ryukContainer.AddToDeathNote(new LabelsFilter(Labels));
                        _ryukStartupTaskCompletionSource.SetResult(true);

                        logger?.LogDebug("Ryuk container started");
                    }
                    else
                    {
                        logger?.LogDebug("Reaper is already started");
                    }
                }
                finally
                {
                    InitLock.Release();
                }
            }
            else
            {
                logger?.LogDebug("Reaper is already started");
            }

            SetupShutdownHook(dockerClient);

            await _ryukStartupTaskCompletionSource.Task;
        }
        /// <summary>
        /// Starts the resource reaper if it is enabled
        /// </summary>
        /// <param name="dockerClient">Docker client to use</param>
        /// <returns>Task that completes when reaper starts successfully</returns>
        public static async Task StartAsync(IDockerClient dockerClient)
        {
            var disabled = Environment.GetEnvironmentVariable("REAPER_DISABLED");

            if (!string.IsNullOrWhiteSpace(disabled) &&
                (disabled.Equals("1") || disabled.ToLower().Equals("true")))
            {
                return;
            }

            // todo: Remove this when ryuk for windows is complete
            var platformSpecificFactory = new PlatformSpecificFactory();

            if (platformSpecificFactory.IsWindows())
            {
                // don't start resource reaper for windows
                // because ryuk for windows is not developed yet
                return;
            }

            if (_ryukStartupTaskCompletionSource == null)
            {
                await InitLock.WaitAsync();

                try
                {
                    if (_ryukStartupTaskCompletionSource == null)
                    {
                        _ryukStartupTaskCompletionSource = new TaskCompletionSource <bool>();
                        _ryukContainer = new RyukContainer(dockerClient, platformSpecificFactory.Create(),
                                                           NullLoggerFactory.Instance);

                        var ryukStartupTask = _ryukContainer.StartAsync();
                        await ryukStartupTask.ContinueWith(_ =>
                        {
                            _ryukContainer.AddToDeathNote(new LabelsFilter(Labels));
                            _ryukStartupTaskCompletionSource.SetResult(true);
                        });
                    }
                }
                finally
                {
                    InitLock.Release();
                }
            }

            await _ryukStartupTaskCompletionSource.Task;
        }