using System.Diagnostics; using FaKrosnoApi.Models; using Hangfire; using Hangfire.Storage; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using OrdersManagementDataModel.Dtos; using OrdersManagementDataModel.Services; namespace FaKrosnoApi.Controllers; [ApiController] [Route("api/[controller]")] public class HangfireJobsController( JobStorage jobStorage, IRecurringJobManager recurringJobManager, ITaskSchedulerService service) : Controller { // public async Task>> GetJobsToRun() // { // IList jobsToRun = new List(); // // using (IStorageConnection? connection = jobStorage.GetConnection()) // { // IList? recurringJobs = connection.GetRecurringJobs(); // IList? taskSchedulers = (await service.GetTaskSchedulers()).ToList(); // // foreach (var recurringJob in recurringJobs) // { // TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id); // // if (taskScheduler != null) // { // jobsToRun.Add(new JobModel(recurringJob.Id, recurringJob.Cron, taskScheduler.Path, // recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job)); // } // } // } // // return Ok(jobsToRun); // } // // [HttpPost("run")] // public async Task RunJobs() // { // var jobsToRun = (await GetJobsToRun()).Value?.ToList(); // // if (jobsToRun == null || jobsToRun.Count == 0) // { // return BadRequest("Nie udało się pobrać zadań do uruchomienia."); // } // // foreach (var job in jobsToRun) // { // 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")] // public async Task AddTask([FromBody] TaskSchedulerDto taskSchedulerDto) // { // var taskScheduler = new OrdersManagementDataModel.Entities.TaskScheduler // { // Name = taskSchedulerDto.Name, // Path = taskSchedulerDto.Path, // CronOptions = taskSchedulerDto.CronOptions, // CreateDate = DateTime.UtcNow // }; // // int result = await service.AddTaskScheduler(taskSchedulerDto); // // if (result == 0) // { // return BadRequest("Nie udało się dodać zadania."); // } // // recurringJobManager.AddOrUpdate(taskScheduler.Name, () => RunConsoleApplication(taskScheduler.Path), // taskScheduler.CronOptions, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local }); // // return Ok("Zadanie zostało dodane."); // } // // [HttpPost("delete")] // public async Task DeleteTask([FromBody] TaskSchedulerDto taskSchedulerDto) // { // int result = await service.DeleteTaskScheduler(taskSchedulerDto.RowPointer); // // if (result == 0) // { // return BadRequest("Nie udało się usunąć zadania."); // } // // recurringJobManager.RemoveIfExists(taskSchedulerDto.Name); // // return Ok("Zadanie zostało usunięte."); // } // // [HttpGet] // public async Task>> GetTasks() // { // var tasks = await service.GetTaskSchedulers(); // // foreach (TaskSchedulerDto taskSchedulerDto in tasks) // { // var job = GetJob(taskSchedulerDto.Name); // taskSchedulerDto.LastExecution = job?.LastExecution; // taskSchedulerDto.NextExecution = job?.NextExecution; // } // // return Ok(tasks); // } // // [HttpGet("by-name")] // public async Task> GetTaskSchedulerByTaskName([FromQuery] string name) // { // var taskSchedulerDto = await service.GetTaskSchedulerByTaskName(name); // // if (taskSchedulerDto == null) return NotFound(); // // var job = GetJob(taskSchedulerDto.Name); // taskSchedulerDto.LastExecution = job?.LastExecution; // taskSchedulerDto.NextExecution = job?.NextExecution; // // return Ok(taskSchedulerDto); // } // // private RecurringJobDto? GetJob(string jobId) // { // using IStorageConnection? connection = jobStorage.GetConnection(); // IList? recurringJobs = connection.GetRecurringJobs(); // return recurringJobs.FirstOrDefault(x => x.Id == jobId); // } // // public void RunConsoleApplication(string pathToApp) // { // try // { // var process = new Process // { // StartInfo = new ProcessStartInfo // { // FileName = pathToApp, // UseShellExecute = false, // RedirectStandardOutput = true, // RedirectStandardError = true, // CreateNoWindow = true, // WorkingDirectory = Path.GetDirectoryName(pathToApp) // } // }; // 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}"); // } // } }