| | | 1 | | using CShells.Lifecycle; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Shells.Api.Endpoints.Shells; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Shells.Api.Endpoints.Shells.ReloadAll; |
| | | 10 | | |
| | | 11 | | [PublicAPI] |
| | 6 | 12 | | internal class ReloadAll(IShellRegistry shellRegistry, IApiSerializer apiSerializer, ILogger<ReloadAll> logger) : ElsaEn |
| | | 13 | | { |
| | | 14 | | public override void Configure() |
| | | 15 | | { |
| | 4 | 16 | | Post("/shells/reload"); |
| | 4 | 17 | | ConfigurePermissions("actions:shells:reload"); |
| | 4 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 21 | | { |
| | | 22 | | // CShells 0.0.15: ReloadActiveAsync replaces the old ReloadAllShellsAsync. Per-shell failures are surfaced |
| | | 23 | | // in each ReloadResult.Error rather than thrown; aggregate any errors into a single 503 response. |
| | 2 | 24 | | var results = await shellRegistry.ReloadActiveAsync(cancellationToken: cancellationToken); |
| | 3 | 25 | | var errors = results.Where(r => r.Error is not null).ToList(); |
| | | 26 | | |
| | 2 | 27 | | if (errors.Count > 0) |
| | | 28 | | { |
| | 2 | 29 | | var summary = string.Join("; ", errors.Select(r => $"{r.Name}: {r.Error!.Message}")); |
| | 1 | 30 | | logger.LogError("Failed to reload {Count} shell(s): {Summary}", errors.Count, summary); |
| | 1 | 31 | | var failedResponse = new ShellReloadResponse |
| | 1 | 32 | | { |
| | 1 | 33 | | Status = ShellReloadStatus.Failed, |
| | 1 | 34 | | Timestamp = DateTimeOffset.UtcNow, |
| | 1 | 35 | | Message = summary |
| | 1 | 36 | | }; |
| | 1 | 37 | | await SendResponseAsync(failedResponse, StatusCodes.Status503ServiceUnavailable, cancellationToken); |
| | 1 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | var response = new ShellReloadResponse |
| | 1 | 42 | | { |
| | 1 | 43 | | Status = ShellReloadStatus.Completed, |
| | 1 | 44 | | Timestamp = DateTimeOffset.UtcNow |
| | 1 | 45 | | }; |
| | 1 | 46 | | await SendResponseAsync(response, StatusCodes.Status200OK, cancellationToken); |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task SendResponseAsync(ShellReloadResponse response, int statusCode, CancellationToken cancellationTok |
| | | 50 | | { |
| | 2 | 51 | | var serializerOptions = apiSerializer.GetOptions(); |
| | 2 | 52 | | HttpContext.Response.StatusCode = statusCode; |
| | 2 | 53 | | await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken); |
| | 2 | 54 | | } |
| | | 55 | | } |