mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 13:01: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
@@ -0,0 +1,18 @@
|
||||
namespace Lumina.Presentation.Api.Common.Routes.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Class for the collection of the scheduled job routes of the API.
|
||||
/// </summary>
|
||||
public static partial class ApiRoutes
|
||||
{
|
||||
public static class ScheduledJobs
|
||||
{
|
||||
public const string GET_SCHEDULED_JOBS = "/scheduled-jobs";
|
||||
public const string ADD_SCHEDULED_JOB = "/scheduled-jobs";
|
||||
public const string GET_SCHEDULED_JOB_HISTORY = "/scheduled-jobs/history";
|
||||
public const string REMOVE_SCHEDULED_JOB = "/scheduled-jobs/{scheduledJobId}";
|
||||
public const string START_SCHEDULED_JOB = "/scheduled-jobs/{scheduledJobId}/start";
|
||||
public const string STOP_SCHEDULED_JOB = "/scheduled-jobs/{scheduledJobId}/stop";
|
||||
public const string FIRE_SCHEDULED_JOB = "/scheduled-jobs/{scheduledJobId}/fire";
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Commands.AddScheduledJob;
|
||||
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.AddScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs</c> route.
|
||||
/// </summary>
|
||||
public class AddScheduledJobEndpoint : BaseEndpoint<AddScheduledJobRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<AddScheduledJobCommand, Result<ScheduledJobResponse>> _addScheduledJobCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddScheduledJobEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="addScheduledJobCommandHandler">Injected service for handling add scheduled job commands.</param>
|
||||
public AddScheduledJobEndpoint(ICommandHandler<AddScheduledJobCommand, Result<ScheduledJobResponse>> addScheduledJobCommandHandler)
|
||||
{
|
||||
_addScheduledJobCommandHandler = addScheduledJobCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.ScheduledJobs.ADD_SCHEDULED_JOB);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds 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(AddScheduledJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ScheduledJobResponse> result = await _addScheduledJobCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
#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.AddScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="AddScheduledJobEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class AddScheduledJobEndpointSummary : Summary<AddScheduledJobEndpoint, AddScheduledJobRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddScheduledJobEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public AddScheduledJobEndpointSummary()
|
||||
{
|
||||
Summary = "Adds a scheduled job.";
|
||||
Description = "Adds a scheduled job that can run its task periodically or once. Only administrators can add scheduled jobs.";
|
||||
|
||||
ExampleRequest = new AddScheduledJobRequest(
|
||||
Name: "Rescan media libraries",
|
||||
TaskType: ScheduledTaskType.ScanMediaLibraries,
|
||||
ScheduleType: ScheduleType.DailyAtHourAndMinute,
|
||||
IntervalMinutes: null,
|
||||
Hour: 3,
|
||||
Minute: 0
|
||||
);
|
||||
|
||||
RequestParam(request => request.Name, "The name of the scheduled job. Required.");
|
||||
RequestParam(request => request.TaskType, "The type of the task executed by the scheduled job. Required.");
|
||||
RequestParam(request => request.ScheduleType, "The type of the schedule of the scheduled job. Required.");
|
||||
RequestParam(request => request.IntervalMinutes, "The interval in minutes of the schedule, required when the schedule type is WithIntervalInMinutes.");
|
||||
RequestParam(request => request.Hour, "The hour of the schedule, required when the schedule type is DailyAtHourAndMinute.");
|
||||
RequestParam(request => request.Minute, "The minute of the schedule, required when the schedule type is DailyAtHourAndMinute.");
|
||||
|
||||
Response(200, "The scheduled job was successfully added.",
|
||||
example: new ScheduledJobResponse(
|
||||
Id: Guid.NewGuid(),
|
||||
Name: "Rescan media libraries",
|
||||
TaskType: ScheduledTaskType.ScanMediaLibraries,
|
||||
ScheduleType: ScheduleType.DailyAtHourAndMinute,
|
||||
IntervalMinutes: null,
|
||||
Hour: 3,
|
||||
Minute: 0,
|
||||
Status: ScheduledJobStatus.Added,
|
||||
LastStartedOnUtc: null,
|
||||
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"
|
||||
},
|
||||
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"
|
||||
},
|
||||
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"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/scheduled-jobs",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/scheduled-jobs",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ScheduledJobNameCannotBeEmpty",
|
||||
"IntervalMinutesMustBePositive",
|
||||
"HourMustBeBetweenZeroAndTwentyThree",
|
||||
"MinuteMustBeBetweenZeroAndFiftyNine",
|
||||
"InvalidScheduleType"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Commands.FireScheduledJob;
|
||||
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.FireScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs/{scheduledJobId}/fire</c> route.
|
||||
/// </summary>
|
||||
public class FireScheduledJobEndpoint : BaseEndpoint<FireScheduledJobRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<FireScheduledJobCommand, Result<ScheduledJobResponse>> _fireScheduledJobCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FireScheduledJobEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="fireScheduledJobCommandHandler">Injected service for handling fire scheduled job commands.</param>
|
||||
public FireScheduledJobEndpoint(ICommandHandler<FireScheduledJobCommand, Result<ScheduledJobResponse>> fireScheduledJobCommandHandler)
|
||||
{
|
||||
_fireScheduledJobCommandHandler = fireScheduledJobCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.PUT);
|
||||
Routes(ApiRoutes.ScheduledJobs.FIRE_SCHEDULED_JOB);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires the task of a scheduled job once, without affecting its execution cycle.
|
||||
/// </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(FireScheduledJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ScheduledJobResponse> result = await _fireScheduledJobCommandHandler.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.FireScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="FireScheduledJobEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FireScheduledJobEndpointSummary : Summary<FireScheduledJobEndpoint, FireScheduledJobRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FireScheduledJobEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public FireScheduledJobEndpointSummary()
|
||||
{
|
||||
Summary = "Fires the task of a scheduled job once.";
|
||||
Description = "Runs the task of a scheduled job once immediately, without affecting its execution cycle. Only administrators can fire scheduled jobs.";
|
||||
|
||||
ExampleRequest = new FireScheduledJobRequest(ScheduledJobId: Guid.NewGuid());
|
||||
|
||||
RequestParam(request => request.ScheduledJobId, "The unique identifier of the scheduled job whose task is fired. Required.");
|
||||
|
||||
Response(200, "The task of the scheduled job was successfully fired.",
|
||||
example: new ScheduledJobResponse(
|
||||
Id: Guid.NewGuid(),
|
||||
Name: "Clean temporary files",
|
||||
TaskType: ScheduledTaskType.CleanTemporaryFiles,
|
||||
ScheduleType: ScheduleType.WithIntervalInMinutes,
|
||||
IntervalMinutes: 1440,
|
||||
Hour: null,
|
||||
Minute: null,
|
||||
Status: ScheduledJobStatus.Completed,
|
||||
LastStartedOnUtc: DateTime.UtcNow.AddMinutes(-1),
|
||||
LastCompletedOnUtc: DateTime.UtcNow
|
||||
));
|
||||
|
||||
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}/fire"
|
||||
},
|
||||
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}/fire"
|
||||
},
|
||||
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}/fire"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator, or because the task of the scheduled job is currently running.", "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}/fire",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "ScheduledJobAlreadyRunning",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/fire",
|
||||
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}/fire",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobHistory;
|
||||
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.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobHistory;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs/history</c> route.
|
||||
/// </summary>
|
||||
public class GetScheduledJobHistoryEndpoint : BaseEndpoint<GetScheduledJobHistoryRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetScheduledJobHistoryQuery, Result<IEnumerable<ScheduledJobExecutionResponse>>> _getScheduledJobHistoryQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetScheduledJobHistoryEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getScheduledJobHistoryQueryHandler">Injected service for handling get scheduled job history queries.</param>
|
||||
public GetScheduledJobHistoryEndpoint(IQueryHandler<GetScheduledJobHistoryQuery, Result<IEnumerable<ScheduledJobExecutionResponse>>> getScheduledJobHistoryQueryHandler)
|
||||
{
|
||||
_getScheduledJobHistoryQueryHandler = getScheduledJobHistoryQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.GET);
|
||||
Routes(ApiRoutes.ScheduledJobs.GET_SCHEDULED_JOB_HISTORY);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the history of the executions of the tasks of scheduled jobs.
|
||||
/// </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(GetScheduledJobHistoryRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<IEnumerable<ScheduledJobExecutionResponse>> result = await _getScheduledJobHistoryQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#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.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobHistory;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetScheduledJobHistoryEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetScheduledJobHistoryEndpointSummary : Summary<GetScheduledJobHistoryEndpoint, GetScheduledJobHistoryRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetScheduledJobHistoryEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetScheduledJobHistoryEndpointSummary()
|
||||
{
|
||||
Summary = "Gets the history of the executions of the tasks of scheduled jobs.";
|
||||
Description = "Gets the executions of the tasks of scheduled jobs that started in the requested time interval. Only administrators can get the history of scheduled jobs.";
|
||||
|
||||
ExampleRequest = new GetScheduledJobHistoryRequest(From: DateTime.UtcNow.AddDays(-1), To: DateTime.UtcNow);
|
||||
|
||||
RequestParam(request => request.From, "The inclusive lower bound of the interval for which the history is requested. Optional.");
|
||||
RequestParam(request => request.To, "The inclusive upper bound of the interval for which the history is requested. Optional.");
|
||||
|
||||
Response(200, "The execution history was successfully retrieved.",
|
||||
example: new[]
|
||||
{
|
||||
new ScheduledJobExecutionResponse(
|
||||
Id: Guid.NewGuid(),
|
||||
ScheduledJobId: Guid.NewGuid(),
|
||||
TaskType: ScheduledTaskType.CleanTemporaryFiles,
|
||||
IsCycleRun: true,
|
||||
StartedOnUtc: DateTime.UtcNow.AddMinutes(-10),
|
||||
CompletedOnUtc: DateTime.UtcNow.AddMinutes(-9)
|
||||
)
|
||||
});
|
||||
|
||||
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/history"
|
||||
},
|
||||
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/history"
|
||||
},
|
||||
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/history"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/scheduled-jobs/history",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobs;
|
||||
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.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobs;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs</c> route.
|
||||
/// </summary>
|
||||
public class GetScheduledJobsEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetScheduledJobsQuery, Result<IEnumerable<ScheduledJobResponse>>> _getScheduledJobsQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetScheduledJobsEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getScheduledJobsQueryHandler">Injected service for handling get scheduled jobs queries.</param>
|
||||
public GetScheduledJobsEndpoint(IQueryHandler<GetScheduledJobsQuery, Result<IEnumerable<ScheduledJobResponse>>> getScheduledJobsQueryHandler)
|
||||
{
|
||||
_getScheduledJobsQueryHandler = getScheduledJobsQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.GET);
|
||||
Routes(ApiRoutes.ScheduledJobs.GET_SCHEDULED_JOBS);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of scheduled jobs.
|
||||
/// </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(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<IEnumerable<ScheduledJobResponse>> result = await _getScheduledJobsQueryHandler.HandleAsync(new GetScheduledJobsQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobs;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetScheduledJobsEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetScheduledJobsEndpointSummary : Summary<GetScheduledJobsEndpoint, EmptyRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetScheduledJobsEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetScheduledJobsEndpointSummary()
|
||||
{
|
||||
Summary = "Gets the list of scheduled jobs.";
|
||||
Description = "Gets the list of scheduled jobs and their status. Only administrators can get the list of scheduled jobs.";
|
||||
|
||||
Response(200, "The list of scheduled jobs was successfully retrieved.",
|
||||
example: new[]
|
||||
{
|
||||
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.AddHours(-2),
|
||||
LastCompletedOnUtc: DateTime.UtcNow.AddHours(-2).AddMinutes(5)
|
||||
)
|
||||
});
|
||||
|
||||
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"
|
||||
},
|
||||
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"
|
||||
},
|
||||
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"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/scheduled-jobs",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Commands.RemoveScheduledJob;
|
||||
using Lumina.Contracts.Requests.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.RemoveScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs/{scheduledJobId}</c> route.
|
||||
/// </summary>
|
||||
public class RemoveScheduledJobEndpoint : BaseEndpoint<RemoveScheduledJobRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<RemoveScheduledJobCommand, Result<Success>> _removeScheduledJobCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RemoveScheduledJobEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="removeScheduledJobCommandHandler">Injected service for handling remove scheduled job commands.</param>
|
||||
public RemoveScheduledJobEndpoint(ICommandHandler<RemoveScheduledJobCommand, Result<Success>> removeScheduledJobCommandHandler)
|
||||
{
|
||||
_removeScheduledJobCommandHandler = removeScheduledJobCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.DELETE);
|
||||
Routes(ApiRoutes.ScheduledJobs.REMOVE_SCHEDULED_JOB);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes 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(RemoveScheduledJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<Success> result = await _removeScheduledJobCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(_ => TypedResults.Ok(new { }), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.Scheduling;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Scheduling.RemoveScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="RemoveScheduledJobEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RemoveScheduledJobEndpointSummary : Summary<RemoveScheduledJobEndpoint, RemoveScheduledJobRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RemoveScheduledJobEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public RemoveScheduledJobEndpointSummary()
|
||||
{
|
||||
Summary = "Removes a scheduled job.";
|
||||
Description = "Removes a scheduled job, after stopping its execution cycle, if it has one. Only administrators can remove scheduled jobs.";
|
||||
|
||||
ExampleRequest = new RemoveScheduledJobRequest(ScheduledJobId: Guid.NewGuid());
|
||||
|
||||
RequestParam(request => request.ScheduledJobId, "The unique identifier of the scheduled job to remove. Required.");
|
||||
|
||||
Response(200, "The scheduled job was successfully removed.");
|
||||
|
||||
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}"
|
||||
},
|
||||
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}"
|
||||
},
|
||||
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}"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator.", "application/problem+json",
|
||||
example: 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}",
|
||||
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}",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+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"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Scheduling;
|
||||
using Lumina.Application.Core.Scheduling.Commands.StopScheduledJob;
|
||||
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.StopScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/scheduled-jobs/{scheduledJobId}/stop</c> route.
|
||||
/// </summary>
|
||||
public class StopScheduledJobEndpoint : BaseEndpoint<StopScheduledJobRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<StopScheduledJobCommand, Result<ScheduledJobResponse>> _stopScheduledJobCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopScheduledJobEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="stopScheduledJobCommandHandler">Injected service for handling stop scheduled job commands.</param>
|
||||
public StopScheduledJobEndpoint(ICommandHandler<StopScheduledJobCommand, Result<ScheduledJobResponse>> stopScheduledJobCommandHandler)
|
||||
{
|
||||
_stopScheduledJobCommandHandler = stopScheduledJobCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.PUT);
|
||||
Routes(ApiRoutes.ScheduledJobs.STOP_SCHEDULED_JOB);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops 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(StopScheduledJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ScheduledJobResponse> result = await _stopScheduledJobCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#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.StopScheduledJob;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StopScheduledJobEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StopScheduledJobEndpointSummary : Summary<StopScheduledJobEndpoint, StopScheduledJobRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopScheduledJobEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StopScheduledJobEndpointSummary()
|
||||
{
|
||||
Summary = "Stops the execution cycle of a scheduled job.";
|
||||
Description = "Stops the execution cycle of a scheduled job, without removing it. Only administrators can stop scheduled jobs.";
|
||||
|
||||
ExampleRequest = new StopScheduledJobRequest(ScheduledJobId: Guid.NewGuid());
|
||||
|
||||
RequestParam(request => request.ScheduledJobId, "The unique identifier of the scheduled job whose execution cycle is stopped. Required.");
|
||||
|
||||
Response(200, "The execution cycle of the scheduled job was successfully stopped.",
|
||||
example: new ScheduledJobResponse(
|
||||
Id: Guid.NewGuid(),
|
||||
Name: "Rescan media libraries",
|
||||
TaskType: ScheduledTaskType.ScanMediaLibraries,
|
||||
ScheduleType: ScheduleType.DailyAtHourAndMinute,
|
||||
IntervalMinutes: null,
|
||||
Hour: 3,
|
||||
Minute: 0,
|
||||
Status: ScheduledJobStatus.Added,
|
||||
LastStartedOnUtc: DateTime.UtcNow.AddMinutes(-1),
|
||||
LastCompletedOnUtc: DateTime.UtcNow
|
||||
));
|
||||
|
||||
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}/stop"
|
||||
},
|
||||
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}/stop"
|
||||
},
|
||||
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}/stop"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an administrator, or because the execution cycle of the scheduled job was not started, or its task is currently running.", "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}/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "ScheduledJobNotStarted",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "ScheduledJobAlreadyRunning",
|
||||
instance = "/api/v1/scheduled-jobs/{scheduledJobId}/stop",
|
||||
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}/stop",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user