Files
leleka/desktop/CoreClient.Worker.cs
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

46 lines
2.4 KiB
C#

using Grpc.Core;
using Strata.Node.V1;
namespace Strata.Desktop;
internal static partial class CoreClient
{
public static IWorkerRpc WorkerRpc() => new WorkerRpcClient();
private sealed class WorkerRpcClient : IWorkerRpc
{
public async Task<WorkerReading> Read(CancellationToken token)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
timeout.CancelAfter(TimeSpan.FromSeconds(10));
var c = await Connect(timeout.Token);
var hello = await c.HandshakeAsync(new Hello { ProtocolVersion = 1 }, Headers(),
deadline: DateTime.UtcNow.AddSeconds(3), cancellationToken: timeout.Token);
ValidateSnapshot(hello, null);
if (!hello.Capabilities.Contains("worker.lifecycle.v1")) return new(hello.AgentEpoch, null);
var state = await c.WorkerStatusAsync(new Empty(), Headers(),
deadline: DateTime.UtcNow.AddSeconds(3), cancellationToken: timeout.Token);
var after = await c.HandshakeAsync(new Hello { ProtocolVersion = 1 }, Headers(),
deadline: DateTime.UtcNow.AddSeconds(3), cancellationToken: timeout.Token);
ValidateSnapshot(after, hello);
if (after.AgentEpoch != hello.AgentEpoch)
throw new RpcException(new Status(StatusCode.Unavailable, "Node changed"));
if (state.ProfileId.Length > 128 || state.Model.Length > 256 || state.Reason.Length > 256
|| state.Phase is not ("stopped" or "starting" or "ready" or "draining" or "failed"))
throw new InvalidOperationException("Invalid worker state");
return new(hello.AgentEpoch, state, hello.Capabilities.Contains("worker.journal.v1"));
}
public async Task<WorkerOperation> Submit(WorkerCommand command, CancellationToken token)
{
var c = await Connect(token);
return await c.ControlWorkerAsync(command, Headers(),
deadline: DateTime.UtcNow.AddSeconds(5), cancellationToken: token);
}
public async Task<WorkerOperation> Lookup(string id, CancellationToken token)
{
var c = await Connect(token);
return await c.OperationStatusAsync(new OperationQuery { OperationId = id }, Headers(),
deadline: DateTime.UtcNow.AddSeconds(3), cancellationToken: token);
}
}
}