This commit is contained in:
Flaminel
2025-05-19 12:35:58 +03:00
parent c675924be7
commit 3d9b286206
7 changed files with 254 additions and 56 deletions

View File

@@ -1,4 +1,7 @@
using Executable.Models;
using Infrastructure.Models;
using Infrastructure.Services.Interfaces;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Mvc;
namespace Executable.Controllers;
@@ -31,122 +34,131 @@ public class JobsController : ControllerBase
}
}
[HttpGet("{jobName}")]
public async Task<IActionResult> GetJob(string jobName)
[HttpGet("{jobType}")]
public async Task<IActionResult> GetJob(JobType jobType)
{
try
{
var jobInfo = await _jobManagementService.GetJob(jobName);
var jobInfo = await _jobManagementService.GetJob(jobType);
if (jobInfo.Status == "Not Found")
{
return NotFound($"Job '{jobName}' not found");
return NotFound($"Job '{jobType}' not found");
}
return Ok(jobInfo);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting job {jobName}", jobName);
return StatusCode(500, $"An error occurred while retrieving job '{jobName}'");
_logger.LogError(ex, "Error getting job {jobType}", jobType);
return StatusCode(500, $"An error occurred while retrieving job '{jobType}'");
}
}
[HttpPost("{jobName}/start")]
public async Task<IActionResult> StartJob(string jobName, [FromQuery] string cronExpression = null)
[HttpPost("{jobType}/start")]
public async Task<IActionResult> StartJob(JobType jobType, [FromBody] ScheduleRequest scheduleRequest = null)
{
try
{
var result = await _jobManagementService.StartJob(jobName, cronExpression);
// Get the schedule from the request body if provided
JobSchedule jobSchedule = scheduleRequest?.Schedule;
var result = await _jobManagementService.StartJob(jobType, jobSchedule);
if (!result)
{
return BadRequest($"Failed to start job '{jobName}'");
return BadRequest($"Failed to start job '{jobType}'");
}
return Ok(new { Message = $"Job '{jobName}' started successfully" });
return Ok(new { Message = $"Job '{jobType}' started successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error starting job {jobName}", jobName);
return StatusCode(500, $"An error occurred while starting job '{jobName}'");
_logger.LogError(ex, "Error starting job {jobType}", jobType);
return StatusCode(500, $"An error occurred while starting job '{jobType}'");
}
}
[HttpPost("{jobName}/stop")]
public async Task<IActionResult> StopJob(string jobName)
[HttpPost("{jobType}/stop")]
public async Task<IActionResult> StopJob(JobType jobType)
{
try
{
var result = await _jobManagementService.StopJob(jobName);
var result = await _jobManagementService.StopJob(jobType);
if (!result)
{
return BadRequest($"Failed to stop job '{jobName}'");
return BadRequest($"Failed to stop job '{jobType}'");
}
return Ok(new { Message = $"Job '{jobName}' stopped successfully" });
return Ok(new { Message = $"Job '{jobType}' stopped successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error stopping job {jobName}", jobName);
return StatusCode(500, $"An error occurred while stopping job '{jobName}'");
_logger.LogError(ex, "Error stopping job {jobType}", jobType);
return StatusCode(500, $"An error occurred while stopping job '{jobType}'");
}
}
[HttpPost("{jobName}/pause")]
public async Task<IActionResult> PauseJob(string jobName)
[HttpPost("{jobType}/pause")]
public async Task<IActionResult> PauseJob(JobType jobType)
{
try
{
var result = await _jobManagementService.PauseJob(jobName);
var result = await _jobManagementService.PauseJob(jobType);
if (!result)
{
return BadRequest($"Failed to pause job '{jobName}'");
return BadRequest($"Failed to pause job '{jobType}'");
}
return Ok(new { Message = $"Job '{jobName}' paused successfully" });
return Ok(new { Message = $"Job '{jobType}' paused successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error pausing job {jobName}", jobName);
return StatusCode(500, $"An error occurred while pausing job '{jobName}'");
_logger.LogError(ex, "Error pausing job {jobType}", jobType);
return StatusCode(500, $"An error occurred while pausing job '{jobType}'");
}
}
[HttpPost("{jobName}/resume")]
public async Task<IActionResult> ResumeJob(string jobName)
[HttpPost("{jobType}/resume")]
public async Task<IActionResult> ResumeJob(JobType jobType)
{
try
{
var result = await _jobManagementService.ResumeJob(jobName);
var result = await _jobManagementService.ResumeJob(jobType);
if (!result)
{
return BadRequest($"Failed to resume job '{jobName}'");
return BadRequest($"Failed to resume job '{jobType}'");
}
return Ok(new { Message = $"Job '{jobName}' resumed successfully" });
return Ok(new { Message = $"Job '{jobType}' resumed successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error resuming job {jobName}", jobName);
return StatusCode(500, $"An error occurred while resuming job '{jobName}'");
_logger.LogError(ex, "Error resuming job {jobType}", jobType);
return StatusCode(500, $"An error occurred while resuming job '{jobType}'");
}
}
[HttpPut("{jobName}/schedule")]
public async Task<IActionResult> UpdateJobSchedule(string jobName, [FromQuery] string cronExpression)
[HttpPut("{jobType}/schedule")]
public async Task<IActionResult> UpdateJobSchedule(JobType jobType, [FromBody] ScheduleRequest scheduleRequest)
{
if (string.IsNullOrEmpty(cronExpression))
if (scheduleRequest?.Schedule == null)
{
return BadRequest("Cron expression is required");
return BadRequest("Schedule is required");
}
try
{
var result = await _jobManagementService.UpdateJobSchedule(jobName, cronExpression);
var result = await _jobManagementService.UpdateJobSchedule(jobType, scheduleRequest.Schedule);
if (!result)
{
return BadRequest($"Failed to update schedule for job '{jobName}'");
return BadRequest($"Failed to update schedule for job '{jobType}'");
}
return Ok(new { Message = $"Job '{jobName}' schedule updated successfully" });
return Ok(new { Message = $"Job '{jobType}' schedule updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating job {jobName} schedule", jobName);
return StatusCode(500, $"An error occurred while updating schedule for job '{jobName}'");
_logger.LogError(ex, "Error updating job {jobType} schedule", jobType);
return StatusCode(500, $"An error occurred while updating schedule for job '{jobType}'");
}
}
}

View File

@@ -0,0 +1,14 @@
namespace Executable.Models;
using Infrastructure.Models;
/// <summary>
/// Represents a request to schedule a job
/// </summary>
public class ScheduleRequest
{
/// <summary>
/// The schedule information for the job
/// </summary>
public JobSchedule Schedule { get; set; }
}