MoShiro__MinAgentVersion makes the backend refuse agents below it at hello (fleet_version_required); fleet.min_server_version or MOSHIRO_MIN_SERVER_VERSION makes the agent drop a backend below it. Either side waits five seconds and shakes hands again, because the other one may be updating.
56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
using Fleet.Core;
|
|
using Fleet.Rpc;
|
|
using Fleet.Server;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MoShiro.Backend.Application;
|
|
using MoShiro.Protocol;
|
|
|
|
namespace MoShiro.Backend.Gateway;
|
|
|
|
public static class GatewayServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// 在 Fleet.Server 之上接入 MoShiro:期望状态(TaskSet、DialTargets)、RawChunk 收件箱、<c>moshiro.*</c> 方法、注册挂钩和节点事件。
|
|
/// 需要事先注册 <see cref="IFleetStore"/> 和 <see cref="TimeProvider"/>,并在 <c>AddMoShiroApplication</c> 之后调用。
|
|
/// </summary>
|
|
public static IServiceCollection AddMoShiroGateway(this IServiceCollection services)
|
|
{
|
|
services.AddSingleton<IAgentGateway, FleetGateway>();
|
|
services.AddSingleton<IFleetVersionPolicy, BackendVersionPolicy>();
|
|
services.AddHostedService<ConnectionShutdown>();
|
|
services.AddFleetEnrollment();
|
|
services.AddFleetServer(options => options.NodeIdPrefix = AgentIds.Prefix)
|
|
.AddState<TaskSetStateProvider>()
|
|
.AddState<DialTargetsStateProvider>()
|
|
.AddInbox<RawChunkInbox>()
|
|
.WithMethods(MoShiroMethodTable.Table)
|
|
.MapMethods(MapMoShiroMethods);
|
|
return services;
|
|
}
|
|
|
|
private static void MapMoShiroMethods(RpcRouter<NodeCallContext> router, IServiceProvider services)
|
|
{
|
|
router
|
|
.OnNotify(
|
|
MoShiroMethods.Event,
|
|
MoShiroJsonContext.Default.AgentEventParams,
|
|
(context, parameters, ct) => services.GetRequiredService<AgentEventService>().OnEventAsync(context.Node.Id, parameters, ct))
|
|
.OnNotify(
|
|
MoShiroMethods.Host,
|
|
MoShiroJsonContext.Default.HostSnapshot,
|
|
(context, snapshot, ct) => services.GetRequiredService<HostStatusService>().OnHostAsync(context.Node.Id, snapshot, ct))
|
|
.OnNotify(
|
|
MoShiroMethods.TestStatus,
|
|
MoShiroJsonContext.Default.TestStatusParams,
|
|
(context, parameters, ct) => services.GetRequiredService<TestService>().OnStatusAsync(context.Node.Id, parameters, ct));
|
|
}
|
|
}
|
|
|
|
/// <summary>握手时的版本要求:告诉 Agent 面板的版本,并要求 Agent 不低于配置的最低版本。</summary>
|
|
internal sealed class BackendVersionPolicy(BackendOptions options) : IFleetVersionPolicy
|
|
{
|
|
public string? ServerVersion => BuildInfo.Version;
|
|
|
|
public string? MinNodeVersion => string.IsNullOrWhiteSpace(options.MinAgentVersion) ? null : options.MinAgentVersion;
|
|
}
|