/// <summary> /// Waits for process to start. /// </summary> /// <param name="service">The service to check processes within.</param> /// <param name="process">The process to wait for.</param> /// <param name="millisTimeout">Timeout giving up the wait.</param> /// <returns>The inparam service.</returns> public static IContainerService WaitForProcess(this IContainerService service, string process, long millisTimeout = -1) { if (service == null) { return(null); } Exception exception = null; var stopwatch = Stopwatch.StartNew(); using (var mre = new ManualResetEventSlim()) using (new Timer(_ => { var processes = service.GetRunningProcesses(); if (processes?.Rows.Any(x => x.Command == process) ?? false) { mre.Set(); } if (stopwatch.ElapsedMilliseconds > millisTimeout) { exception = new FluentDockerException($"Wait expired for process {process} in container {service.Id}"); mre.Set(); } }, null, 0, 500)) mre.Wait(); if (exception != null) { throw exception; } return(service); }
/// <summary> /// Waits for a specific message in the logs /// </summary> /// <param name="service">The service to check processes within.</param> /// <param name="message">The message to wait for</param> /// <param name="millisTimeout">Timeout giving up the wait.</param> /// <returns>The in param service.</returns> public static IContainerService WaitForMessageInLogs(this IContainerService service, string message, long millisTimeout = -1) { if (service == null) { return(null); } Exception exception = null; var stopwatch = Stopwatch.StartNew(); using (var mre = new ManualResetEventSlim()) { Timer timer; using (timer = new Timer(_ => { var logs = service.Logs().ReadToEnd((int)millisTimeout); var match = logs.FirstOrDefault(stringToCheck => stringToCheck.Contains(message)); if (match != null) { mre.Set(); } if (stopwatch.ElapsedMilliseconds > millisTimeout) { exception = new FluentDockerException($"Wait for message '{message}' in logs for container {service.Id}"); mre.Set(); } }, null, 0, 500)) mre.Wait(); timer.Dispose(); } if (exception != null) { throw exception; } return(service); }
/// <summary> /// Waits for the container to be in a healthy state /// </summary> /// <param name="service">The service to check processes within.</param> /// <param name="millisTimeout">Timeout giving up the wait.</param> /// <returns>The in param service.</returns> public static IContainerService WaitForHealthy(this IContainerService service, long millisTimeout = -1) { if (service == null) { return(null); } Exception exception = null; var stopwatch = Stopwatch.StartNew(); using (var mre = new ManualResetEventSlim()) { Timer timer; using (timer = new Timer(_ => { var config = service.GetConfiguration(true); if (config?.State?.Health?.Status == HealthState.Healthy) { mre.Set(); } if (stopwatch.ElapsedMilliseconds > millisTimeout) { exception = new FluentDockerException($"Wait for healthy expired for container {service.Id}"); mre.Set(); } }, null, 0, 500)) mre.Wait(); timer.Dispose(); } if (exception != null) { throw exception; } return(service); }
public static void WaitForRunning(this IContainerService container) { Exception exception = null; using (var mre = new ManualResetEventSlim()) using (new Timer(_ => { var state = container.GetConfiguration(true).State; if (!string.IsNullOrWhiteSpace(state.Error)) { exception = new FluentDockerException($"Unable to start container: {state.Error}"); mre.Set(); } if (state.ToServiceState() == ServiceRunningState.Running) { mre.Set(); } }, null, 0, 500)) mre.Wait(); if (exception != null) { throw exception; } }