|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.AspNetCore.Http.HttpResults; |
| 3 | +using SquidStd.Workers.Abstractions.Data; |
| 4 | +using SquidStd.Workers.Manager.Data; |
| 5 | +using SquidStd.Workers.Manager.Interfaces; |
| 6 | + |
| 7 | +namespace SquidStd.Workers.Manager.Endpoints; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Typed minimal-API handlers for the worker manager surface. Static so they can be unit-tested directly. |
| 11 | +/// </summary> |
| 12 | +public static class WorkerManagerEndpoints |
| 13 | +{ |
| 14 | + /// <summary>Lists all known workers.</summary> |
| 15 | + public static Ok<IReadOnlyCollection<WorkerInfo>> GetWorkers(IWorkerRegistry registry) |
| 16 | + => TypedResults.Ok(registry.GetAll()); |
| 17 | + |
| 18 | + /// <summary>Returns a single worker, or 404 when unknown.</summary> |
| 19 | + public static Results<Ok<WorkerInfo>, NotFound> GetWorker(string id, IWorkerRegistry registry) |
| 20 | + { |
| 21 | + var info = registry.Get(id); |
| 22 | + |
| 23 | + return info is null ? TypedResults.NotFound() : TypedResults.Ok(info); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary>Enqueues a job; 400 on a blank name, 503 when the queue is unavailable.</summary> |
| 27 | + public static async Task<Results<Accepted, BadRequest<string>, ProblemHttpResult>> EnqueueJob( |
| 28 | + EnqueueJobRequest request, |
| 29 | + IJobScheduler scheduler, |
| 30 | + CancellationToken cancellationToken) |
| 31 | + { |
| 32 | + if (string.IsNullOrWhiteSpace(request.JobName)) |
| 33 | + { |
| 34 | + return TypedResults.BadRequest("JobName is required."); |
| 35 | + } |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + await scheduler.EnqueueAsync(request.JobName, request.Parameters ?? new Dictionary<string, string>(), cancellationToken); |
| 40 | + |
| 41 | + return TypedResults.Accepted((string?)null); |
| 42 | + } |
| 43 | + catch (Exception) |
| 44 | + { |
| 45 | + return TypedResults.Problem(statusCode: StatusCodes.Status503ServiceUnavailable, detail: "Job queue unavailable."); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments