internal static RetryDelegate GetRetryDelegate()
        {
            var retriesAttempted = 0;
            var retryPolicy      = new Microsoft.WindowsAzure.Storage.RetryPolicies.ExponentialRetry(DefaultBackoffForRetry, DefaultNumberOfTimesToRetry);

            return(async(statusCode, ex, retry) =>
            {
                TimeSpan retryDelay;
                bool shouldRetry = retryPolicy.ShouldRetry(retriesAttempted++, statusCode, ex, out retryDelay, null);
                if (!shouldRetry)
                {
                    throw new Exception("After " + retriesAttempted + "attempts finding the resource timed out");
                }
                await Task.Delay(retryDelay);
                await retry();
            });
        }
Beispiel #2
0
        protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                /// On start up, CreateIfNotExists CloudBreadLog table on Azure Table Storage
                /// On start up, CreateIfNotExists messagestolog table on Azure Queue Service
                if (globalVal.StorageConnectionString != "")
                {
                    /// this table is used for CloudBread game log saving
                    /// Azure Storage connection retry policy
                    var retryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
                    tableClient.DefaultRequestOptions.RetryPolicy = retryPolicy;
                    var cloudTable = tableClient.GetTableReference("CloudBreadLog");
                    cloudTable.CreateIfNotExists();
                    cloudTable = tableClient.GetTableReference("CloudBreadErrorLog");
                    cloudTable.CreateIfNotExists();

                    /// this queue is used for CloudBread queue method game log saving
                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                    queueClient.DefaultRequestOptions.RetryPolicy = retryPolicy;
                    CloudQueue queue = queueClient.GetQueueReference("messagestolog");      /// must be lowercase
                    queue.CreateIfNotExists();

                    /// this queue is used for CloudBread queue method game log saving
                    queue = queueClient.GetQueueReference("cloudbread-batch");      /// must be lowercase
                    queue.CreateIfNotExists();

                }

                // Regarding to configuration, check startup fill or not
                if (globalVal.CloudBreadFillRedisRankSetOnStartup)
                {
                    // execute redis rank fill task
                    CBRedis.FillAllRankFromDB();
                }

            }
            catch (System.Exception ex)
            {

                throw ex;
            }
        }
