mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-05 03:34:05 +02:00
Guard loading instances from database from exceptions
This commit is contained in:
parent
424dccb14e
commit
1b12fd9c3b
Controller/Phantom.Controller.Services/Agents
Utils/Phantom.Utils/Collections
@ -13,11 +13,13 @@ using Phantom.Common.Data.Web.Minecraft;
|
|||||||
using Phantom.Common.Messages.Agent;
|
using Phantom.Common.Messages.Agent;
|
||||||
using Phantom.Common.Messages.Agent.ToAgent;
|
using Phantom.Common.Messages.Agent.ToAgent;
|
||||||
using Phantom.Controller.Database;
|
using Phantom.Controller.Database;
|
||||||
|
using Phantom.Controller.Database.Entities;
|
||||||
using Phantom.Controller.Minecraft;
|
using Phantom.Controller.Minecraft;
|
||||||
using Phantom.Controller.Services.Instances;
|
using Phantom.Controller.Services.Instances;
|
||||||
using Phantom.Utils.Actor;
|
using Phantom.Utils.Actor;
|
||||||
using Phantom.Utils.Actor.Mailbox;
|
using Phantom.Utils.Actor.Mailbox;
|
||||||
using Phantom.Utils.Actor.Tasks;
|
using Phantom.Utils.Actor.Tasks;
|
||||||
|
using Phantom.Utils.Collections;
|
||||||
using Phantom.Utils.Logging;
|
using Phantom.Utils.Logging;
|
||||||
using Phantom.Utils.Rpc.Runtime;
|
using Phantom.Utils.Rpc.Runtime;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@ -194,21 +196,29 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
|
|||||||
public sealed record ReceiveInstanceDataCommand(Instance Instance) : ICommand, IJumpAhead;
|
public sealed record ReceiveInstanceDataCommand(Instance Instance) : ICommand, IJumpAhead;
|
||||||
|
|
||||||
private async Task Initialize(InitializeCommand command) {
|
private async Task Initialize(InitializeCommand command) {
|
||||||
await using var ctx = dbProvider.Eager();
|
ImmutableArray<InstanceEntity> instanceEntities;
|
||||||
await foreach (var entity in ctx.Instances.Where(instance => instance.AgentGuid == agentGuid).AsAsyncEnumerable().WithCancellation(cancellationToken)) {
|
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(
|
var instanceConfiguration = new InstanceConfiguration(
|
||||||
entity.AgentGuid,
|
instanceEntity.AgentGuid,
|
||||||
entity.InstanceName,
|
instanceEntity.InstanceName,
|
||||||
entity.ServerPort,
|
instanceEntity.ServerPort,
|
||||||
entity.RconPort,
|
instanceEntity.RconPort,
|
||||||
entity.MinecraftVersion,
|
instanceEntity.MinecraftVersion,
|
||||||
entity.MinecraftServerKind,
|
instanceEntity.MinecraftServerKind,
|
||||||
entity.MemoryAllocation,
|
instanceEntity.MemoryAllocation,
|
||||||
entity.JavaRuntimeGuid,
|
instanceEntity.JavaRuntimeGuid,
|
||||||
JvmArgumentsHelper.Split(entity.JvmArguments)
|
JvmArgumentsHelper.Split(instanceEntity.JvmArguments)
|
||||||
);
|
);
|
||||||
|
|
||||||
CreateNewInstance(Instance.Offline(entity.InstanceGuid, instanceConfiguration, entity.LaunchAutomatically));
|
CreateNewInstance(Instance.Offline(instanceEntity.InstanceGuid, instanceConfiguration, instanceEntity.LaunchAutomatically));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,27 @@ public static class EnumerableExtensions {
|
|||||||
|
|
||||||
return builder.ToImmutable();
|
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) {
|
public static async Task<ImmutableHashSet<TSource>> ToImmutableSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) {
|
||||||
var builder = ImmutableHashSet.CreateBuilder<TSource>();
|
var builder = ImmutableHashSet.CreateBuilder<TSource>();
|
||||||
|
Loading…
Reference in New Issue
Block a user