예제 #1
0
        static async Task CheckQueue(IQueueClient queueClient, KubeClient kubeClient, IScalingRule scalingRule, ConfigurationValues configuration, ConfigurationManager configurationManager)
        {
            Console.WriteLine("Nanny.Checker is Running.");

            var queueName = configuration.Queue.QueueName;

            do
            {
                if (!configurationManager.GetNotRequired("NANNY_IS_ACTIVE", defaultValue: true))
                {
                    var nextCheck = 60;
                    Console.WriteLine($"Waiting for {nextCheck} seconds, before checking queue {queueName}");
                    await Task.Delay(nextCheck * 1000);
                }

                var messageCount = await queueClient.GetMessageCountAsync(queueName);

                Console.WriteLine($"There are {messageCount} messages in the queue {queueName}");

                var currentRunningJobs = await GetCurrentRunningJobs(kubeClient, configuration.Kubernetes.K8Namespace, queueName);

                Console.WriteLine($"I have found {messageCount} jobs in Kubernetes");

                if (await kubeClient.IsResourceAvailableAsync() && currentRunningJobs < configuration.Kubernetes.ContainerLimit)
                {
                    var result = scalingRule.GetJobScalingResult(messageCount, currentRunningJobs);

                    if (result.JobCount > 0)
                    {
                        Console.WriteLine($"Creating {result.JobCount} jobs");
                        await CreateKubeJob(kubeClient, configuration.Kubernetes, queueName, result.JobCount);
                    }
                    Console.WriteLine($"Waiting for {result.NextCheck.TotalSeconds} seconds, before checking queue {queueName}");
                    await Task.Delay(result.NextCheck);
                }
                else
                {
                    Console.WriteLine("No more resources available in the cluster!");
                    await Task.Delay(5 * 60 * 1000);
                }
            } while (true);
        }