* Fixed Hangfire update jobs
This commit is contained in:
@@ -2,7 +2,6 @@ using System.Diagnostics;
|
|||||||
using FaKrosnoApi.Models;
|
using FaKrosnoApi.Models;
|
||||||
using Hangfire;
|
using Hangfire;
|
||||||
using Hangfire.Storage;
|
using Hangfire.Storage;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using OrdersManagementDataModel.Dtos;
|
using OrdersManagementDataModel.Dtos;
|
||||||
using OrdersManagementDataModel.Services;
|
using OrdersManagementDataModel.Services;
|
||||||
@@ -16,155 +15,170 @@ public class HangfireJobsController(
|
|||||||
IRecurringJobManager recurringJobManager,
|
IRecurringJobManager recurringJobManager,
|
||||||
ITaskSchedulerService service) : Controller
|
ITaskSchedulerService service) : Controller
|
||||||
{
|
{
|
||||||
|
public async Task<ActionResult<IEnumerable<JobModel>>> GetJobsToRun()
|
||||||
|
{
|
||||||
|
IList<JobModel> jobsToRun = new List<JobModel>();
|
||||||
|
|
||||||
// public async Task<ActionResult<IEnumerable<JobModel>>> GetJobsToRun()
|
using (IStorageConnection? connection = jobStorage.GetConnection())
|
||||||
// {
|
{
|
||||||
// IList<JobModel> jobsToRun = new List<JobModel>();
|
IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
||||||
//
|
IList<TaskSchedulerDto>? taskSchedulers = (await service.GetTaskSchedulers()).ToList();
|
||||||
// using (IStorageConnection? connection = jobStorage.GetConnection())
|
|
||||||
// {
|
foreach (var recurringJob in recurringJobs)
|
||||||
// IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
{
|
||||||
// IList<TaskSchedulerDto>? taskSchedulers = (await service.GetTaskSchedulers()).ToList();
|
TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id);
|
||||||
//
|
|
||||||
// foreach (var recurringJob in recurringJobs)
|
if (taskScheduler != null)
|
||||||
// {
|
{
|
||||||
// TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id);
|
jobsToRun.Add(new JobModel(recurringJob.Id, recurringJob.Cron, taskScheduler.Path,
|
||||||
//
|
recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job));
|
||||||
// if (taskScheduler != null)
|
}
|
||||||
// {
|
}
|
||||||
// jobsToRun.Add(new JobModel(recurringJob.Id, recurringJob.Cron, taskScheduler.Path,
|
}
|
||||||
// recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job));
|
|
||||||
// }
|
return Ok(jobsToRun);
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
//
|
[HttpPost("run")]
|
||||||
// return Ok(jobsToRun);
|
public async Task<IActionResult> RunJobs()
|
||||||
// }
|
{
|
||||||
//
|
var jobsToRun = (await GetJobsToRun()).Value?.ToList();
|
||||||
// [HttpPost("run")]
|
|
||||||
// public async Task<IActionResult> RunJobs()
|
if (jobsToRun == null || jobsToRun.Count == 0)
|
||||||
// {
|
{
|
||||||
// var jobsToRun = (await GetJobsToRun()).Value?.ToList();
|
return BadRequest("Nie udało się pobrać zadań do uruchomienia.");
|
||||||
//
|
}
|
||||||
// if (jobsToRun == null || jobsToRun.Count == 0)
|
|
||||||
// {
|
foreach (var job in jobsToRun)
|
||||||
// return BadRequest("Nie udało się pobrać zadań do uruchomienia.");
|
{
|
||||||
// }
|
if (!string.IsNullOrEmpty(job.Path))
|
||||||
//
|
{
|
||||||
// foreach (var job in jobsToRun)
|
recurringJobManager.AddOrUpdate(job.JobId, () => RunConsoleApplication(job.Path), job.Cron,
|
||||||
// {
|
new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||||
// if (!string.IsNullOrEmpty(job.Path))
|
}
|
||||||
// {
|
}
|
||||||
// recurringJobManager.AddOrUpdate(job.JobId, () => RunConsoleApplication(job.Path), job.Cron,
|
|
||||||
// new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
return Ok("Zadania zostały zaplanowane do uruchamiania zgodnie z ich CRON.");
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
//
|
[HttpPost("add")]
|
||||||
// return Ok("Zadania zostały zaplanowane do uruchamiania zgodnie z ich CRON.");
|
public async Task<IActionResult> AddTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
||||||
// }
|
{
|
||||||
//
|
var taskScheduler = new OrdersManagementDataModel.Entities.TaskScheduler
|
||||||
// [HttpPost("add")]
|
{
|
||||||
// public async Task<IActionResult> AddTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
Name = taskSchedulerDto.Name,
|
||||||
// {
|
Path = taskSchedulerDto.Path,
|
||||||
// var taskScheduler = new OrdersManagementDataModel.Entities.TaskScheduler
|
CronOptions = taskSchedulerDto.CronOptions,
|
||||||
// {
|
CreateDate = DateTime.UtcNow
|
||||||
// Name = taskSchedulerDto.Name,
|
};
|
||||||
// Path = taskSchedulerDto.Path,
|
|
||||||
// CronOptions = taskSchedulerDto.CronOptions,
|
int result = await service.AddTaskScheduler(taskSchedulerDto);
|
||||||
// CreateDate = DateTime.UtcNow
|
|
||||||
// };
|
if (result == 0)
|
||||||
//
|
{
|
||||||
// int result = await service.AddTaskScheduler(taskSchedulerDto);
|
return BadRequest("Nie udało się dodać zadania.");
|
||||||
//
|
}
|
||||||
// if (result == 0)
|
|
||||||
// {
|
recurringJobManager.AddOrUpdate(taskScheduler.Name, () => RunConsoleApplication(taskScheduler.Path),
|
||||||
// return BadRequest("Nie udało się dodać zadania.");
|
taskScheduler.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||||
// }
|
|
||||||
//
|
return Ok("Zadanie zostało dodane.");
|
||||||
// recurringJobManager.AddOrUpdate(taskScheduler.Name, () => RunConsoleApplication(taskScheduler.Path),
|
}
|
||||||
// taskScheduler.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
|
||||||
//
|
[HttpPost("delete")]
|
||||||
// return Ok("Zadanie zostało dodane.");
|
public async Task<IActionResult> DeleteTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
||||||
// }
|
{
|
||||||
//
|
int result = await service.DeleteTaskScheduler(taskSchedulerDto.RowPointer);
|
||||||
// [HttpPost("delete")]
|
|
||||||
// public async Task<IActionResult> DeleteTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
if (result == 0)
|
||||||
// {
|
{
|
||||||
// int result = await service.DeleteTaskScheduler(taskSchedulerDto.RowPointer);
|
return BadRequest("Nie udało się usunąć zadania.");
|
||||||
//
|
}
|
||||||
// if (result == 0)
|
|
||||||
// {
|
recurringJobManager.RemoveIfExists(taskSchedulerDto.Name);
|
||||||
// return BadRequest("Nie udało się usunąć zadania.");
|
|
||||||
// }
|
return Ok("Zadanie zostało usunięte.");
|
||||||
//
|
}
|
||||||
// recurringJobManager.RemoveIfExists(taskSchedulerDto.Name);
|
|
||||||
//
|
[HttpPost("update")]
|
||||||
// return Ok("Zadanie zostało usunięte.");
|
public async Task<IActionResult> UpdateTask([FromBody] TaskSchedulerDto taskSchedulerDto)
|
||||||
// }
|
{
|
||||||
//
|
int result = await service.UpdateTaskScheduler(taskSchedulerDto);
|
||||||
// [HttpGet]
|
|
||||||
// public async Task<ActionResult<IEnumerable<TaskSchedulerDto>>> GetTasks()
|
if (result == 0)
|
||||||
// {
|
{
|
||||||
// var tasks = await service.GetTaskSchedulers();
|
return BadRequest("Nie udało się uaktualnic zadania.");
|
||||||
//
|
}
|
||||||
// foreach (TaskSchedulerDto taskSchedulerDto in tasks)
|
|
||||||
// {
|
recurringJobManager.AddOrUpdate(taskSchedulerDto.Name, () => RunConsoleApplication(taskSchedulerDto.Path),
|
||||||
// var job = GetJob(taskSchedulerDto.Name);
|
taskSchedulerDto.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||||
// taskSchedulerDto.LastExecution = job?.LastExecution;
|
|
||||||
// taskSchedulerDto.NextExecution = job?.NextExecution;
|
return Ok("Zadanie zostało zaktualizowane.");
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return Ok(tasks);
|
[HttpGet]
|
||||||
// }
|
public async Task<ActionResult<IEnumerable<TaskSchedulerDto>>> GetTasks()
|
||||||
//
|
{
|
||||||
// [HttpGet("by-name")]
|
var tasks = await service.GetTaskSchedulers();
|
||||||
// public async Task<ActionResult<TaskSchedulerDto>> GetTaskSchedulerByTaskName([FromQuery] string name)
|
|
||||||
// {
|
foreach (TaskSchedulerDto taskSchedulerDto in tasks)
|
||||||
// var taskSchedulerDto = await service.GetTaskSchedulerByTaskName(name);
|
{
|
||||||
//
|
var job = GetJob(taskSchedulerDto.Name);
|
||||||
// if (taskSchedulerDto == null) return NotFound();
|
taskSchedulerDto.LastExecution = job?.LastExecution;
|
||||||
//
|
taskSchedulerDto.NextExecution = job?.NextExecution;
|
||||||
// var job = GetJob(taskSchedulerDto.Name);
|
}
|
||||||
// taskSchedulerDto.LastExecution = job?.LastExecution;
|
|
||||||
// taskSchedulerDto.NextExecution = job?.NextExecution;
|
return Ok(tasks);
|
||||||
//
|
}
|
||||||
// return Ok(taskSchedulerDto);
|
|
||||||
// }
|
[HttpGet("by-name")]
|
||||||
//
|
public async Task<ActionResult<TaskSchedulerDto>> GetTaskSchedulerByTaskName([FromQuery] string name)
|
||||||
// private RecurringJobDto? GetJob(string jobId)
|
{
|
||||||
// {
|
var taskSchedulerDto = await service.GetTaskSchedulerByTaskName(name);
|
||||||
// using IStorageConnection? connection = jobStorage.GetConnection();
|
|
||||||
// IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
if (taskSchedulerDto == null) return NotFound();
|
||||||
// return recurringJobs.FirstOrDefault(x => x.Id == jobId);
|
|
||||||
// }
|
var job = GetJob(taskSchedulerDto.Name);
|
||||||
//
|
taskSchedulerDto.LastExecution = job?.LastExecution;
|
||||||
// public void RunConsoleApplication(string pathToApp)
|
taskSchedulerDto.NextExecution = job?.NextExecution;
|
||||||
// {
|
|
||||||
// try
|
return Ok(taskSchedulerDto);
|
||||||
// {
|
}
|
||||||
// var process = new Process
|
|
||||||
// {
|
private RecurringJobDto? GetJob(string jobId)
|
||||||
// StartInfo = new ProcessStartInfo
|
{
|
||||||
// {
|
using IStorageConnection? connection = jobStorage.GetConnection();
|
||||||
// FileName = pathToApp,
|
IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
||||||
// UseShellExecute = false,
|
return recurringJobs.FirstOrDefault(x => x.Id == jobId);
|
||||||
// RedirectStandardOutput = true,
|
}
|
||||||
// RedirectStandardError = true,
|
|
||||||
// CreateNoWindow = true,
|
public void RunConsoleApplication(string pathToApp)
|
||||||
// WorkingDirectory = Path.GetDirectoryName(pathToApp)
|
{
|
||||||
// }
|
try
|
||||||
// };
|
{
|
||||||
// process.Start();
|
var process = new Process
|
||||||
// string output = process.StandardOutput.ReadToEnd();
|
{
|
||||||
// string error = process.StandardError.ReadToEnd();
|
StartInfo = new ProcessStartInfo
|
||||||
// process.WaitForExit();
|
{
|
||||||
//
|
FileName = pathToApp,
|
||||||
// Console.WriteLine($"Output: {output}");
|
UseShellExecute = false,
|
||||||
// Console.WriteLine($"Error: {error}");
|
RedirectStandardOutput = true,
|
||||||
// }
|
RedirectStandardError = true,
|
||||||
// catch (Exception ex)
|
CreateNoWindow = true,
|
||||||
// {
|
WorkingDirectory = Path.GetDirectoryName(pathToApp)
|
||||||
// Console.WriteLine($"Error executing console application: {ex.Message}");
|
}
|
||||||
// }
|
};
|
||||||
// }
|
process.Start();
|
||||||
|
string output = process.StandardOutput.ReadToEnd();
|
||||||
|
string error = process.StandardError.ReadToEnd();
|
||||||
|
process.WaitForExit();
|
||||||
|
|
||||||
|
Console.WriteLine($"Output: {output}");
|
||||||
|
Console.WriteLine($"Error: {error}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error executing console application: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,17 @@
|
|||||||
await LoadTasks();
|
await LoadTasks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task UpdateTask(TaskSchedulerDto taskSchedulerDto)
|
||||||
|
{
|
||||||
|
var response = await HangfireService.UpdateTaskSchedulerAsync(taskSchedulerDto);
|
||||||
|
if (response == 1)
|
||||||
|
{
|
||||||
|
await LoadTasks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async Task LoadTasks()
|
private async Task LoadTasks()
|
||||||
{
|
{
|
||||||
@@ -108,6 +119,9 @@
|
|||||||
case Action.Save when args.Data.Id == 0:
|
case Action.Save when args.Data.Id == 0:
|
||||||
await AddTask(args.Data);
|
await AddTask(args.Data);
|
||||||
break;
|
break;
|
||||||
|
case Action.Save when args.Data.Id != 0:
|
||||||
|
await UpdateTask(args.Data);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,4 +28,10 @@ public class HangfireService(
|
|||||||
HttpResponseMessage responseMessage = await PostAsJsonAsync("api/HangfireJobs/delete", taskSchedulerDto);
|
HttpResponseMessage responseMessage = await PostAsJsonAsync("api/HangfireJobs/delete", taskSchedulerDto);
|
||||||
return responseMessage.IsSuccessStatusCode ? 1 : 0;
|
return responseMessage.IsSuccessStatusCode ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> UpdateTaskSchedulerAsync(TaskSchedulerDto taskSchedulerDto)
|
||||||
|
{
|
||||||
|
HttpResponseMessage responseMessage = await PostAsJsonAsync("api/HangfireJobs/update", taskSchedulerDto);
|
||||||
|
return responseMessage.IsSuccessStatusCode ? 1 : 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user