Skip to content

Commit

Permalink
Add Retry logic to RunCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed Aug 9, 2023
1 parent fcc60b7 commit 6210e5c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion AbpDevTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.0.3</Version>
<Version>1.1.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<PackAsTool>true</PackAsTool>
<ToolCommandName>abpdev</ToolCommandName>
Expand Down
27 changes: 25 additions & 2 deletions Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class RunCommand : ICommand
[CommandOption("env", 'e', Description = "Uses the virtual environment for this process. Use 'abpdev env config' command to see/manage environments.")]
public string EnvironmentName { get; set; }

[CommandOption("retry", 'r', Description = "Retries running again when application exits.")]
public bool Retry { get; set; }

protected IConsole console;

protected readonly List<RunningProjectItem> runningProjects = new();
Expand Down Expand Up @@ -190,7 +193,11 @@ await AnsiConsole.Live(table)
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(1000);
#if DEBUG
await Task.Delay(100);
#else
await Task.Delay(500);
#endif
table.Rows.Clear();
foreach (var project in runningProjects)
Expand All @@ -201,9 +208,16 @@ await AnsiConsole.Live(table)
}
else
{
if (project.Process.HasExited && !project.IsCompleted)
if (project.Process.HasExited && !project.Queued)
{
project.Status = $"[red]*[/] Exited({project.Process.ExitCode})";
if (Retry)
{
project.Status = $"[orange1]*[/] Exited({project.Process.ExitCode})";
_ = RestartProject(project); // fire and forget
}
}
table.AddRow(project.Name, project.Status);
}
Expand All @@ -214,6 +228,15 @@ await AnsiConsole.Live(table)
});
}

private static async Task RestartProject(RunningProjectItem project)
{
project.Queued = true;
await Task.Delay(3100);
project.Status = $"[orange1]*[/] Exited({project.Process.ExitCode}) (Retrying...)";
project.Process = Process.Start(project.Process.StartInfo);
project.StartReadingOutput();
}

protected void KillRunningProcesses()
{
console.Output.WriteLine($"- Killing running {runningProjects.Count} processes...");
Expand Down
37 changes: 31 additions & 6 deletions Commands/RunningProjectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public class RunningProjectItem
public Process Process { get; set; }
public virtual string Status { get; set; }
public virtual bool IsCompleted { get; set; }
public virtual bool Queued { get; set; }

public virtual void StartReadingOutput()
{
}

}

public class RunningCsProjItem : RunningProjectItem
Expand All @@ -17,23 +23,36 @@ public RunningCsProjItem(string name, Process process, string status = null)
this.Name = name;
this.Process = process;
this.Status = status ?? "Building...";
StartReadingOutput();
}

process.OutputDataReceived += OutputReceived;

public override void StartReadingOutput()
{
Queued = false;
Process.OutputDataReceived -= OutputReceived;
Process.OutputDataReceived += OutputReceived;
Process.BeginOutputReadLine();
}

protected virtual void OutputReceived(object sender, DataReceivedEventArgs args)
{
#if DEBUG
if (!IsCompleted)
{
Status = args.Data?.Replace("[", string.Empty).Replace("]", string.Empty) ?? string.Empty;
}
#endif

if (args.Data != null && args.Data.Contains("Now listening on: "))
{
Status = args.Data[args.Data.IndexOf("Now listening on: ")..];
Process.CancelOutputRead();
IsCompleted = true;
}

if (DateTime.Now - Process.StartTime > TimeSpan.FromMinutes(2))
if (DateTime.Now - Process.StartTime > TimeSpan.FromMinutes(5))
{
Status = "Stale";
Process.OutputDataReceived -= OutputReceived;
Process.CancelOutputRead();
}
Expand All @@ -44,12 +63,17 @@ public class RunningInstallLibsItem : RunningProjectItem
{
public RunningInstallLibsItem(string name, Process process, string status = null)
{
this.Name=name;
this.Name = name;
this.Process = process;
this.Status = status ?? "Installing...";
StartReadingOutput();
}

process.OutputDataReceived += OutputReceived;
process.BeginOutputReadLine();
public override void StartReadingOutput()
{
Process.OutputDataReceived -= OutputReceived;
Process.OutputDataReceived += OutputReceived;
Process.BeginOutputReadLine();
}

protected virtual void OutputReceived(object sender, DataReceivedEventArgs args)
Expand All @@ -63,6 +87,7 @@ protected virtual void OutputReceived(object sender, DataReceivedEventArgs args)

if (DateTime.Now - Process.StartTime > TimeSpan.FromMinutes(5))
{
Status = "Stale";
Process.OutputDataReceived -= OutputReceived;
Process.CancelOutputRead();
}
Expand Down

0 comments on commit 6210e5c

Please sign in to comment.