Allow servers to autostart. Enable setting to control if server is executed using shell

This commit is contained in:
Pat Hartl 2023-08-11 15:03:30 -05:00
parent 6f5e6f8a45
commit a7707a84c7
7 changed files with 1442 additions and 18 deletions

View file

@ -10,6 +10,8 @@
public string OnStartScriptPath { get; set; } = "";
public string OnStopScriptPath { get; set; } = "";
public bool UseShellExecute { get; set; }
public bool Autostart { get; set; }
public int AutostartDelay { get; set; }
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LANCommander.Migrations
{
/// <inheritdoc />
public partial class AddServerUseShellExecuteAndAutostartDelay : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AutostartDelay",
table: "Servers",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<bool>(
name: "UseShellExecute",
table: "Servers",
type: "INTEGER",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AutostartDelay",
table: "Servers");
migrationBuilder.DropColumn(
name: "UseShellExecute",
table: "Servers");
}
}
}

View file

@ -15,7 +15,11 @@ namespace LANCommander.Migrations
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.8");
modelBuilder
.HasAnnotation("ProductVersion", "7.0.9")
.HasAnnotation("Proxies:ChangeTracking", false)
.HasAnnotation("Proxies:CheckEquality", false)
.HasAnnotation("Proxies:LazyLoading", true);
modelBuilder.Entity("CategoryGame", b =>
{
@ -627,6 +631,9 @@ namespace LANCommander.Migrations
b.Property<bool>("Autostart")
.HasColumnType("INTEGER");
b.Property<int>("AutostartDelay")
.HasColumnType("INTEGER");
b.Property<Guid?>("CreatedById")
.HasColumnType("TEXT");
@ -655,6 +662,9 @@ namespace LANCommander.Migrations
b.Property<DateTime>("UpdatedOn")
.HasColumnType("TEXT");
b.Property<bool>("UseShellExecute")
.HasColumnType("INTEGER");
b.Property<string>("WorkingDirectory")
.IsRequired()
.HasColumnType("TEXT");
@ -708,6 +718,12 @@ namespace LANCommander.Migrations
b.Property<int>("AccessFailedCount")
.HasColumnType("INTEGER");
b.Property<bool>("Approved")
.HasColumnType("INTEGER");
b.Property<DateTime>("ApprovedOn")
.HasColumnType("TEXT");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("TEXT");

View file

@ -87,6 +87,20 @@
<FormItem Label="Working Directory">
<Input @bind-Value="@context.WorkingDirectory" />
</FormItem>
<FormItem Label="Use Shell Execute">
<Switch @bind-Checked="context.UseShellExecute" />
</FormItem>
<FormItem Label="Autostart">
<Switch @bind-Checked="context.Autostart" />
</FormItem>
@if (context.Autostart)
{
<FormItem Label="Autostart Delay">
<AntDesign.Input @bind-Value="context.AutostartDelay" Placeholder="0">
<Suffix>Seconds</Suffix>
</AntDesign.Input>
</FormItem>
}
<FormItem>
<Button Type="@ButtonType.Primary" OnClick="Save" Icon="@IconType.Fill.Save">Save</Button>
</FormItem>

View file

@ -161,11 +161,6 @@ app.UseEndpoints(endpoints =>
endpoints.MapControllers();
});
// Migrate
await using var scope = app.Services.CreateAsyncScope();
using var db = scope.ServiceProvider.GetService<DatabaseContext>();
await db.Database.MigrateAsync();
if (!Directory.Exists("Upload"))
Directory.CreateDirectory("Upload");
@ -178,4 +173,28 @@ if (!Directory.Exists("Saves"))
if (!Directory.Exists("Snippets"))
Directory.CreateDirectory("Snippets");
// Migrate
await using var scope = app.Services.CreateAsyncScope();
using var db = scope.ServiceProvider.GetService<DatabaseContext>();
await db.Database.MigrateAsync();
// Autostart any server processes
var serverService = scope.ServiceProvider.GetService<ServerService>();
var serverProcessService = scope.ServiceProvider.GetService<ServerProcessService>();
foreach (var server in await serverService.Get(s => s.Autostart).ToListAsync())
{
try
{
if (server.AutostartDelay > 0)
await Task.Delay(server.AutostartDelay);
serverProcessService.StartServerAsync(server);
}
catch (Exception ex)
{
}
}
app.Run();

View file

@ -24,10 +24,13 @@ namespace LANCommander.Services
process.StartInfo.FileName = server.Path;
process.StartInfo.WorkingDirectory = server.WorkingDirectory;
process.StartInfo.Arguments = server.Arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.UseShellExecute = server.UseShellExecute;
process.EnableRaisingEvents = true;
if (!process.StartInfo.UseShellExecute)
{
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
@ -38,11 +41,15 @@ namespace LANCommander.Services
{
Logger.Error("Game Server {ServerName} ({ServerId}) Error: {Message}", server.Name, server.Id, e.Data);
});
}
process.Start();
if (!process.StartInfo.UseShellExecute)
{
process.BeginErrorReadLine();
process.BeginOutputReadLine();
}
Processes[server.Id] = process;