1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-04 00:34:05 +02:00

Fix Server forgetting instance status after reconfiguring & refactor automatic instance launch

This commit is contained in:
chylex 2023-02-26 19:33:52 +01:00
parent d93c93cbf7
commit a31dda7439
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
8 changed files with 20 additions and 24 deletions
Agent/Phantom.Agent.Services
Common
Phantom.Common.Data/Instance
Phantom.Common.Messages/ToAgent
Server
Phantom.Server.Services/Instances
Phantom.Server.Web

View File

@ -71,7 +71,7 @@ sealed class InstanceSessionManager : IDisposable {
});
}
public async Task<InstanceActionResult<ConfigureInstanceResult>> Configure(InstanceConfiguration configuration, InstanceLaunchProperties launchProperties) {
public async Task<InstanceActionResult<ConfigureInstanceResult>> Configure(InstanceConfiguration configuration, InstanceLaunchProperties launchProperties, bool launchNow) {
return await AcquireSemaphoreAndRun(async () => {
var instanceGuid = configuration.InstanceGuid;
var instanceFolder = Path.Combine(basePath, instanceGuid.ToString());
@ -106,7 +106,7 @@ sealed class InstanceSessionManager : IDisposable {
Logger.Information("Created instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid);
}
if (configuration.LaunchAutomatically) {
if (launchNow) {
await LaunchInternal(instance);
}

View File

@ -60,7 +60,7 @@ public sealed class MessageListener : IMessageToAgentListener {
}
public async Task<InstanceActionResult<ConfigureInstanceResult>> HandleConfigureInstance(ConfigureInstanceMessage message) {
return await agent.InstanceSessionManager.Configure(message.Configuration, message.LaunchProperties);
return await agent.InstanceSessionManager.Configure(message.Configuration, message.LaunchProperties, message.LaunchNow);
}
public async Task<InstanceActionResult<LaunchInstanceResult>> HandleLaunchInstance(LaunchInstanceMessage message) {

View File

@ -15,6 +15,5 @@ public sealed partial record InstanceConfiguration(
[property: MemoryPackOrder(6)] MinecraftServerKind MinecraftServerKind,
[property: MemoryPackOrder(7)] RamAllocationUnits MemoryAllocation,
[property: MemoryPackOrder(8)] Guid JavaRuntimeGuid,
[property: MemoryPackOrder(9)] ImmutableArray<string> JvmArguments,
[property: MemoryPackOrder(10)] bool LaunchAutomatically
[property: MemoryPackOrder(9)] ImmutableArray<string> JvmArguments
);

View File

@ -7,7 +7,8 @@ namespace Phantom.Common.Messages.ToAgent;
[MemoryPackable]
public sealed partial record ConfigureInstanceMessage(
[property: MemoryPackOrder(0)] InstanceConfiguration Configuration,
[property: MemoryPackOrder(1)] InstanceLaunchProperties LaunchProperties
[property: MemoryPackOrder(1)] InstanceLaunchProperties LaunchProperties,
[property: MemoryPackOrder(2)] bool LaunchNow = false
) : IMessageToAgent<InstanceActionResult<ConfigureInstanceResult>> {
public Task<InstanceActionResult<ConfigureInstanceResult>> Accept(IMessageToAgentListener listener) {
return listener.HandleConfigureInstance(this);

View File

@ -4,7 +4,8 @@ namespace Phantom.Server.Services.Instances;
public sealed record Instance(
InstanceConfiguration Configuration,
IInstanceStatus Status
IInstanceStatus Status,
bool LaunchAutomatically
) {
internal Instance(InstanceConfiguration configuration) : this(configuration, InstanceStatus.Offline) {}
internal Instance(InstanceConfiguration configuration, bool launchAutomatically = false) : this(configuration, InstanceStatus.Offline, launchAutomatically) {}
}

View File

@ -52,11 +52,10 @@ public sealed class InstanceManager {
entity.MinecraftServerKind,
entity.MemoryAllocation,
entity.JavaRuntimeGuid,
JvmArgumentsHelper.Split(entity.JvmArguments),
entity.LaunchAutomatically
JvmArgumentsHelper.Split(entity.JvmArguments)
);
var instance = new Instance(configuration);
var instance = new Instance(configuration, entity.LaunchAutomatically);
instances.ByGuid[instance.Configuration.InstanceGuid] = instance;
}
}
@ -86,8 +85,10 @@ public sealed class InstanceManager {
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
try {
var instance = new Instance(configuration);
instances.ByGuid.AddOrReplace(instance.Configuration.InstanceGuid, instance, out var oldInstance);
isNewInstance = !instances.ByGuid.TryReplace(configuration.InstanceGuid, instance => instance with { Configuration = configuration });
if (isNewInstance) {
instances.ByGuid.TryAdd(configuration.InstanceGuid, new Instance(configuration));
}
var message = new ConfigureInstanceMessage(configuration, new InstanceLaunchProperties(serverExecutableInfo));
var reply = await agentManager.SendMessage<ConfigureInstanceMessage, InstanceActionResult<ConfigureInstanceResult>>(configuration.AgentGuid, message, TimeSpan.FromSeconds(10));
@ -96,8 +97,6 @@ public sealed class InstanceManager {
ConfigureInstanceResult.Success => AddOrEditInstanceResult.Success,
_ => AddOrEditInstanceResult.UnknownError
});
isNewInstance = oldInstance == null;
if (result.Is(AddOrEditInstanceResult.Success)) {
using var scope = databaseProvider.CreateScope();
@ -112,7 +111,6 @@ public sealed class InstanceManager {
entity.MemoryAllocation = configuration.MemoryAllocation;
entity.JavaRuntimeGuid = configuration.JavaRuntimeGuid;
entity.JvmArguments = JvmArgumentsHelper.Join(configuration.JvmArguments);
entity.LaunchAutomatically = configuration.LaunchAutomatically;
await scope.Ctx.SaveChangesAsync(cancellationToken);
}
@ -189,9 +187,7 @@ public sealed class InstanceManager {
private async Task SetInstanceShouldLaunchAutomatically(Guid instanceGuid, bool shouldLaunchAutomatically) {
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
try {
instances.ByGuid.TryReplace(instanceGuid, instance => instance with {
Configuration = instance.Configuration with { LaunchAutomatically = shouldLaunchAutomatically }
});
instances.ByGuid.TryReplace(instanceGuid, instance => instance with { LaunchAutomatically = shouldLaunchAutomatically });
using var scope = databaseProvider.CreateScope();
var entity = await scope.Ctx.Instances.FindAsync(instanceGuid, cancellationToken);
@ -211,9 +207,9 @@ public sealed class InstanceManager {
internal async Task<ImmutableArray<ConfigureInstanceMessage>> GetInstanceConfigurationsForAgent(Guid agentGuid) {
var configurationMessages = ImmutableArray.CreateBuilder<ConfigureInstanceMessage>();
foreach (var configuration in instances.ByGuid.ValuesCopy.Select(static instance => instance.Configuration).Where(configuration => configuration.AgentGuid == agentGuid)) {
foreach (var (configuration, _, launchAutomatically) in instances.ByGuid.ValuesCopy.Where(instance => instance.Configuration.AgentGuid == agentGuid)) {
var serverExecutableInfo = await minecraftVersions.GetServerExecutableInfo(configuration.MinecraftVersion, cancellationToken);
configurationMessages.Add(new ConfigureInstanceMessage(configuration, new InstanceLaunchProperties(serverExecutableInfo)));
configurationMessages.Add(new ConfigureInstanceMessage(configuration, new InstanceLaunchProperties(serverExecutableInfo), launchAutomatically));
}
return configurationMessages.ToImmutable();

View File

@ -29,7 +29,7 @@
</thead>
@if (!instances.IsEmpty) {
<tbody>
@foreach (var (configuration, status) in instances) {
@foreach (var (configuration, status, _) in instances) {
var agentName = agentNames.TryGetValue(configuration.AgentGuid, out var name) ? name : string.Empty;
var instanceGuid = configuration.InstanceGuid.ToString();
<tr>

View File

@ -325,8 +325,7 @@
form.MinecraftServerKind,
form.MemoryAllocation ?? RamAllocationUnits.Zero,
form.JavaRuntimeGuid.GetValueOrDefault(),
JvmArgumentsHelper.Split(form.JvmArguments),
EditedInstanceConfiguration?.LaunchAutomatically ?? false
JvmArgumentsHelper.Split(form.JvmArguments)
);
var result = await InstanceManager.AddOrEditInstance(instance);