public static async System.Threading.Tasks.Task WaitForServiceToStart(this ECSHelper ecs, string cluster, string serviceName, int timeout, int delay = 2500) { var services = await((cluster.IsNullOrEmpty()) ? ecs.ListServicesAsync() : ecs.ListServicesAsync(cluster)); services = services.Where(x => ((serviceName.StartsWith("arn:")) ? x.ARN == serviceName : x.ARN.EndsWith($":service/{serviceName}"))); if (services?.Count() != 1) { throw new Exception($"Could not find service '{serviceName}' for cluster: '{cluster}' or found more then one matching result (In such case use ARN insted of serviceName, or specify cluster) [{services?.Count()}]."); } var service = services.First(); var tt = new TickTimeout(timeout, TickTime.Unit.s, Enabled: true); while (!tt.IsTriggered) { var serviceDescription = await ecs.DescribeServicesAsync(service.Cluster, new string[] { service.ARN }); if (serviceDescription.IsNullOrEmpty()) { throw new Exception($"Could not find or describe service: '{service.ARN}' for the cluster '{service.Cluster}'."); } var result = serviceDescription.First(); if (result.DesiredCount == result.RunningCount) { return; //desired count reached } await System.Threading.Tasks.Task.Delay(delay); } throw new Exception($"Timeout '{timeout}' [s], service: '{service.ARN}' could not reach its desired count in time."); }
public static bool IsLocked(this object o, int timeout_ms = 0) { if (o == null) { return(false); } TickTimeout timeout = new TickTimeout(timeout_ms, TickTime.Unit.ms); bool entered = false; try { do { entered = Monitor.TryEnter(o); if (entered || timeout_ms == 0) { break; } }while (timeout_ms < 0 || !timeout.IsTriggered); } finally { if (entered) { Monitor.Exit(o); } } return(!entered); }
/// <summary> /// /// </summary> /// <param name="text"></param> /// <param name="color"></param> /// <param name="maxLines"></param> /// <param name="deleteMultiplayer">Reduces flicker of scrollbar</param> /// <param name="timeout"></param> public void AppendTextToStart(string text, Color color, int maxLines, double deleteMultiplayer = 2, int timeout = 1000) { try { bool readonlystate = this.ReadOnly; this.ReadOnly = false; this.SelectionStart = 0; this.SelectionLength = 0;// text.Length - 1; this.ScrollToCaret(); this.SelectionColor = color; this.SelectedText = text; TickTimeout ttimeout = new TickTimeout(timeout, TickTime.Unit.ms); if (this.Lines.Length >= maxLines) { while (this.Lines.Length >= (maxLines * deleteMultiplayer) && !ttimeout.IsTriggered) { string rtext = this.Text; int last1 = rtext.IndexOfByCount('\n', -1); int last2 = rtext.IndexOfByCount('\n', -2); if (last1 < 0 || last2 < 0) { Asmodat.Debugging.Output.WriteLine("Not managed outcom in RichTextBox Abbreviate class !"); this.ScrollTop(); return; } this.SelectionStart = (int)last2 + 1; this.SelectionLength = ((int)last1 - (int)last2) + 1; this.SelectedText = ""; this.ScrollTop(); } } this.ScrollTop(); if (ttimeout.IsTriggered) { Asmodat.Debugging.Output.WriteLine("Rtbx timeout."); } this.ReadOnly = readonlystate; } catch (Exception ex) { ex.ToOutput(); } }
public static async Task AwaitSuccessCurlGET(string uri, int timeout, int intensity = 1000) { if (uri.IsNullOrEmpty()) { throw new ArgumentException($"{nameof(uri)} can't be null or empty."); } var tt = new TickTimeout(timeout, TickTime.Unit.ms); HttpResponseMessage lastResponse = null; Exception lastException = null; do { try { var result = (await HttpHelper.CURL(HttpMethod.Get, uri, null)); lastResponse = result.Response; if (lastResponse.StatusCode == System.Net.HttpStatusCode.OK) { return; } } catch (Exception ex) { lastException = ex; } if (!tt.IsTriggered) { await Task.Delay(intensity); } } while (!tt.IsTriggered); throw new Exception($"AwaitSuccessCurlGET, span: {(int)tt.Span}/{timeout} [ms], status code: '{lastResponse?.StatusCode}', response: '{lastResponse?.Content?.ReadAsStringAsync()}'", lastException); }