Пример #1
0
        public CloudSMVActionScheduler(SMVCloudConfig config)
        {
            // Set the instance GUID.
            schedulerInstanceGuid = Guid.NewGuid().ToString();
            Log.LogInfo("Scheduler Instance GUID: " + schedulerInstanceGuid);

            Log.LogInfo($"Setting max dequeue count to {maxDequeueCount}");

            // Check if the connection strings are set properly.
            storageConnectionString    = config.StorageConnectionString.value;
            serviceBusConnectionString = config.ServiceBusConnectionString.value;
            if (string.IsNullOrEmpty(storageConnectionString))
            {
                throw new Exception("Connection string \"Microsoft.WindowsAzure.Storage.ConnectionString\" is not set.");
            }
            if (string.IsNullOrEmpty(serviceBusConnectionString))
            {
                throw new Exception("Connection string \"Microsoft.ServiceBus.ConnectionString\" is not set.");
            }

            int retriesLeft = CloudConstants.MaxRetries;

            while (true)
            {
                try
                {
                    // Connect to cloud storage.
                    var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
                    var queueStorage   = storageAccount.CreateCloudQueueClient();
                    var blobStorage    = storageAccount.CreateCloudBlobClient();
                    var tableStorage   = storageAccount.CreateCloudTableClient();

                    // Set up blob storage.
                    blobStorage.DefaultRequestOptions             = new BlobRequestOptions();
                    blobStorage.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);
                    inputContainer  = blobStorage.GetContainerReference(CloudConstants.InputBlobContainerName);
                    outputContainer = blobStorage.GetContainerReference(CloudConstants.OutputBlobContainerName);

                    // Set up our queue.
                    queueStorage.DefaultRequestOptions             = new QueueRequestOptions();
                    queueStorage.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);
                    actionsQueue = queueStorage.GetQueueReference(CloudConstants.InputQueueName);

                    // Set up table storage.
                    tableStorage.DefaultRequestOptions             = new TableRequestOptions();
                    tableStorage.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);
                    CloudTable table = tableStorage.GetTableReference(CloudConstants.TableName);
                    tableDataSource = new ActionsTableDataSource(table);

                    // Set up the service bus subscription.
                    namespaceManager = NamespaceManager.CreateFromConnectionString(serviceBusConnectionString);
                    var filter  = new SqlFilter(String.Format(CultureInfo.CurrentCulture, "(SchedulerInstanceGuid = '{0}')", schedulerInstanceGuid));
                    var subDesc = new SubscriptionDescription(CloudConstants.ResultsTopicName, schedulerInstanceGuid);
                    subDesc.AutoDeleteOnIdle = TimeSpan.FromDays(7.0);
                    if (!namespaceManager.SubscriptionExists(CloudConstants.ResultsTopicName, schedulerInstanceGuid))
                    {
                        namespaceManager.CreateSubscription(subDesc, filter);
                    }
                    subscriptionClient = SubscriptionClient.CreateFromConnectionString(serviceBusConnectionString,
                                                                                       CloudConstants.ResultsTopicName, schedulerInstanceGuid);
                    subscriptionClient.RetryPolicy = new RetryExponential(
                        TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval),
                        TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval * CloudConstants.MaxRetries),
                        CloudConstants.MaxRetries);
                    subscriptionClient.OnMessage((msg) =>
                    {
                        try
                        {
                            ActionComplete(msg);
                        }
                        catch (Exception e)
                        {
                            Log.LogError("Exception when completing action for " + msg.MessageId);
                            Log.LogError(e.ToString());
                            cloudExceptionCallback(msg);
                            msg.Abandon();
                        }
                    });
                    // If we're here, we've successfully initialized everything and can break out of the retry loop.
                    break;
                }
                catch (MessagingEntityAlreadyExistsException e)
                {
                    Thread.Sleep(60000);
                    if (e.IsTransient && retriesLeft > 0)
                    {
                        retriesLeft--;
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (Exception e)
                {
                    // We can fix the three exceptions below by using retry logic. But there's no point retrying for other exceptions.
                    if ((e is TimeoutException || e is ServerBusyException || e is MessagingCommunicationException) && retriesLeft > 0)
                    {
                        retriesLeft--;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Пример #2
0
        public override bool OnStart()
        {
            try
            {
                // Set the maximum number of concurrent connections .
                System.Net.ServicePointManager.DefaultConnectionLimit = 12;

                // Set options for blob storage.
                options = new BlobRequestOptions();
                options.ServerTimeout = TimeSpan.FromMinutes(10);
                options.RetryPolicy   = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);

                // Get the connection strings.
                string assemblyDir         = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string cloudConfigPath     = Path.Combine(assemblyDir, CloudConstants.CloudConfigXmlFileName);
                string cloudConfigContents = Utility.ReadFile(cloudConfigPath);
                string schemaPath          = Path.Combine(assemblyDir, CloudConstants.CloudConfigXsdFileName);
                using (var c = new StringReader(cloudConfigContents))
                {
                    if (!Utility.ValidateXmlFile(schemaPath, c))
                    {
                        Trace.TraceError("Could not load cloud config from file: " + cloudConfigPath);
                        return(false);
                    }
                }
                var serializer = new XmlSerializer(typeof(SMVCloudConfig));
                using (var reader = new StringReader(cloudConfigContents))
                {
                    cloudConfig = (SMVCloudConfig)serializer.Deserialize(reader);
                }

                bool done = false;
                while (!done)
                {
                    try
                    {
                        var storageAccount = CloudStorageAccount.Parse(cloudConfig.StorageConnectionString.value);

                        // Setup queue storage.
                        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                        queueClient.DefaultRequestOptions             = new QueueRequestOptions();
                        queueClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval),
                                                                                             CloudConstants.MaxRetries);
                        inputQueue = queueClient.GetQueueReference(CloudConstants.InputQueueName);

                        // Setup blob storage.
                        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                        blobClient.DefaultRequestOptions             = new BlobRequestOptions();
                        blobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval),
                                                                                            CloudConstants.MaxRetries);
                        jobsContainer     = blobClient.GetContainerReference(CloudConstants.InputBlobContainerName);
                        resultsContainer  = blobClient.GetContainerReference(CloudConstants.OutputBlobContainerName);
                        versionsContainer = blobClient.GetContainerReference(CloudConstants.VersionsContainerName);

                        // Setup table storage.
                        var tableStorage = storageAccount.CreateCloudTableClient();
                        tableStorage.DefaultRequestOptions             = new TableRequestOptions();
                        tableStorage.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);
                        CloudTable table = tableStorage.GetTableReference(CloudConstants.TableName);
                        tableDataSource = new ActionsTableDataSource(table);

                        // Setup the service bus topic.
                        var namespaceManager = NamespaceManager.CreateFromConnectionString(cloudConfig.ServiceBusConnectionString.value);
                        if (!namespaceManager.TopicExists(CloudConstants.ResultsTopicName))
                        {
                            namespaceManager.CreateTopic(CloudConstants.ResultsTopicName);
                        }

                        done = true;
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError("Failure trying to connect to Azure storage: " + e.ToString());
                    }
                }

                // Get paths to important directories in the file system. Remove the trailing slash from the paths.
                LocalResource localResource = RoleEnvironment.GetLocalResource(CloudConstants.SmvWorkingDirectoryResourceName);
                workingDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);

                localResource    = RoleEnvironment.GetLocalResource(CloudConstants.SmvResultsDirectoryResourceName);
                resultsDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);

                localResource        = RoleEnvironment.GetLocalResource(CloudConstants.SmvDirectoryResourceName);
                smvVersionsDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);
            }
            catch (Exception e)
            {
                Trace.TraceError("Exception while running OnStart(): " + e.ToString());
                return(false);
            }

            Utility.result = null;

            return(base.OnStart());
        }