Beispiel #3
0
        /**
        * @brief Save log task processor. \n
        */
        public static bool RunLog(CBLoggers message)
        {
            if (globalVal.CloudBreadLoggerSetting != "")
            {
                if (string.IsNullOrEmpty(message.memberID))
                {
                    message.memberID = "";      /// in case of non-member triggered job
                }

                /// critical error case, save in ATS CloudBreadErrorLog
                if (message.Level.ToUpper() == "ERROR")
                {
                    try
                    {
                        /// Save error log on Azure Table Storage
                        {
                            /// Azure Table Storage connection retry policy
                            var tableStorageRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                            CloudStorageAccount storageAccountT = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                            CloudTableClient tableClient = storageAccountT.CreateCloudTableClient();
                            tableClient.DefaultRequestOptions.RetryPolicy = tableStorageRetryPolicy;
                            CloudTable table = tableClient.GetTableReference("CloudBreadErrorLog");
                            CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());
                            Message.jobID = message.jobID;
                            Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                            Message.Thread = message.Thread;
                            Message.Level = message.Level;
                            Message.Logger = message.Logger;
                            Message.Message = message.Message;
                            Message.Exception = message.Exception;
                            TableOperation insertOperation = TableOperation.Insert(Message);
                            table.Execute(insertOperation);
                        }
                    }
                    catch (Exception)
                    {
                        /// Catch fail to log on database. Most case database connection or login fail issue.
                        throw;
                    }
                }
                else
                {
                    /// Regarding to web.config logger settting, save logs on specific storage
                    try
                    {
                        switch (globalVal.CloudBreadLoggerSetting)
                        {
                            case "SQL":
                                /// Save log on SQL
                                string strQuery = string.Format("insert into dbo.CloudBreadLog(memberid, jobID, [Thread], [Level], [Logger], [Message], [Exception]) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                message.memberID,
                                message.jobID,
                                message.Thread,
                                message.Level,
                                message.Logger,
                                message.Message,
                                message.Exception
                                );

                                /// Database connection retry policy
                                RetryPolicy retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(globalVal.conRetryCount, TimeSpan.FromSeconds(globalVal.conRetryFromSeconds));
                                SqlConnection connection = new SqlConnection(globalVal.DBConnectionString);
                                {
                                    connection.OpenWithRetry(retryPolicy);
                                    SqlCommand command = new SqlCommand(strQuery, connection);
                                    int rowcount = command.ExecuteNonQueryWithRetry(retryPolicy);
                                    connection.Close();
                                    break;
                                }

                            case "ATS":
                                /// Save log on Azure Table Storage
                                {
                                    /// Azure Table Storage connection retry policy
                                    var tableStorageRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                                    CloudStorageAccount storageAccountT = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                                    CloudTableClient tableClient = storageAccountT.CreateCloudTableClient();
                                    tableClient.DefaultRequestOptions.RetryPolicy = tableStorageRetryPolicy;
                                    CloudTable table = tableClient.GetTableReference("CloudBreadLog");
                                    CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());
                                    Message.jobID = message.jobID;
                                    Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                                    //Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
                                    Message.Thread = message.Thread;
                                    Message.Level = message.Level;
                                    Message.Logger = message.Logger;
                                    Message.Message = message.Message;
                                    Message.Exception = message.Exception;
                                    TableOperation insertOperation = TableOperation.Insert(Message);
                                    table.Execute(insertOperation);
                                    break;
                                }

                            case "AQS":
                                /// Save log on Azure Queue Storage
                                {
                                    /// Azure Queue Storage connection retry policy
                                    var queueStorageRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                                    queueClient.DefaultRequestOptions.RetryPolicy = queueStorageRetryPolicy;
                                    CloudQueue queue = queueClient.GetQueueReference("messagestolog");      /// must be lower case
                                    CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());
                                    Message.jobID = message.jobID;
                                    Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                                    Message.Thread = message.Thread;
                                    Message.Level = message.Level;
                                    Message.Logger = message.Logger;
                                    Message.Message = message.Message;
                                    Message.Exception = message.Exception;
                                    CloudQueueMessage Qmessage = new CloudQueueMessage(JsonConvert.SerializeObject(Message));
                                    queue.AddMessage(Qmessage);
                                    break;
                                }

                            case "redis":
                                /// todolist - save log on Azure Redis Cache
                                /// yyyymmdd:memberid:Controller:GUID
                                {
                                    string redisKey = "";
                                    string redisVal = "";
                                    message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"); ;
                                    redisKey = DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmm") + ":" + message.memberID + ":" + message.Logger + ":" + Guid.NewGuid().ToString();   // guid - too long key size
                                    redisVal = JsonConvert.SerializeObject(message);
                                    CBRedis.saveRedisLog(redisKey, redisVal, globalVal.CloudBreadGameLogExpTimeDays);
                                }
                                break;

                            //case "DocDB":
                            //    /// @todo save log data on Azure DocumentDB
                            //    break;

                            default:
                                /// case do nothing
                                break;
                        }
                    }
                    catch (Exception)
                    {
                        /// catch save log error here.
                        throw;
                    }
                }
            }

            return true;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TransferRetryPolicy"/> class.
 /// </summary>
 /// <param name="retryPolicy">ExponentialRetry object.</param>
 /// <param name="maxAttemptsInOtherError">Max retry count when meets non x-ms error.</param>
 private TransferRetryPolicy(ExponentialRetry retryPolicy, int maxAttemptsInOtherError)
 {
     this.retryPolicy = retryPolicy;
     this.maxAttemptsOtherError = maxAttemptsInOtherError;
 }
            /// <summary>
            /// Initializes a new instance of the <see cref="TransferRetryPolicy"/> class.
            /// </summary>
            /// <param name="deltaBackoff">Back-off in ExponentialRetry retry policy.</param>
            /// <param name="maxAttemptsXMsError">Max retry count when meets x-ms error.</param>
            /// <param name="maxAttemptsOtherError">Max retry count when meets non x-ms error.</param>
            public TransferRetryPolicy(TimeSpan deltaBackoff, int maxAttemptsXMsError, int maxAttemptsOtherError)
            {
                Debug.Assert(
                    maxAttemptsXMsError >= maxAttemptsOtherError,
                    "We should retry more times when meets x-ms errors than the other errors.");

                this.retryPolicy = new ExponentialRetry(deltaBackoff, maxAttemptsXMsError);
                this.maxAttemptsOtherError = maxAttemptsOtherError;
            }
        public void VerifyBackoffTimeOverflow()
        {
            ExponentialRetry exponentialRetry = new ExponentialRetry(TimeSpan.FromSeconds(4), 100000);
            VerifyBackoffTimeOverflow(exponentialRetry, 100000);

            LinearRetry linearRetry = new LinearRetry(TimeSpan.FromSeconds(4), 100000);
            VerifyBackoffTimeOverflow(linearRetry, 100000);
        }
 private void Initialize(TimeSpan deltaBackoff, int maxAttempts)
 {
     this.deltaBackoff = deltaBackoff;
     this.maximumAttempts = maxAttempts;
     retry = new ExponentialRetry(this.deltaBackoff, this.maximumAttempts);
 }