| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using CShells.Lifecycle; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Shells.Api.Endpoints.Shells; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Http; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Shells.Api.Endpoints.Shells.Reload; |
| | | 10 | | |
| | | 11 | | [PublicAPI] |
| | 10 | 12 | | internal class Reload(IShellRegistry shellRegistry, IApiSerializer apiSerializer) : ElsaEndpointWithoutRequest |
| | | 13 | | { |
| | | 14 | | public override void Configure() |
| | | 15 | | { |
| | 6 | 16 | | Post("/shells/{shellId}/reload"); |
| | 6 | 17 | | RequirePermission(Elsa.Shells.Api.Permissions.ShellPermissions.Shells, "reload"); |
| | 6 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 21 | | { |
| | 4 | 22 | | var shellId = Route<string>("shellId")!; |
| | | 23 | | ReloadResult result; |
| | | 24 | | |
| | | 25 | | try |
| | | 26 | | { |
| | 4 | 27 | | result = await shellRegistry.ReloadAsync(shellId, cancellationToken); |
| | 2 | 28 | | } |
| | 1 | 29 | | catch (ShellBlueprintNotFoundException ex) |
| | | 30 | | { |
| | 1 | 31 | | var notFoundResponse = new ShellReloadResponse |
| | 1 | 32 | | { |
| | 1 | 33 | | Status = ShellReloadStatus.NotFound, |
| | 1 | 34 | | RequestedShellId = shellId, |
| | 1 | 35 | | Timestamp = DateTimeOffset.UtcNow, |
| | 1 | 36 | | Message = ex.Message |
| | 1 | 37 | | }; |
| | 1 | 38 | | await SendResponseAsync(notFoundResponse, StatusCodes.Status404NotFound, cancellationToken); |
| | 1 | 39 | | return; |
| | | 40 | | } |
| | 1 | 41 | | catch (ShellBlueprintUnavailableException ex) |
| | | 42 | | { |
| | 1 | 43 | | var unavailableResponse = new ShellReloadResponse |
| | 1 | 44 | | { |
| | 1 | 45 | | Status = ShellReloadStatus.Failed, |
| | 1 | 46 | | RequestedShellId = shellId, |
| | 1 | 47 | | Timestamp = DateTimeOffset.UtcNow, |
| | 1 | 48 | | Message = ex.Message |
| | 1 | 49 | | }; |
| | 1 | 50 | | await SendResponseAsync(unavailableResponse, StatusCodes.Status503ServiceUnavailable, cancellationToken); |
| | 1 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | if (result.Error is not null) |
| | | 55 | | { |
| | 1 | 56 | | var failedResponse = new ShellReloadResponse |
| | 1 | 57 | | { |
| | 1 | 58 | | Status = ShellReloadStatus.Failed, |
| | 1 | 59 | | RequestedShellId = shellId, |
| | 1 | 60 | | Timestamp = DateTimeOffset.UtcNow, |
| | 1 | 61 | | Message = result.Error.Message |
| | 1 | 62 | | }; |
| | 1 | 63 | | await SendResponseAsync(failedResponse, StatusCodes.Status503ServiceUnavailable, cancellationToken); |
| | 1 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | var response = new ShellReloadResponse |
| | 1 | 68 | | { |
| | 1 | 69 | | Status = ShellReloadStatus.Completed, |
| | 1 | 70 | | RequestedShellId = shellId, |
| | 1 | 71 | | Timestamp = DateTimeOffset.UtcNow |
| | 1 | 72 | | }; |
| | 1 | 73 | | await SendResponseAsync(response, StatusCodes.Status200OK, cancellationToken); |
| | 4 | 74 | | } |
| | | 75 | | |
| | | 76 | | private async Task SendResponseAsync(ShellReloadResponse response, int statusCode, CancellationToken cancellationTok |
| | | 77 | | { |
| | 4 | 78 | | var serializerOptions = apiSerializer.GetOptions(); |
| | 4 | 79 | | HttpContext.Response.StatusCode = statusCode; |
| | 4 | 80 | | await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken); |
| | 4 | 81 | | } |
| | | 82 | | } |