mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Adds the scheduled jobs API endpoints
This commit is contained in:
1 parent
58d7375d61
commit
29c7d536e0
15 files changed
+1130
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Commands.StartScheduledJob;
|
||||
using Lumina.Contracts.Requests.Scheduling;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Scheduling;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.StartScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs/{scheduledJobId}/start</c> route.
|
||||
/// </summary>
|
||||
public class StartScheduledJobEndpoint : BaseEndpoint<StartScheduledJobRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<StartScheduledJobCommand, Result<ScheduledJobResponse>> _startScheduledJobCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartScheduledJobEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="startScheduledJobCommandHandler">Injected service for handling start scheduled job commands.</param>
|
||||
public StartScheduledJobEndpoint(ICommandHandler<StartScheduledJobCommand, Result<ScheduledJobResponse>> startScheduledJobCommandHandler)
|
||||
{
|
||||
_startScheduledJobCommandHandler = startScheduledJobCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.PUT);
|
||||
Routes(ApiRoutes.ScheduledJobs.START_SCHEDULED_JOB);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the execution cycle of a scheduled job.
|
||||
/// </summary>
|
||||
/// <param name="request">The request object.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(StartScheduledJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ScheduledJobResponse> result = await _startScheduledJobCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.Scheduling;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.StartScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StartScheduledJobEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StartScheduledJobEndpointSummary : Summary<StartScheduledJobEndpoint, StartScheduledJobRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartScheduledJobEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StartScheduledJobEndpointSummary()
|
||||
{
|
||||
Summary = "Starts the execution cycle of a scheduled job.";
|
||||
Description = "Starts the execution cycle of a scheduled job, which runs its task once immediately and then on its schedule. Only administrators can start scheduled jobs.";
|
||||
|
||||
ExampleRequest = new StartScheduledJobRequest(ScheduledJobId: Guid.NewGuid());
|
||||
|
||||
RequestParam(request => request.ScheduledJobId, "The unique identifier of the scheduled job whose execution cycle is started. Required.");
|
||||
|
||||
Response(200, "The execution cycle of the scheduled job was successfully started.",
|
||||
example: new ScheduledJobResponse(
|
||||
Id: Guid.NewGuid(),
|
||||
Name: "Rescan media libraries",
|
||||
TaskType: ScheduledTaskType.ScanMediaLibraries,
|
||||
ScheduleType: ScheduleType.DailyAtHourAndMinute,
|
||||
IntervalMinutes: null,
|
||||
Hour: 3,
|
||||
Minute: 0,
|
||||
Status: ScheduledJobStatus.Active,
|
||||
LastStartedOnUtc: DateTime.UtcNow.AddMinutes(-1),
|
||||
LastCompletedOnUtc: null
|
||||
));
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "You are not authorized",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token is invalid",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator, or because the execution cycle of the scheduled job is already started.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "ScheduledJobCycleAlreadyStarted",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the provided scheduled job does not exist.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ScheduledJobNotFound",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/start",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user