public void TestRetry() { RetryUtil tu = new RetryUtil(); tu.NumRetries = 3; try { tu.RetryFunc<object, object, Exception>(x => { throw new Exception("abc"); }, new object(), x => x.Message == "abc"); } catch (Exception) { Assert.AreEqual(3, tu.RetryExecuted); } tu = new RetryUtil(); tu.RetryDelay = new TimeSpan(0, 0, 1); try { tu.RetryFunc<object, object, Exception>(x => { throw new Exception("abc"); }, new object(), x => x.Message == "abc"); } catch (Exception) { Assert.AreEqual(1, tu.RetryExecuted); } }
public void TestRetry() { RetryUtil tu = new RetryUtil(); tu.NumRetries = 3; try { tu.RetryFunc <object, object, Exception>(x => { throw new Exception("abc"); }, new object(), x => x.Message == "abc"); } catch (Exception) { Assert.AreEqual(3, tu.RetryExecuted); } tu = new RetryUtil(); tu.RetryDelay = new TimeSpan(0, 0, 1); try { tu.RetryFunc <object, object, Exception>(x => { throw new Exception("abc"); }, new object(), x => x.Message == "abc"); } catch (Exception) { Assert.AreEqual(1, tu.RetryExecuted); } }
/// <summary> /// Releases the lock. /// </summary> /// <param name="path">The path to the lock file.</param> protected virtual void RemoveLock(string path) { RetryUtil.Retry(() => { try { if (!File.Exists(path)) { return; } File.Delete(path); _lockStatus[path] = false; Debug.WriteLine("[Thread: {0}] Deleted lock: {1}", Thread.CurrentThread.ManagedThreadId, path); } catch (IOException) { Debug.WriteLine("[Thread: {0}] Error creating lock: {1}", Thread.CurrentThread.ManagedThreadId, path); throw; } }, 5); }
public void Listen() { Task.Factory.StartNew(() => { using (IRedisClient client = _redisClientsManager.GetReadOnlyClient()) { using (IRedisSubscription subscription = client.CreateSubscription()) { subscription.OnMessage = (channel, msg) => { if (msg == "ping" && Ping != null) { Ping(this, EventArgs.Empty); } string[] parts = msg.Split(':'); if (parts.Length != 2) { return; } NewError(parts[0], parts[1]); }; RetryUtil.Retry(() => subscription.SubscribeToChannels(NotifySignalRAction.NOTIFICATION_CHANNEL_KEY)); } } }); }
private List <string> GetDownloadFilePath() { List <string> result = new List <string>(); HtmlDocument htc = null; string baseUrl = @"http://www.tfex.co.th"; string downloadFolder = Path.Combine(configObj.FolderPath, "Download"); try { RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate { htc = WebClientUtil.GetHtmlDocument(sourceUrl, 3000); }); if (htc == null) { throw new Exception(string.Format("open website: {0} error.", sourceUrl)); } HtmlNodeCollection trs = htc.DocumentNode.SelectNodes(".//table")[7].SelectNodes(".//tr"); List <string> urlFoundAll = GetUrlFoundAll(trs); if (!Directory.Exists(downloadFolder)) { Directory.CreateDirectory(downloadFolder); } string tdNewSeries = GetUrlFoundOne(urlFoundAll); if ((tdNewSeries + "").Trim().Length == 0) { return(null); } string dexsrp = Path.Combine(downloadFolder, string.Format("dexsrp{0}.txt", DateTimeConvert(configObj.DateOfSource, "yyyy-MM-dd", "yyyyMMdd"))); if (File.Exists(dexsrp)) { File.Delete(dexsrp); } RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate { WebClientUtil.DownloadFile(string.Format("{0}{1}", baseUrl, tdNewSeries), 30000, dexsrp); }); if (File.Exists(dexsrp)) { result.Add(dexsrp); } } catch (Exception ex) { string msg = string.Format("\r\n ClassName: {0}\r\n MethodName: {1}\r\n Message: {2}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message); Logger.Log(msg, Logger.LogType.Error); } return(result); }
/// <summary> /// Create an AppHost with embedded configuration of app binary location /// </summary> /// <param name="appHostSourceFilePath">The path of Apphost template, which has the place holder</param> /// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param> /// <param name="appBinaryFilePath">Full path to app binary or relative path to the result apphost file</param> /// <param name="windowsGraphicalUserInterface">Specify whether to set the subsystem to GUI. Only valid for PE apphosts.</param> /// <param name="assemblyToCopyResorcesFrom">Path to the intermediate assembly, used for copying resources to PE apphosts.</param> public static void CreateAppHost( string appHostSourceFilePath, string appHostDestinationFilePath, string appBinaryFilePath, bool windowsGraphicalUserInterface = false, string assemblyToCopyResorcesFrom = null) { var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryFilePath); if (bytesToWrite.Length > 1024) { throw new AppNameTooLongException(appBinaryFilePath); } BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath); bool appHostIsPEImage = false; void RewriteAppHost() { // Re-write the destination apphost with the proper contents. using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) { using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) { BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); appHostIsPEImage = BinaryUtils.IsPEImage(accessor); if (windowsGraphicalUserInterface) { if (!appHostIsPEImage) { throw new AppHostNotPEFileException(); } BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); } } } } void UpdateResources() { if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) { if (ResourceUpdater.IsSupportedOS()) { // Copy resources from managed dll to the apphost new ResourceUpdater(appHostDestinationFilePath) .AddResourcesFromPEImage(assemblyToCopyResorcesFrom) .Update(); } else { throw new AppHostCustomizationUnsupportedOSException(); } } } try { RetryUtil.RetryOnIOError(RewriteAppHost); RetryUtil.RetryOnWin32Error(UpdateResources); // Memory-mapped write does not updating last write time RetryUtil.RetryOnIOError(() => File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow)); } catch (Exception ex) { // Delete the destination file so we don't leave an unmodified apphost try { File.Delete(appHostDestinationFilePath); } catch (Exception failedToDeleteEx) { throw new AggregateException(ex, failedToDeleteEx); } throw; } }
/// <summary> /// Create an AppHost with embedded configuration of app binary location /// </summary> /// <param name="appHostSourceFilePath">The path of Apphost template, which has the place holder</param> /// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param> /// <param name="appBinaryFilePath">Full path to app binary or relative path to the result apphost file</param> /// <param name="windowsGraphicalUserInterface">Specify whether to set the subsystem to GUI. Only valid for PE apphosts.</param> /// <param name="assemblyToCopyResorcesFrom">Path to the intermediate assembly, used for copying resources to PE apphosts.</param> public static void CreateAppHost( string appHostSourceFilePath, string appHostDestinationFilePath, string appBinaryFilePath, bool windowsGraphicalUserInterface = false, string assemblyToCopyResorcesFrom = null) { var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryFilePath); if (bytesToWrite.Length > 1024) { throw new AppNameTooLongException(appBinaryFilePath); } BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath); bool appHostIsPEImage = false; void RewriteAppHost() { // Re-write the destination apphost with the proper contents. using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) { using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) { BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); appHostIsPEImage = BinaryUtils.IsPEImage(accessor); if (windowsGraphicalUserInterface) { if (!appHostIsPEImage) { throw new AppHostNotPEFileException(); } BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); } } } } void UpdateResources() { if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) { if (ResourceUpdater.IsSupportedOS()) { // Copy resources from managed dll to the apphost new ResourceUpdater(appHostDestinationFilePath) .AddResourcesFromPEImage(assemblyToCopyResorcesFrom) .Update(); } else { throw new AppHostCustomizationUnsupportedOSException(); } } } void RemoveSignatureIfMachO() { MachOUtils.RemoveSignature(appHostDestinationFilePath); } void SetLastWriteTime() { // Memory-mapped write does not updating last write time File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); } try { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var filePermissionOctal = Convert.ToInt32("755", 8); // -rwxr-xr-x const int EINTR = 4; int chmodReturnCode = 0; do { chmodReturnCode = chmod(appHostDestinationFilePath, filePermissionOctal); }while (chmodReturnCode == -1 && Marshal.GetLastWin32Error() == EINTR); if (chmodReturnCode == -1) { throw new Win32Exception(Marshal.GetLastWin32Error(), $"Could not set file permission {filePermissionOctal} for {appHostDestinationFilePath}."); } } RetryUtil.RetryOnIOError(RewriteAppHost); RetryUtil.RetryOnWin32Error(UpdateResources); RetryUtil.RetryOnIOError(RemoveSignatureIfMachO); RetryUtil.RetryOnIOError(SetLastWriteTime); } catch (Exception ex) { // Delete the destination file so we don't leave an unmodified apphost try { File.Delete(appHostDestinationFilePath); } catch (Exception failedToDeleteEx) { throw new AggregateException(ex, failedToDeleteEx); } throw; } }
/// <summary> /// Create an AppHost with embedded configuration of app binary location /// </summary> /// <param name="appHostSourceFilePath">The path of Apphost template, which has the place holder</param> /// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param> /// <param name="appBinaryFilePath">Full path to app binary or relative path to the result apphost file</param> /// <param name="windowsGraphicalUserInterface">Specify whether to set the subsystem to GUI. Only valid for PE apphosts.</param> /// <param name="assemblyToCopyResorcesFrom">Path to the intermediate assembly, used for copying resources to PE apphosts.</param> public static void CreateAppHost( string appHostSourceFilePath, string appHostDestinationFilePath, string appBinaryFilePath, bool windowsGraphicalUserInterface = false, string assemblyToCopyResorcesFrom = null) { var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryFilePath); if (bytesToWrite.Length > 1024) { throw new AppNameTooLongException(appBinaryFilePath); } bool appHostIsPEImage = false; void RewriteAppHost(MemoryMappedViewAccessor accessor) { // Re-write the destination apphost with the proper contents. BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); appHostIsPEImage = PEUtils.IsPEImage(accessor); if (windowsGraphicalUserInterface) { if (!appHostIsPEImage) { throw new AppHostNotPEFileException(); } PEUtils.SetWindowsGraphicalUserInterfaceBit(accessor); } } void UpdateResources() { if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) { if (ResourceUpdater.IsSupportedOS()) { // Copy resources from managed dll to the apphost new ResourceUpdater(appHostDestinationFilePath) .AddResourcesFromPEImage(assemblyToCopyResorcesFrom) .Update(); } else { throw new AppHostCustomizationUnsupportedOSException(); } } } try { RetryUtil.RetryOnIOError(() => { FileStream appHostSourceStream = null; MemoryMappedFile memoryMappedFile = null; MemoryMappedViewAccessor memoryMappedViewAccessor = null; try { // Open the source host file. appHostSourceStream = new FileStream(appHostSourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostSourceStream, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, true); memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite); // Get the size of the source app host to ensure that we don't write extra data to the destination. // On Windows, the size of the view accessor is rounded up to the next page boundary. long sourceAppHostLength = appHostSourceStream.Length; // Transform the host file in-memory. RewriteAppHost(memoryMappedViewAccessor); // Save the transformed host. using (FileStream fileStream = new FileStream(appHostDestinationFilePath, FileMode.Create)) { BinaryUtils.WriteToStream(memoryMappedViewAccessor, fileStream, sourceAppHostLength); // Remove the signature from MachO hosts. if (!appHostIsPEImage) { MachOUtils.RemoveSignature(fileStream); } } } finally { memoryMappedViewAccessor?.Dispose(); memoryMappedFile?.Dispose(); appHostSourceStream?.Dispose(); } }); RetryUtil.RetryOnWin32Error(UpdateResources); if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var filePermissionOctal = Convert.ToInt32("755", 8); // -rwxr-xr-x const int EINTR = 4; int chmodReturnCode = 0; do { chmodReturnCode = chmod(appHostDestinationFilePath, filePermissionOctal); }while (chmodReturnCode == -1 && Marshal.GetLastWin32Error() == EINTR); if (chmodReturnCode == -1) { throw new Win32Exception(Marshal.GetLastWin32Error(), $"Could not set file permission {filePermissionOctal} for {appHostDestinationFilePath}."); } } } catch (Exception ex) { // Delete the destination file so we don't leave an unmodified apphost try { File.Delete(appHostDestinationFilePath); } catch (Exception failedToDeleteEx) { throw new AggregateException(ex, failedToDeleteEx); } throw; } }
public override HttpResponseMessage Post(Error value) { if (value == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid error posted.")); } _stats.Counter(StatNames.ErrorsSubmitted); if (User != null && User.Project != null) { value.ProjectId = User.Project.Id; value.OrganizationId = User.Project.OrganizationId; } if (value.OccurrenceDate == DateTimeOffset.MinValue) { value.OccurrenceDate = DateTimeOffset.UtcNow; } string message = User == null?String.Format("Inserting error '{0}'.", value.Id) : String.Format("Inserting error '{0}' with API key '{1}'.", value.Id, User.Identity.Name); if (value.RequestInfo != null) { message += String.Format(" IP Address: {0}.", value.RequestInfo.ClientIpAddress); } if (value.ExceptionlessClientInfo != null) { message += String.Format(" Client Version: {0}.", value.ExceptionlessClientInfo.Version); } Log.Debug().Message(message).Write(); if (String.IsNullOrWhiteSpace(value.OrganizationId) || !User.IsInOrganization(value.OrganizationId)) { return(InvalidOrganizationErrorResponseMessage()); } string id = value.Id; if (String.IsNullOrEmpty(id)) { value.Id = ObjectId.GenerateNewId().ToString(); id = value.Id; } if (_messageFactory != null) { using (IMessageProducer messageProducer = _messageFactory.CreateMessageProducer()) { RetryUtil.Retry(() => messageProducer.Publish(value)); _stats.Counter(StatNames.ErrorsQueued); } } else { Log.Error().Message("Message Factory is null").Write(); } if (Request == null) { return(CreatedResponseMessage()); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id })); return(response); }
/// <summary> /// Gets the with multiple attempts. /// </summary> /// <param name="relativeUri">The relative URI.</param> /// <param name="retries">The number of retries.</param> /// <remarks>Sometimes web services can fail due to load and other issues. /// </remarks> public byte[] GetWithRetries(string relativeUri, int retries) { var ru = new RetryUtil(); ru.NumRetries = retries; return ru.RetryFunc<string, byte[], WebException>(Get, relativeUri, x => (x.Response as HttpWebResponse).StatusCode == HttpStatusCode.ServiceUnavailable); }
public static void Retry(Action action, int maxAttempts) { RetryUtil.Retry(action, maxAttempts); }
public static TResult Retry <TResult>(Func <TResult> func, int maxAttempts) { return(RetryUtil.Retry(func, maxAttempts)); }