Exemplo n.º 1
0
        private async Task SyncTriggers(Site functionApp)
        {
            await RetryHelper.Retry(async() =>
            {
                if (functionApp.IsDynamic)
                {
                    ColoredConsole.WriteLine("Syncing triggers...");
                    HttpResponseMessage response = null;
                    if (functionApp.IsLinux)
                    {
                        response = await AzureHelper.SyncTriggers(functionApp, AccessToken);
                    }
                    else
                    {
                        using (var client = GetRemoteZipClient(new Uri($"https://{functionApp.ScmUri}")))
                        {
                            response = await client.PostAsync("api/functions/synctriggers", content: null);
                        }
                    }

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new CliException($"Error calling sync triggers ({response.StatusCode}).");
                    }
                }
            }, retryCount : 5);
        }
Exemplo n.º 2
0
        public bool Handle(TMessage message)
        {
            try
            {
                // 执行DoHandle可能会发生异常,如果是我们特定的异常则进行重试操作
                // 否则直接抛出异常
                var ret = RetryHelper.Retry(() => DoHandle(message), Idempotent ? _maxRetry : 1, _backoffMs, p =>
                {
                    var ex = p as Exception <LightMessagerExceptionArgs>;
                    if (ex != null)
                    {
                        return(true);
                    }

                    return(false);
                });

                if (ret)
                {
                    MarkConsumed(message);
                }
                return(ret);
            }
            catch (Exception ex)
            {
                _logger.Debug("未知异常:" + ex.Message + ";堆栈:" + ex.StackTrace);
                // 说明:_maxRetry次之后还需要判该条消息requeue次数是否超过允
                // 许的最大值:如果是,不再做任何进一步尝试了,log一波;否则
                // 设置NeedRequeue为true准备重新入队列
                DoRequeue(message);
            }
            return(false);
        }
Exemplo n.º 3
0
        public static async Task InitApiApps(this ResourceGroup resourceGroup)
        {
            if (!resourceGroup.Tags.ContainsKey(Constants.CommonApiAppsDeployed) ||
                !resourceGroup.Tags[Constants.CommonApiAppsDeployed].Equals(Constants.CommonApiAppsDeployedVersion))
            {
                var csmTemplateString = string.Empty;

                using (var reader = new StreamReader(SimpleSettings.CommonApiAppsCsmTemplatePath))
                {
                    csmTemplateString = await reader.ReadToEndAsync();
                }

                var gatewayName = resourceGroup.Gateways.Count() != 0
                    ? resourceGroup.Gateways.Select(s => s.GatewayName).First()
                    : Guid.NewGuid().ToString().Replace("-", "");
                csmTemplateString = csmTemplateString.Replace("{{gatewayName}}", gatewayName);

                var deployment = new CsmDeployment
                {
                    DeploymentName    = resourceGroup.ResourceUniqueId,
                    SubscriptionId    = resourceGroup.SubscriptionId,
                    ResourceGroupName = resourceGroup.ResourceGroupName,
                    CsmTemplate       = JsonConvert.DeserializeObject <JToken>(csmTemplateString),
                };

                await RetryHelper.Retry(() => deployment.Deploy(block: true), 3);

                resourceGroup.Tags[Constants.CommonApiAppsDeployed] = Constants.CommonApiAppsDeployedVersion;
                await resourceGroup.Update();

                await resourceGroup.Load();
            }
        }
Exemplo n.º 4
0
        public async Task <TestRunStatistic> GetTestRunSummaryAsync(string projectName,
                                                                    int testRunId,
                                                                    bool allowRetry,
                                                                    CancellationToken cancellationToken)
        {
            Trace.Entering();

            TestRunStatistic testRunStatistic = null;

            _executionContext.Debug(string.Format(CultureInfo.CurrentCulture, $"TestRunPublisher.GetTestRunSummaryAsync: Getting test run summary with run id: {testRunId} using new summary api. Allow Retry: {allowRetry}"));

            if (allowRetry)
            {
                var retryHelper = new RetryHelper(_executionContext, MaxRetries);

                testRunStatistic = await retryHelper.Retry(() => GetTestRunSummaryByOutcome(projectName, testRunId, cancellationToken),
                                                           (retryCounter) => GetTimeIntervalDelay(retryCounter),
                                                           (exception) => IsRetriableException(exception));
            }
            else
            {
                testRunStatistic = await GetTestRunSummaryByOutcome(projectName, testRunId, cancellationToken);
            }

            Trace.Leaving();

            return(testRunStatistic);
        }
