mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-21 22: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 () => {
|
return await AcquireSemaphoreAndRun(async () => {
|
||||||
var instanceGuid = configuration.InstanceGuid;
|
var instanceGuid = configuration.InstanceGuid;
|
||||||
var instanceFolder = Path.Combine(basePath, instanceGuid.ToString());
|
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);
|
Logger.Information("Created instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configuration.LaunchAutomatically) {
|
if (launchNow) {
|
||||||
await LaunchInternal(instance);
|
await LaunchInternal(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ public sealed class MessageListener : IMessageToAgentListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<InstanceActionResult<ConfigureInstanceResult>> HandleConfigureInstance(ConfigureInstanceMessage message) {
|
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) {
|
public async Task<InstanceActionResult<LaunchInstanceResult>> HandleLaunchInstance(LaunchInstanceMessage message) {
|
||||||
|
@ -15,6 +15,5 @@ public sealed partial record InstanceConfiguration(
|
|||||||
[property: MemoryPackOrder(6)] MinecraftServerKind MinecraftServerKind,
|
[property: MemoryPackOrder(6)] MinecraftServerKind MinecraftServerKind,
|
||||||
[property: MemoryPackOrder(7)] RamAllocationUnits MemoryAllocation,
|
[property: MemoryPackOrder(7)] RamAllocationUnits MemoryAllocation,
|
||||||
[property: MemoryPackOrder(8)] Guid JavaRuntimeGuid,
|
[property: MemoryPackOrder(8)] Guid JavaRuntimeGuid,
|
||||||
[property: MemoryPackOrder(9)] ImmutableArray<string> JvmArguments,
|
[property: MemoryPackOrder(9)] ImmutableArray<string> JvmArguments
|
||||||
[property: MemoryPackOrder(10)] bool LaunchAutomatically
|
|
||||||
);
|
);
|
||||||
|
@ -7,7 +7,8 @@ namespace Phantom.Common.Messages.ToAgent;
|
|||||||
[MemoryPackable]
|
[MemoryPackable]
|
||||||
public sealed partial record ConfigureInstanceMessage(
|
public sealed partial record ConfigureInstanceMessage(
|
||||||
[property: MemoryPackOrder(0)] InstanceConfiguration Configuration,
|
[property: MemoryPackOrder(0)] InstanceConfiguration Configuration,
|
||||||
[property: MemoryPackOrder(1)] InstanceLaunchProperties LaunchProperties
|
[property: MemoryPackOrder(1)] InstanceLaunchProperties LaunchProperties,
|
||||||
|
[property: MemoryPackOrder(2)] bool LaunchNow = false
|
||||||
) : IMessageToAgent<InstanceActionResult<ConfigureInstanceResult>> {
|
) : IMessageToAgent<InstanceActionResult<ConfigureInstanceResult>> {
|
||||||
public Task<InstanceActionResult<ConfigureInstanceResult>> Accept(IMessageToAgentListener listener) {
|
public Task<InstanceActionResult<ConfigureInstanceResult>> Accept(IMessageToAgentListener listener) {
|
||||||
return listener.HandleConfigureInstance(this);
|
return listener.HandleConfigureInstance(this);
|
||||||
|
@ -4,7 +4,8 @@ namespace Phantom.Server.Services.Instances;
|
|||||||
|
|
||||||
public sealed record Instance(
|
public sealed record Instance(
|
||||||
InstanceConfiguration Configuration,
|
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.MinecraftServerKind,
|
||||||
entity.MemoryAllocation,
|
entity.MemoryAllocation,
|
||||||
entity.JavaRuntimeGuid,
|
entity.JavaRuntimeGuid,
|
||||||
JvmArgumentsHelper.Split(entity.JvmArguments),
|
JvmArgumentsHelper.Split(entity.JvmArguments)
|
||||||
entity.LaunchAutomatically
|
|
||||||
);
|
);
|
||||||
|
|
||||||
var instance = new Instance(configuration);
|
var instance = new Instance(configuration, entity.LaunchAutomatically);
|
||||||
instances.ByGuid[instance.Configuration.InstanceGuid] = instance;
|
instances.ByGuid[instance.Configuration.InstanceGuid] = instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,8 +85,10 @@ public sealed class InstanceManager {
|
|||||||
|
|
||||||
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
|
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
|
||||||
try {
|
try {
|
||||||
var instance = new Instance(configuration);
|
isNewInstance = !instances.ByGuid.TryReplace(configuration.InstanceGuid, instance => instance with { Configuration = configuration });
|
||||||
instances.ByGuid.AddOrReplace(instance.Configuration.InstanceGuid, instance, out var oldInstance);
|
if (isNewInstance) {
|
||||||
|
instances.ByGuid.TryAdd(configuration.InstanceGuid, new Instance(configuration));
|
||||||
|
}
|
||||||
|
|
||||||
var message = new ConfigureInstanceMessage(configuration, new InstanceLaunchProperties(serverExecutableInfo));
|
var message = new ConfigureInstanceMessage(configuration, new InstanceLaunchProperties(serverExecutableInfo));
|
||||||
var reply = await agentManager.SendMessage<ConfigureInstanceMessage, InstanceActionResult<ConfigureInstanceResult>>(configuration.AgentGuid, message, TimeSpan.FromSeconds(10));
|
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,
|
ConfigureInstanceResult.Success => AddOrEditInstanceResult.Success,
|
||||||
_ => AddOrEditInstanceResult.UnknownError
|
_ => AddOrEditInstanceResult.UnknownError
|
||||||
});
|
});
|
||||||
|
|
||||||
isNewInstance = oldInstance == null;
|
|
||||||
|
|
||||||
if (result.Is(AddOrEditInstanceResult.Success)) {
|
if (result.Is(AddOrEditInstanceResult.Success)) {
|
||||||
using var scope = databaseProvider.CreateScope();
|
using var scope = databaseProvider.CreateScope();
|
||||||
@ -112,7 +111,6 @@ public sealed class InstanceManager {
|
|||||||
entity.MemoryAllocation = configuration.MemoryAllocation;
|
entity.MemoryAllocation = configuration.MemoryAllocation;
|
||||||
entity.JavaRuntimeGuid = configuration.JavaRuntimeGuid;
|
entity.JavaRuntimeGuid = configuration.JavaRuntimeGuid;
|
||||||
entity.JvmArguments = JvmArgumentsHelper.Join(configuration.JvmArguments);
|
entity.JvmArguments = JvmArgumentsHelper.Join(configuration.JvmArguments);
|
||||||
entity.LaunchAutomatically = configuration.LaunchAutomatically;
|
|
||||||
|
|
||||||
await scope.Ctx.SaveChangesAsync(cancellationToken);
|
await scope.Ctx.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
@ -189,9 +187,7 @@ public sealed class InstanceManager {
|
|||||||
private async Task SetInstanceShouldLaunchAutomatically(Guid instanceGuid, bool shouldLaunchAutomatically) {
|
private async Task SetInstanceShouldLaunchAutomatically(Guid instanceGuid, bool shouldLaunchAutomatically) {
|
||||||
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
|
await modifyInstancesSemaphore.WaitAsync(cancellationToken);
|
||||||
try {
|
try {
|
||||||
instances.ByGuid.TryReplace(instanceGuid, instance => instance with {
|
instances.ByGuid.TryReplace(instanceGuid, instance => instance with { LaunchAutomatically = shouldLaunchAutomatically });
|
||||||
Configuration = instance.Configuration with { LaunchAutomatically = shouldLaunchAutomatically }
|
|
||||||
});
|
|
||||||
|
|
||||||
using var scope = databaseProvider.CreateScope();
|
using var scope = databaseProvider.CreateScope();
|
||||||
var entity = await scope.Ctx.Instances.FindAsync(instanceGuid, cancellationToken);
|
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) {
|
internal async Task<ImmutableArray<ConfigureInstanceMessage>> GetInstanceConfigurationsForAgent(Guid agentGuid) {
|
||||||
var configurationMessages = ImmutableArray.CreateBuilder<ConfigureInstanceMessage>();
|
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);
|
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();
|
return configurationMessages.ToImmutable();
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
</thead>
|
</thead>
|
||||||
@if (!instances.IsEmpty) {
|
@if (!instances.IsEmpty) {
|
||||||
<tbody>
|
<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 agentName = agentNames.TryGetValue(configuration.AgentGuid, out var name) ? name : string.Empty;
|
||||||
var instanceGuid = configuration.InstanceGuid.ToString();
|
var instanceGuid = configuration.InstanceGuid.ToString();
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -325,8 +325,7 @@
|
|||||||
form.MinecraftServerKind,
|
form.MinecraftServerKind,
|
||||||
form.MemoryAllocation ?? RamAllocationUnits.Zero,
|
form.MemoryAllocation ?? RamAllocationUnits.Zero,
|
||||||
form.JavaRuntimeGuid.GetValueOrDefault(),
|
form.JavaRuntimeGuid.GetValueOrDefault(),
|
||||||
JvmArgumentsHelper.Split(form.JvmArguments),
|
JvmArgumentsHelper.Split(form.JvmArguments)
|
||||||
EditedInstanceConfiguration?.LaunchAutomatically ?? false
|
|
||||||
);
|
);
|
||||||
|
|
||||||
var result = await InstanceManager.AddOrEditInstance(instance);
|
var result = await InstanceManager.AddOrEditInstance(instance);
|
||||||
|
Loading…
Reference in New Issue
Block a user