public static async Task MonitorStatus(RompExecutionEnvironment executer, CancellationToken cancel) { Console.CursorVisible = false; try { while (true) { cancel.ThrowIfCancellationRequested(); var status = executer.GetStatus(); if (status != null) { WriteCurrentStatus(OperationStatus.GetOperationStatusText(status)); } await Task.Delay(500, cancel).ConfigureAwait(false); } } finally { Console.WriteLine(new string(' ', Console.BufferWidth - 1)); Console.Write(new string(' ', Console.BufferWidth - 1)); Console.CursorLeft = 0; Console.CursorTop--; Console.CursorVisible = true; } }
private static async Task RunPlanAsync(ScopedStatementBlock script, bool simulate) { if (simulate) { Console.WriteLine("Running as simulation"); } Console.WriteLine(); var executer = new RompExecutionEnvironment(script, simulate); if (!Console.IsOutputRedirected) { RompConsoleMessenger.ShowProgress = true; using (var done = new CancellationTokenSource()) { Task consoleTask = null; try { consoleTask = Task.Run(() => RompConsoleMessenger.MonitorStatus(executer, done.Token)); await executer.ExecuteAsync(); } finally { done.Cancel(); try { if (consoleTask != null) { await consoleTask; } } catch { } } } } else { RompConsoleMessenger.ShowProgress = false; await executer.ExecuteAsync(); } var exec = RompDb.GetExecutions() .FirstOrDefault(e => e.ExecutionId == executer.ExecutionId); if (exec != null) { var logs = RompDb.GetExecutionLogs(exec.ExecutionId); foreach (var log in logs) { log.WriteErrors(Console.Out); } if (exec.StatusCode == Domains.ExecutionStatus.Normal) { RompConsoleMessenger.WriteDirect($"Job #{executer.ExecutionId} completed successfully.", ConsoleColor.White); } else if (exec.StatusCode == Domains.ExecutionStatus.Warning) { RompConsoleMessenger.WriteDirect($"Job #{executer.ExecutionId} completed with warnings.", ConsoleColor.Yellow); } else { RompConsoleMessenger.WriteDirect($"Job #{executer.ExecutionId} encountered an error.", ConsoleColor.Red); throw new RompException("Job execution failed."); } Console.WriteLine(); } else { throw new RompException("Execution could not be created."); } }