Exemplo n.º 5
0
        public void Handle(TMessage message)
        {
            try
            {
                // 执行DoHandle可能会发生异常,如果是我们特定的异常则进行重试操作
                // 否则直接抛出异常
                RetryHelper.Retry(() => DoHandle(message), _maxRetry, _backoffMs, p =>
                {
                    var ex = p as Exception <LightMessagerExceptionArgs>;
                    if (ex != null)
                    {
                        return(true);
                    }

                    return(false);
                });
            }
            catch (Exception ex)
            {
                _logger.Debug("未知异常:" + ex.Message + ";堆栈:" + ex.StackTrace);
                // _maxRetry次之后还有问题,先判该条消息requeue次数是否超过
                // 允许的最大值:如果是,不再做任何进一步尝试了,log一波;否则
                // 设置NeedRequeue为true准备重新入队列定
                message.NeedRequeue = NeedRequeue(message.MsgId);
            }
        }
Exemplo n.º 6
0
        public async Task <T> ExecuteAsync <T>(Func <Task <T> > func)
        {
            if (_retryMechanismOptions == null && _circuitBreakerOptions == null)
            {
                throw new ArgumentNullException("You must use Retry or CircuitBreaker method!");
            }

            try
            {
                if (_retryMechanismOptions != null)
                {
                    RetryHelper retryHelper = new RetryHelper();

                    return(await retryHelper.Retry(func, _retryMechanismOptions));
                }

                return(await func.Invoke());
            }
            catch
            {
                if (_circuitBreakerOptions != null)
                {
                    CircuitBreakerHelper circuitBreakerHelper = new CircuitBreakerHelper(_circuitBreakerOptions, _circuitBreakerStateStore);

                    return(await circuitBreakerHelper.ExecuteAsync(func));
                }

                throw;
            }
        }
