1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-04-24 07:15:47 +02:00

Guard loading instances from database from exceptions

This commit is contained in:
chylex 2024-04-04 20:37:37 +02:00
parent 424dccb14e
commit 1b12fd9c3b
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 43 additions and 12 deletions
Controller/Phantom.Controller.Services/Agents
Utils/Phantom.Utils/Collections

View File

@ -13,11 +13,13 @@ using Phantom.Common.Data.Web.Minecraft;
using Phantom.Common.Messages.Agent;
using Phantom.Common.Messages.Agent.ToAgent;
using Phantom.Controller.Database;
using Phantom.Controller.Database.Entities;
using Phantom.Controller.Minecraft;
using Phantom.Controller.Services.Instances;
using Phantom.Utils.Actor;
using Phantom.Utils.Actor.Mailbox;
using Phantom.Utils.Actor.Tasks;
using Phantom.Utils.Collections;
using Phantom.Utils.Logging;
using Phantom.Utils.Rpc.Runtime;
using Serilog;
@ -194,21 +196,29 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
public sealed record ReceiveInstanceDataCommand(Instance Instance) : ICommand, IJumpAhead;
private async Task Initialize(InitializeCommand command) {
await using var ctx = dbProvider.Eager();
await foreach (var entity in ctx.Instances.Where(instance => instance.AgentGuid == agentGuid).AsAsyncEnumerable().WithCancellation(cancellationToken)) {
ImmutableArray<InstanceEntity> instanceEntities;
await using (var ctx = dbProvider.Eager()) {
instanceEntities = await ctx.Instances.Where(instance => instance.AgentGuid == agentGuid).AsAsyncEnumerable().ToImmutableArrayCatchingExceptionsAsync(OnException, cancellationToken);
}
static void OnException(Exception e) {
Logger.Error(e, "Could not load instance from database.");
}
foreach (var instanceEntity in instanceEntities) {
var instanceConfiguration = new InstanceConfiguration(
entity.AgentGuid,
entity.InstanceName,
entity.ServerPort,
entity.RconPort,
entity.MinecraftVersion,
entity.MinecraftServerKind,
entity.MemoryAllocation,
entity.JavaRuntimeGuid,
JvmArgumentsHelper.Split(entity.JvmArguments)
instanceEntity.AgentGuid,
instanceEntity.InstanceName,
instanceEntity.ServerPort,
instanceEntity.RconPort,
instanceEntity.MinecraftVersion,
instanceEntity.MinecraftServerKind,
instanceEntity.MemoryAllocation,
instanceEntity.JavaRuntimeGuid,
JvmArgumentsHelper.Split(instanceEntity.JvmArguments)
);
CreateNewInstance(Instance.Offline(entity.InstanceGuid, instanceConfiguration, entity.LaunchAutomatically));
CreateNewInstance(Instance.Offline(instanceEntity.InstanceGuid, instanceConfiguration, instanceEntity.LaunchAutomatically));
}
}

View File

@ -12,6 +12,27 @@ public static class EnumerableExtensions {
return builder.ToImmutable();
}
public static async Task<ImmutableArray<TSource>> ToImmutableArrayCatchingExceptionsAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<Exception> onException, CancellationToken cancellationToken = default) {
var builder = ImmutableArray.CreateBuilder<TSource>();
await using (var enumerator = source.GetAsyncEnumerator(cancellationToken)) {
while (true) {
try {
if (!await enumerator.MoveNextAsync()) {
break;
}
} catch (Exception e) {
onException(e);
continue;
}
builder.Add(enumerator.Current);
}
}
return builder.ToImmutable();
}
public static async Task<ImmutableHashSet<TSource>> ToImmutableSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) {
var builder = ImmutableHashSet.CreateBuilder<TSource>();