67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
using System.Threading;
|
|
using LCU.Api.Internal.MaidBotLib.Models;
|
|
using LCU.Api.Internal.Dispatcher.Services.QueueServices;
|
|
using LCU.Library.Internal.DispatcherModels.Enums;
|
|
using System;
|
|
namespace LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
|
|
|
|
public class JobScheduleService : IJobScheduleService
|
|
{
|
|
private readonly IQueueExecutionService _queueExecutionService;
|
|
private readonly IJobService _jobService;
|
|
private readonly IWorkItemService _workItemService;
|
|
private readonly IWorkItemExecutionService _workItemExecutionService;
|
|
|
|
public JobScheduleService(IQueueExecutionService queueExecutionService,
|
|
IJobService jobService,
|
|
IWorkItemService workItemService,
|
|
IWorkItemExecutionService workItemExecutionService)
|
|
{
|
|
_queueExecutionService = queueExecutionService;
|
|
_jobService = jobService;
|
|
_workItemService = workItemService;
|
|
_workItemExecutionService = workItemExecutionService;
|
|
}
|
|
|
|
public async Task PerformSchedule(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var readyToScheduleJobs = await _jobService.GetJobsReadyToBeScheduled();
|
|
|
|
foreach (var job in readyToScheduleJobs)
|
|
{
|
|
var workItem = await _workItemService.GetFirstOrDefaultWorkItemByJobId(job.JobId);
|
|
|
|
if (workItem == null)
|
|
{
|
|
// THIS SHOULD NEVER HAPPEN
|
|
Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group),
|
|
Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment))
|
|
.Error($"Failure in Dispatcher Job Scheduler. Attempting to schedule Job: {job.JobId} - {job.Name} and NO workItems were found to process.");
|
|
continue;
|
|
}
|
|
|
|
var workItemExecution = await _workItemExecutionService.CreateWorkItemExecution(workItem.WorkItemId);
|
|
|
|
job.JobStatus = JobStatus.PendingToRun;
|
|
|
|
if (job.SoftDeleteDate != null)
|
|
{
|
|
job.SoftDeleteDate = null;
|
|
}
|
|
|
|
await _jobService.UpdateAsync(job);
|
|
Log.Information("Job Id {JobId} was saved with status {Status}", job.JobId, job.JobStatus);
|
|
|
|
await _queueExecutionService.QueueExecution(workItemExecution);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group),
|
|
Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment))
|
|
.Error(exception, $"Failed to perform schedule.");
|
|
}
|
|
}
|
|
} |