Exemplo n.º 7
0
        private async Task <string> UploadZipToStorage(Stream zip, IDictionary <string, string> appSettings)
        {
            return(await RetryHelper.Retry(async() =>
            {
                // Setting position to zero, in case we retry, we want to reset the stream
                zip.Position = 0;
                var zipMD5 = SecurityHelpers.CalculateMd5(zip);

                const string containerName = "function-releases";
                const string blobNameFormat = "{0}-{1}.zip";

                CloudBlobContainer blobContainer = null;

                try
                {
                    var storageConnection = appSettings["AzureWebJobsStorage"];
                    var storageAccount = CloudStorageAccount.Parse(storageConnection);
                    var blobClient = storageAccount.CreateCloudBlobClient();
                    blobContainer = blobClient.GetContainerReference(containerName);
                    await blobContainer.CreateIfNotExistsAsync();
                }
                catch (Exception ex)
                {
                    if (StaticSettings.IsDebug)
                    {
                        ColoredConsole.Error.WriteLine(ErrorColor(ex.ToString()));
                    }
                    throw new CliException($"Error creating a Blob container reference. Please make sure your connection string in \"AzureWebJobsStorage\" is valid");
                }

                var releaseName = Guid.NewGuid().ToString();
                var blob = blobContainer.GetBlockBlobReference(string.Format(blobNameFormat, DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmss"), releaseName));
                using (var progress = new StorageProgressBar($"Uploading {Utilities.BytesToHumanReadable(zip.Length)}", zip.Length))
                {
                    await blob.UploadFromStreamAsync(zip,
                                                     AccessCondition.GenerateEmptyCondition(),
                                                     new BlobRequestOptions(),
                                                     new OperationContext(),
                                                     progress,
                                                     new CancellationToken());
                }

                var cloudMd5 = blob.Properties.ContentMD5;

                if (!cloudMd5.Equals(zipMD5))
                {
                    throw new CliException("Upload failed: Integrity error: MD5 hash mismatch between the local copy and the uploaded copy.");
                }

                var sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
                sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddYears(10);
                sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

                var blobToken = blob.GetSharedAccessSignature(sasConstraints);

                return blob.Uri + blobToken;
            }, 3, TimeSpan.FromSeconds(1), displayError : true));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 发送短信(重试3次)
        /// </summary>
        /// <param name="phoneNos">电话号码集合,逗号分隔</param>
        /// <param name="templateId">模版Id</param>
        /// <param name="content">短信内容</param>
        /// <returns>是否发送成功</returns>

        public bool Send(string phoneNos, int templateId = 0, params string[] content)
        {
            return(RetryHelper.Retry(() =>
            {
                Dictionary <string, object> result = _api.SendTemplateSMS(phoneNos, templateId.ToString(), content);
                return (string)result["statusCode"] == "000000";
            }, 4));
        }
 public async Task CheckSiteDeploymentStatusAsync(string path)
 {
     await RetryHelper.Retry(async() =>
     {
         {
             await CheckSiteDeploymentStatus(path);
         }
     }, _retryCount, delay : 5000);
 }
        public override async Task RunAsync()
        {
            var functionAppRoot = ScriptHostHelpers.GetFunctionAppRootDirectory(Environment.CurrentDirectory);

            ColoredConsole.WriteLine(WarningColor($"Publish {functionAppRoot} contents to an Azure Function App. Locally deleted files are not removed from destination."));
            ColoredConsole.WriteLine("Getting site publishing info...");
            var functionApp = await _armManager.GetFunctionAppAsync(FunctionAppName);

            if (PublishLocalSettingsOnly)
            {
                var isSuccessful = await PublishAppSettings(functionApp);

                if (!isSuccessful)
                {
                    return;
                }
            }
            else
            {
                await RetryHelper.Retry(async() =>
                {
                    using (var client = await GetRemoteZipClient(new Uri($"https://{functionApp.ScmUri}")))
                        using (var request = new HttpRequestMessage(HttpMethod.Put, new Uri("api/zip/site/wwwroot", UriKind.Relative)))
                        {
                            request.Headers.IfMatch.Add(EntityTagHeaderValue.Any);

                            ColoredConsole.WriteLine("Creating archive for current directory...");

                            request.Content = CreateZip(functionAppRoot);

                            ColoredConsole.WriteLine("Uploading archive...");
                            var response = await client.SendAsync(request);
                            if (!response.IsSuccessStatusCode)
                            {
                                throw new CliException($"Error uploading archive ({response.StatusCode}).");
                            }

                            response = await client.PostAsync("api/functions/synctriggers", content: null);
                            if (!response.IsSuccessStatusCode)
                            {
                                throw new CliException($"Error calling sync triggers ({response.StatusCode}).");
                            }

                            if (PublishLocalSettings)
                            {
                                var isSuccessful = await PublishAppSettings(functionApp);
                                if (!isSuccessful)
                                {
                                    return;
                                }
                            }

                            ColoredConsole.WriteLine("Upload completed successfully.");
                        }
                }, 2);
            }
        }
Exemplo n.º 11
0
 public async Task CheckTimeStampMetaDataDeploymentStatusAsync(string path)
 {
     await RetryHelper.Retry(async() =>
     {
         {
             await CheckTimeStampMetaDataStatus(path);
         }
     }, _retryCount, delay : 1500);
 }
Exemplo n.º 12
0
 public void TestMethod1()
 {
     var time   = DateTime.Now;
     var tesult = RetryHelper.Retry(() =>
     {
         var a = time;
         return(false);
     }, 2);
 }
 public async Task WaitUntilDeleted(
     string name,
     int numOfRetries     = 5,
     int retryIntervalSec = 10)
 {
     await RetryHelper.Retry(async() =>
     {
         await Delete(name);
     }, retryCount : numOfRetries, retryDelay : TimeSpan.FromSeconds(retryIntervalSec));
 }
Exemplo n.º 14
0
 public async Task PutZipFileAsync(string path, string localZipPath)
 {
     await RetryHelper.Retry(async() =>
     {
         using (var stream = File.OpenRead(localZipPath))
         {
             await PutZipStreamAsync(path, stream);
         }
     }, _retryCount);
 }
Exemplo n.º 15
0
 /// <summary>
 /// Get rid of focus from currently focused element
 /// </summary>
 /// <param name="driver">Selenium webdriver</param>
 public static void Blur(this RemoteWebDriver driver)
 {
     driver.SwitchTo().DefaultContent();
     RetryHelper.Retry(3, () =>
     {
         ExecuteBlur(driver);
         Thread.Sleep(500);
         return(IsThereElementWithFocus(driver) == false);
     });
 }
 public async Task WaitUntilCreated(
     string name,
     int numOfRetries     = 5,
     int retryIntervalSec = 10)
 {
     await RetryHelper.Retry(async() =>
     {
         HttpResponseMessage response = await Get(name);
         response.EnsureSuccessStatusCode();
     }, retryCount : numOfRetries, retryDelay : TimeSpan.FromSeconds(retryIntervalSec));
 }
Exemplo n.º 17
0
        public void TestRetryFinishesIfSuccessful()
        {
            Func <TimeSpan> retryStrategy = () => {
                return(TimeSpan.FromMilliseconds(1));
            };

            var target = new RetryTarget();

            RetryHelper.Retry(() => target.TargetActionThrows5Times(), retryStrategy, 20);
            Assert.Equal(6, target.Calls);
        }
Exemplo n.º 18
0
 public async Task CheckSiteDeploymentStatusAsync(string path)
 {
     var trial = 1;
     await RetryHelper.Retry(async() =>
     {
         {
             SimpleTrace.TraceInformation($"Checking if site was deployed {trial++} of {_retryCount} for -> {path}");
             await CheckSiteDeploymentStatus(path);
         }
     }, _retryCount, delay : 5000);
 }
Exemplo n.º 19
0
 public bool Send(string phoneNos, int templateId = 0, params string[] content)
 {
     return(RetryHelper.Retry(() =>
     {
         var cont = "#code#=" + content.ExpandAndToString();
         var value = System.Web.HttpUtility.UrlEncode(cont, Encoding.UTF8);
         var url = "http://v.juhe.cn/sms/send?mobile=" + phoneNos + "&tpl_id=" + templateId + "&tpl_value=" + value + "&key=" + Key;
         var response = GetRequestHelper.GetWebRequest(url, RequestMethod.Get, encoding: Encoding.UTF8);
         var result = response.FromJsonString <JObject>();
         return result["reason"].ToString() == "发送成功";
     }, 4));
 }
Exemplo n.º 20
0
 public async Task PutZipFileAsync(string path, string zipUrl)
 {
     await RetryHelper.Retry(async() =>
     {
         var trial = 0;
         SimpleTrace.TraceInformation($"Site Zip PUT started trial ({++trial}/{_retryCount}): for {zipUrl}->{ServiceUrl}");
         using (var stream = await GetHttpStream(zipUrl))
         {
             await PutZipStreamAsync(path, stream);
         }
     }, _retryCount);
 }
Exemplo n.º 21
0
 public async Task <Uri> PostZipFileAsync(string path, string zipUrl)
 {
     return(await RetryHelper.Retry(async() =>
     {
         var trial = 0;
         SimpleTrace.TraceInformation($"Site POST Zip Deploy started trial ({++trial}/{_retryCount}): for {zipUrl}->{ServiceUrl}");
         using (var stream = await GetHttpStream(zipUrl))
         {
             return await PostZipStreamAsync(path, stream);
         }
     }, _retryCount));
 }
Exemplo n.º 22
0
 public async Task <Uri> PostZipFileAsync(string path, string localZipPath)
 {
     return(await RetryHelper.Retry(async() =>
     {
         var trial = 0;
         SimpleTrace.TraceInformation($"Site POST Zip Deploy started trial ({++trial}/{_retryCount}): for {localZipPath}->{ServiceUrl}-> size({new FileInfo(localZipPath).Length})");
         using (var stream = File.OpenRead(localZipPath))
         {
             return await PostZipStreamAsync(path, stream);
         }
     }, _retryCount));
 }
 public async Task WaitUntilScmSiteAvailable(
     string name,
     int numOfRetries     = 5,
     int retryIntervalSec = 10)
 {
     Uri uri = new Uri($"https://{name}.scm.azurewebsites.net");
     await RetryHelper.Retry(async() =>
     {
         HttpResponseMessage response = await ArmClient.HttpInvoke("GET", uri, AccessToken);
         response.EnsureSuccessStatusCode();
     }, retryCount : numOfRetries, retryDelay : TimeSpan.FromSeconds(retryIntervalSec));
 }
Exemplo n.º 24
0
        public void Retry_Fails(int maxRetryCount, int presetFailureCount)
        {
            var testSubject = new RetryTestSubject(presetFailureCount);

            var result = RetryHelper.Retry(
                () => testSubject.IncrementTryCountAndThrow(),
                maxRetryCount,
                e => testSubject.OnFailure(e));

            result.AsT1.Should().NotBeNull();
            testSubject.TryCount.Should().Be(maxRetryCount + 1);
            testSubject.FailureCount.Should().Be(maxRetryCount);
        }
Exemplo n.º 25
0
        public void Retry_Succeeds(int maxRetryCount, int presetFailureCount, int expectedTryCount)
        {
            var testSubject = new RetryTestSubject(presetFailureCount);

            var result = RetryHelper.Retry(
                () => testSubject.IncrementTryCountAndThrow(),
                maxRetryCount,
                e => testSubject.OnFailure(e));

            result.AsT0.Should().Be(expectedTryCount);
            testSubject.TryCount.Should().Be(expectedTryCount);
            testSubject.FailureCount.Should().Be(presetFailureCount);
        }
Exemplo n.º 26
0
        public async Task Retry_Fails()
        {
            try
            {
                await RetryHelper.Retry(3, new Logger("RETRY", true), () => throw new Exception());

                Assert.Fail("Should throw exception");
            }
            catch
            {
                // ok
            }
        }
Exemplo n.º 27
0
        public async Task <ListKeysResponse> WaitUntilListKeys(
            string name,
            int numOfRetries     = 5,
            int retryIntervalSec = 10)
        {
            ListKeysResponse result = null;
            await RetryHelper.Retry(async() =>
            {
                result = await ListKeys(name);
            }, retryCount : numOfRetries, retryDelay : TimeSpan.FromSeconds(retryIntervalSec));

            return(result);
        }
Exemplo n.º 28
0
 public async Task Put(string remotePath, HttpContent httpContent)
 {
     await RetryHelper.Retry(async() =>
     {
         using (var request = new HttpRequestMessage())
         {
             request.Method     = HttpMethod.Put;
             request.RequestUri = new Uri(remotePath, UriKind.Relative);
             request.Headers.IfMatch.Add(EntityTagHeaderValue.Any);
             request.Content = httpContent;
             await Client.SendAsync(request);
         }
     }, _retryCount);
 }
        private IEnumerable <TypeDependency> GetIllegalTypeDependencies(IEnumerable <string> sourceFilePaths, IEnumerable <string> referencedAssemblyPaths)
        {
            var retryTimeSpans = ConfigProvider.Config.AnalyzerServiceCallRetryTimeSpans;
            var retryCount     = 0;

            var retryResult = RetryHelper.Retry(
                () => InvokeRemoteAnalyzer(sourceFilePaths, referencedAssemblyPaths),
                retryTimeSpans.Length,
                e => ActivateServerAndWaitBeforeRetry(e, retryCount++, retryTimeSpans));

            return(retryResult.Match(
                       UnwrapTraceMessages,
                       OnAllRetriesFailed));
        }
Exemplo n.º 30
0
 private async Task SyncTriggers(Site functionApp)
 {
     await RetryHelper.Retry(async() =>
     {
         ColoredConsole.WriteLine("Syncing triggers...");
         HttpResponseMessage response = null;
         // This SyncTriggers function calls the endpoint for linux syncTriggers
         response = await AzureHelper.SyncTriggers(functionApp, AccessToken, ManagementURL);
         if (!response.IsSuccessStatusCode)
         {
             throw new CliException($"Error calling sync triggers ({response.StatusCode}).");
         }
     }, retryCount : 5);
 }