using LCU.Api.Internal.Dispatcher.HostedServices; using LCU.Library.Internal.ApiSecurity.Attributes; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace LCU.Api.Internal.Dispatcher.Controllers { [ApiController] [Route("api/[controller]")] public class HostedServicesController : ControllerBase { private readonly ICleanUpJobsHostedService _cleanUpJobsHostedServiceStarter; private readonly IBatchErroredJobsHostedService _batchErroredJobsServiceStarter; private readonly IJobSchedulerHostedService _jobSchedulerHostedServiceStarter; public HostedServicesController(ICleanUpJobsHostedService cleanUpJobsHostedService, IBatchErroredJobsHostedService batchErroredJobsServiceStarter, IJobSchedulerHostedService jobSchedulerHostedServiceStarter) { _cleanUpJobsHostedServiceStarter = cleanUpJobsHostedService; _batchErroredJobsServiceStarter = batchErroredJobsServiceStarter; _jobSchedulerHostedServiceStarter = jobSchedulerHostedServiceStarter; } [ApiKey] [HttpPost("StartCleanUpJobs", Name = "StartCleanUpJobs")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StartCleanUpJobs() { try { await _cleanUpJobsHostedServiceStarter.StartAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [ApiKey] [HttpPost("StopCleanUpJobs", Name = "StopCleanUpJobs")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StopCleanUpJobs() { try { await _cleanUpJobsHostedServiceStarter.StopAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [ApiKey] [HttpPost("StartBatchErroredJobs", Name = "StartBatchErroredJobs")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StartBatchErroredJobs() { try { await _batchErroredJobsServiceStarter.StartAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [ApiKey] [HttpPost("StopBatchErroredJobs", Name = "StopBatchErroredJobs")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StopBatchErroredJobs() { try { await _batchErroredJobsServiceStarter.StopAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [ApiKey] [HttpPost("StartJobScheduler", Name = "StartJobScheduler")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StartJobScheduler() { try { await _jobSchedulerHostedServiceStarter.StartAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [ApiKey] [HttpPost("StopJobScheduler", Name = "StopJobScheduler")] [ProducesResponseType(200)] [ProducesResponseType(500)] public async Task StopJobScheduler() { try { await _jobSchedulerHostedServiceStarter.StopAsync(); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } } }