Improve workers for backtests
This commit is contained in:
@@ -933,7 +933,7 @@ public class BacktestController : BaseController
|
||||
var user = await GetUser();
|
||||
|
||||
// Create genetic request using the GeneticService directly
|
||||
var geneticRequest = _geneticService.CreateGeneticRequest(
|
||||
var geneticRequest = await _geneticService.CreateGeneticRequestAsync(
|
||||
user,
|
||||
request.Ticker,
|
||||
request.Timeframe,
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Text.Json;
|
||||
using Managing.Api.Models.Responses;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Backtests;
|
||||
using Managing.Application.Shared;
|
||||
using Managing.Domain.Backtests;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -25,6 +26,7 @@ public class JobController : BaseController
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly IAdminConfigurationService _adminService;
|
||||
private readonly ILogger<JobController> _logger;
|
||||
private readonly JobService _jobService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JobController"/> class.
|
||||
@@ -32,15 +34,18 @@ public class JobController : BaseController
|
||||
/// <param name="userService">The service for user management.</param>
|
||||
/// <param name="serviceScopeFactory">The service scope factory for creating scoped services.</param>
|
||||
/// <param name="adminService">The admin configuration service for authorization checks.</param>
|
||||
/// <param name="jobService">The job service for job operations.</param>
|
||||
/// <param name="logger">The logger instance.</param>
|
||||
public JobController(
|
||||
IUserService userService,
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
IAdminConfigurationService adminService,
|
||||
JobService jobService,
|
||||
ILogger<JobController> logger) : base(userService)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_adminService = adminService;
|
||||
_jobService = jobService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -284,5 +289,77 @@ public class JobController : BaseController
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retries a failed or cancelled job by resetting it to Pending status.
|
||||
/// Admin only endpoint.
|
||||
/// </summary>
|
||||
/// <param name="jobId">The job ID to retry</param>
|
||||
/// <returns>Success response</returns>
|
||||
[HttpPost("{jobId}/retry")]
|
||||
public async Task<ActionResult> RetryJob(string jobId)
|
||||
{
|
||||
if (!await IsUserAdmin())
|
||||
{
|
||||
_logger.LogWarning("Non-admin user attempted to retry job");
|
||||
return StatusCode(403, new { error = "Only admin users can retry jobs" });
|
||||
}
|
||||
|
||||
if (!Guid.TryParse(jobId, out var jobGuid))
|
||||
{
|
||||
return BadRequest("Invalid job ID format. Must be a valid GUID.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var job = await _jobService.RetryJobAsync(jobGuid);
|
||||
|
||||
return Ok(new { message = $"Job {jobId} has been reset to Pending status and will be picked up by workers.", jobId = job.Id });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
if (ex.Message.Contains("not found"))
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a job from the database.
|
||||
/// Admin only endpoint.
|
||||
/// </summary>
|
||||
/// <param name="jobId">The job ID to delete</param>
|
||||
/// <returns>Success response</returns>
|
||||
[HttpDelete("{jobId}")]
|
||||
public async Task<ActionResult> DeleteJob(string jobId)
|
||||
{
|
||||
if (!await IsUserAdmin())
|
||||
{
|
||||
_logger.LogWarning("Non-admin user attempted to delete job");
|
||||
return StatusCode(403, new { error = "Only admin users can delete jobs" });
|
||||
}
|
||||
|
||||
if (!Guid.TryParse(jobId, out var jobGuid))
|
||||
{
|
||||
return BadRequest("Invalid job ID format. Must be a valid GUID.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _jobService.DeleteJobAsync(jobGuid);
|
||||
|
||||
return Ok(new { message = $"Job {jobId} has been deleted successfully.", jobId });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
if (ex.Message.Contains("not found"))
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user