121 lines
3.3 KiB
C#
121 lines
3.3 KiB
C#
using LCU.Library.Internal.DispatcherModels.Entities;
|
|
using LCU.Library.Internal.DispatcherModels.Models;
|
|
using LCU.Library.Internal.LcuHttpClient.HttpRequest;
|
|
using LCU.WebApps.Internal.DispatcherManager.Configurations;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
namespace LCU.WebApps.Internal.DispatcherManager.Services;
|
|
|
|
public class JobService
|
|
{
|
|
private readonly DispatcherApiConfiguration _dispatcherApiConfiguration;
|
|
private readonly IPostRequest _postRequest;
|
|
private readonly IGetRequest _getRequest;
|
|
private readonly IPutRequest _putRequest;
|
|
private readonly IDeleteRequest _deleteRequest;
|
|
|
|
public JobService(DispatcherApiConfiguration dispatcherApiConfiguration,
|
|
IPostRequest postRequest,
|
|
IGetRequest getRequest,
|
|
IPutRequest putRequest,
|
|
IDeleteRequest deleteRequest)
|
|
{
|
|
_dispatcherApiConfiguration = dispatcherApiConfiguration;
|
|
_postRequest = postRequest;
|
|
_getRequest = getRequest;
|
|
_putRequest = putRequest;
|
|
_deleteRequest = deleteRequest;
|
|
}
|
|
|
|
public async Task<PagedResponse<Job>> GetFilteredJobs(Filter filter)
|
|
{
|
|
var jobs = new PagedResponse<Job>();
|
|
try
|
|
{
|
|
jobs = await _postRequest.AsyncWithNewton<Filter, PagedResponse<Job>>(new Uri(_dispatcherApiConfiguration.GetFilteredJobsEndpointUrl), filter);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting jobs.");
|
|
}
|
|
|
|
return jobs;
|
|
}
|
|
|
|
public async Task<List<Job>> GetJobs()
|
|
{
|
|
var jobs = new List<Job>();
|
|
try
|
|
{
|
|
jobs = await _getRequest.AsyncWithNewton<List<Job>>(_dispatcherApiConfiguration.JobControllerEndpointUrl);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting jobs.");
|
|
}
|
|
|
|
return jobs;
|
|
}
|
|
|
|
public async Task<Job> RunJobNow(Job job)
|
|
{
|
|
try
|
|
{
|
|
return await _putRequest.AsyncWithNewton<Job, Job>(_dispatcherApiConfiguration.RunJobNowEndpointUrl, job);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error Setting Job to Now.");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<Job> DeactivateJob(Job job)
|
|
{
|
|
try
|
|
{
|
|
return await _putRequest.AsyncWithNewton<Job, Job>(_dispatcherApiConfiguration.DeactivateJobEndpointUrl, job);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error Deactivating Job.");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<Job> RescheduleJob(Job job)
|
|
{
|
|
try
|
|
{
|
|
return await _putRequest.AsyncWithNewton<Job, Job>(_dispatcherApiConfiguration.RescheduleJobEndpointUrl, job);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error Rescheduling Job.");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<bool> DeleteJob(int jobId)
|
|
{
|
|
try
|
|
{
|
|
var url = _dispatcherApiConfiguration.JobControllerEndpointUrl + "?jobId=" + jobId;
|
|
|
|
await _deleteRequest.Async(url);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error deleting job.");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |