* Fixed Validator
This commit is contained in:
@@ -17,154 +17,154 @@ public class HangfireJobsController(
|
|||||||
ITaskSchedulerService service) : Controller
|
ITaskSchedulerService service) : Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
public async Task<ActionResult<IEnumerable<JobModel>>> GetJobsToRun()
|
// public async Task<ActionResult<IEnumerable<JobModel>>> GetJobsToRun()
|
||||||
{
|
// {
|
||||||
IList<JobModel> jobsToRun = new List<JobModel>();
|
// IList<JobModel> jobsToRun = new List<JobModel>();
|
||||||
|
//
|
||||||
using (IStorageConnection? connection = jobStorage.GetConnection())
|
// using (IStorageConnection? connection = jobStorage.GetConnection())
|
||||||
{
|
// {
|
||||||
IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
// IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
||||||
IList<TaskSchedulerDto>? taskSchedulers = (await service.GetTaskSchedulers()).ToList();
|
// IList<TaskSchedulerDto>? taskSchedulers = (await service.GetTaskSchedulers()).ToList();
|
||||||
|
//
|
||||||
foreach (var recurringJob in recurringJobs)
|
// foreach (var recurringJob in recurringJobs)
|
||||||
{
|
// {
|
||||||
TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id);
|
// TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id);
|
||||||
|
//
|
||||||
if (taskScheduler != null)
|
// if (taskScheduler != null)
|
||||||
{
|
// {
|
||||||
jobsToRun.Add(new JobModel(recurringJob.Id, recurringJob.Cron, taskScheduler.Path,
|
// jobsToRun.Add(new JobModel(recurringJob.Id, recurringJob.Cron, taskScheduler.Path,
|
||||||
recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job));
|
// recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return Ok(jobsToRun);
|
// return Ok(jobsToRun);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[HttpPost("run")]
|
// [HttpPost("run")]
|
||||||
public async Task<IActionResult> RunJobs()
|
// public async Task<IActionResult> RunJobs()
|
||||||
{
|
// {
|
||||||
var jobsToRun = (await GetJobsToRun()).Value?.ToList();
|
// var jobsToRun = (await GetJobsToRun()).Value?.ToList();
|
||||||
|
//
|
||||||
if (jobsToRun == null || jobsToRun.Count == 0)
|
// if (jobsToRun == null || jobsToRun.Count == 0)
|
||||||
{
|
// {
|
||||||
return BadRequest("Nie udało się pobrać zadań do uruchomienia.");
|
// return BadRequest("Nie udało się pobrać zadań do uruchomienia.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
foreach (var job in jobsToRun)
|
// foreach (var job in jobsToRun)
|
||||||
{
|
// {
|
||||||
if (!string.IsNullOrEmpty(job.Path))
|
// if (!string.IsNullOrEmpty(job.Path))
|
||||||
{
|
// {
|
||||||
recurringJobManager.AddOrUpdate(job.JobId, () => RunConsoleApplication(job.Path), job.Cron,
|
// recurringJobManager.AddOrUpdate(job.JobId, () => RunConsoleApplication(job.Path), job.Cron,
|
||||||
new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
// new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return Ok("Zadania zostały zaplanowane do uruchamiania zgodnie z ich CRON.");
|
// return Ok("Zadania zostały zaplanowane do uruchamiania zgodnie z ich CRON.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[HttpPost("add")]
|
// [HttpPost("add")]
|
||||||
public async Task<IActionResult> AddTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
// public async Task<IActionResult> AddTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
||||||
{
|
// {
|
||||||
var taskScheduler = new OrdersManagementDataModel.Entities.TaskScheduler
|
// var taskScheduler = new OrdersManagementDataModel.Entities.TaskScheduler
|
||||||
{
|
// {
|
||||||
Name = taskSchedulerDto.Name,
|
// Name = taskSchedulerDto.Name,
|
||||||
Path = taskSchedulerDto.Path,
|
// Path = taskSchedulerDto.Path,
|
||||||
CronOptions = taskSchedulerDto.CronOptions,
|
// CronOptions = taskSchedulerDto.CronOptions,
|
||||||
CreateDate = DateTime.UtcNow
|
// CreateDate = DateTime.UtcNow
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
int result = await service.AddTaskScheduler(taskSchedulerDto);
|
// int result = await service.AddTaskScheduler(taskSchedulerDto);
|
||||||
|
//
|
||||||
if (result == 0)
|
// if (result == 0)
|
||||||
{
|
// {
|
||||||
return BadRequest("Nie udało się dodać zadania.");
|
// return BadRequest("Nie udało się dodać zadania.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
recurringJobManager.AddOrUpdate(taskScheduler.Name, () => RunConsoleApplication(taskScheduler.Path),
|
// recurringJobManager.AddOrUpdate(taskScheduler.Name, () => RunConsoleApplication(taskScheduler.Path),
|
||||||
taskScheduler.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
// taskScheduler.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||||
|
//
|
||||||
return Ok("Zadanie zostało dodane.");
|
// return Ok("Zadanie zostało dodane.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[HttpPost("delete")]
|
// [HttpPost("delete")]
|
||||||
public async Task<IActionResult> DeleteTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
// public async Task<IActionResult> DeleteTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
||||||
{
|
// {
|
||||||
int result = await service.DeleteTaskScheduler(taskSchedulerDto.RowPointer);
|
// int result = await service.DeleteTaskScheduler(taskSchedulerDto.RowPointer);
|
||||||
|
//
|
||||||
if (result == 0)
|
// if (result == 0)
|
||||||
{
|
// {
|
||||||
return BadRequest("Nie udało się usunąć zadania.");
|
// return BadRequest("Nie udało się usunąć zadania.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
recurringJobManager.RemoveIfExists(taskSchedulerDto.Name);
|
// recurringJobManager.RemoveIfExists(taskSchedulerDto.Name);
|
||||||
|
//
|
||||||
return Ok("Zadanie zostało usunięte.");
|
// return Ok("Zadanie zostało usunięte.");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[HttpGet]
|
// [HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<TaskSchedulerDto>>> GetTasks()
|
// public async Task<ActionResult<IEnumerable<TaskSchedulerDto>>> GetTasks()
|
||||||
{
|
// {
|
||||||
var tasks = await service.GetTaskSchedulers();
|
// var tasks = await service.GetTaskSchedulers();
|
||||||
|
//
|
||||||
foreach (TaskSchedulerDto taskSchedulerDto in tasks)
|
// foreach (TaskSchedulerDto taskSchedulerDto in tasks)
|
||||||
{
|
// {
|
||||||
var job = GetJob(taskSchedulerDto.Name);
|
// var job = GetJob(taskSchedulerDto.Name);
|
||||||
taskSchedulerDto.LastExecution = job?.LastExecution;
|
// taskSchedulerDto.LastExecution = job?.LastExecution;
|
||||||
taskSchedulerDto.NextExecution = job?.NextExecution;
|
// taskSchedulerDto.NextExecution = job?.NextExecution;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return Ok(tasks);
|
// return Ok(tasks);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[HttpGet("by-name")]
|
// [HttpGet("by-name")]
|
||||||
public async Task<ActionResult<TaskSchedulerDto>> GetTaskSchedulerByTaskName([FromQuery] string name)
|
// public async Task<ActionResult<TaskSchedulerDto>> GetTaskSchedulerByTaskName([FromQuery] string name)
|
||||||
{
|
// {
|
||||||
var taskSchedulerDto = await service.GetTaskSchedulerByTaskName(name);
|
// var taskSchedulerDto = await service.GetTaskSchedulerByTaskName(name);
|
||||||
|
//
|
||||||
if (taskSchedulerDto == null) return NotFound();
|
// if (taskSchedulerDto == null) return NotFound();
|
||||||
|
//
|
||||||
var job = GetJob(taskSchedulerDto.Name);
|
// var job = GetJob(taskSchedulerDto.Name);
|
||||||
taskSchedulerDto.LastExecution = job?.LastExecution;
|
// taskSchedulerDto.LastExecution = job?.LastExecution;
|
||||||
taskSchedulerDto.NextExecution = job?.NextExecution;
|
// taskSchedulerDto.NextExecution = job?.NextExecution;
|
||||||
|
//
|
||||||
return Ok(taskSchedulerDto);
|
// return Ok(taskSchedulerDto);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private RecurringJobDto? GetJob(string jobId)
|
// private RecurringJobDto? GetJob(string jobId)
|
||||||
{
|
// {
|
||||||
using IStorageConnection? connection = jobStorage.GetConnection();
|
// using IStorageConnection? connection = jobStorage.GetConnection();
|
||||||
IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
// IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
||||||
return recurringJobs.FirstOrDefault(x => x.Id == jobId);
|
// return recurringJobs.FirstOrDefault(x => x.Id == jobId);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void RunConsoleApplication(string pathToApp)
|
// public void RunConsoleApplication(string pathToApp)
|
||||||
{
|
// {
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
var process = new Process
|
// var process = new Process
|
||||||
{
|
// {
|
||||||
StartInfo = new ProcessStartInfo
|
// StartInfo = new ProcessStartInfo
|
||||||
{
|
// {
|
||||||
FileName = pathToApp,
|
// FileName = pathToApp,
|
||||||
UseShellExecute = false,
|
// UseShellExecute = false,
|
||||||
RedirectStandardOutput = true,
|
// RedirectStandardOutput = true,
|
||||||
RedirectStandardError = true,
|
// RedirectStandardError = true,
|
||||||
CreateNoWindow = true,
|
// CreateNoWindow = true,
|
||||||
WorkingDirectory = Path.GetDirectoryName(pathToApp)
|
// WorkingDirectory = Path.GetDirectoryName(pathToApp)
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
process.Start();
|
// process.Start();
|
||||||
string output = process.StandardOutput.ReadToEnd();
|
// string output = process.StandardOutput.ReadToEnd();
|
||||||
string error = process.StandardError.ReadToEnd();
|
// string error = process.StandardError.ReadToEnd();
|
||||||
process.WaitForExit();
|
// process.WaitForExit();
|
||||||
|
//
|
||||||
Console.WriteLine($"Output: {output}");
|
// Console.WriteLine($"Output: {output}");
|
||||||
Console.WriteLine($"Error: {error}");
|
// Console.WriteLine($"Error: {error}");
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
Console.WriteLine($"Error executing console application: {ex.Message}");
|
// Console.WriteLine($"Error executing console application: {ex.Message}");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user