Example #1
0
        public static IAutoScalerHandler CreateAutoScaler(IDictionary <string, string> resourceDetails, IAutoScalerPolicy policy)
        {
            var autoScaler = new Handlers.AutoScalerHandler();

            autoScaler.Initialize(resourceDetails, policy);
            return(autoScaler);
        }
Example #2
0
        /// <summary>
        /// Initializes all required resources by the AutoSclaer
        /// </summary>
        public void Initialize(IDictionary <string, string> resourceDetails, IAutoScalerPolicy policy)
        {
            if (resourceDetails == null)
            {
                throw new ArgumentException("Argument 'resourceDetails' cannot be null, missing properties for initialization in AutoScaler!");
            }

            //
            // Read configuration settings
            //
            _defaultBatchId = resourceDetails[Constants.QUEUE_HANDLER_INIT_PROP_DEFAULT_BATCH_ID];
            _storageConn    = resourceDetails[Constants.QUEUE_HANDLER_INIT_PROP_STORAGECONNECTION];
            _serviceBusConn = resourceDetails[GlobalConstants.SERVICEBUS_INTERNAL_CONNECTIONSTRING_CONFIGNAME];
            _checkForJobProcessorCommandsIntervalInSeconds = int.Parse(resourceDetails[GlobalConstants.GERES_CONFIG_AUTOSCALER_CHECKFORJOBHOSTUPDATES_INTERVAL]);

            if (string.IsNullOrEmpty(_defaultBatchId) || string.IsNullOrEmpty(_storageConn))
            {
                throw new ArgumentException("Missing properties in resourceDetails parameter!");
            }

            //
            // Check the loaded policy
            //
            if (policy == null)
            {
                throw new ArgumentException("Missing property for autoscaler policy!");
            }
            _policy = policy;

            //
            // Create all required repositories
            //
            _jobHostRepository = RepositoryFactory.CreateJobHostRepository(_storageConn, RoleEnvironment.DeploymentId);
            _batchRepository   = RepositoryFactory.CreateBatchRepository(_storageConn);
            _roleOpsRepository = RepositoryFactory.CreateRoleOperationStatusRepository(_storageConn);

            //
            // Connect to storage for the jobs-queue
            //
            var cloudAccount = CloudStorageAccount.Parse(_storageConn);

            _cloudQueueClient = cloudAccount.CreateCloudQueueClient();
            _defaultQueue     = _cloudQueueClient.GetQueueReference(_defaultBatchId);
            _defaultQueue.CreateIfNotExists();

            //
            // Connect to the service bus for autoscaler commands
            //
            _jobHostServiceBus = new JobHostServiceBus(_serviceBusConn, GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER,
                                                       GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORJOBHOST);
            var subscribeToJobHostCommandsTask = Task.Run(() =>
            {
                SubscriptionClient client = null;

                while (true)
                {
                    try
                    {
                        if (client == null || client.IsClosed)
                        {
                            client = _jobHostServiceBus.CreateSubscription(GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER,
                                                                           GlobalConstants.SERVICEBUS_INTERNAL_SUBSCRIPTION_JOBHOSTUPDATES);
                        }

                        var msg = client.Receive(TimeSpan.FromSeconds(_checkForJobProcessorCommandsIntervalInSeconds));
                        if (msg != null)
                        {
                            if (ProcessCommandFromJobHost(msg))
                            {
                                msg.Complete();
                            }
                            else
                            {
                                msg.Abandon();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        client = null;
                        Geres.Diagnostics.GeresEventSource.Log.AutoScalerWorkerAutoScalerListenForJobHostUpdatesFailed
                        (
                            RoleEnvironment.CurrentRoleInstance.Id, RoleEnvironment.DeploymentId,
                            string.Format("Topic: {0}, Subscription: {1}", GlobalConstants.SERVICEBUS_INTERNAL_SUBSCRIPTION_JOBHOSTUPDATES,
                                          GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER),
                            ex.Message,
                            ex.StackTrace
                        );
                    }
                }
            });
        }