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:
parent
d93c93cbf7
commit
a31dda7439
Agent/Phantom.Agent.Services
Common
Phantom.Common.Data/Instance
Phantom.Common.Messages/ToAgent
Server
Phantom.Server.Services/Instances
Phantom.Server.Web
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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);
|
||||
|
@ -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) {}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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>
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user