This commit is contained in:
2022-09-27 23:06:28 +00:00
parent 1d706b4bb7
commit 153978785c
9 changed files with 142 additions and 7 deletions

View File

@@ -596,10 +596,29 @@ namespace AyaNova.Biz
else
idList = await ct.Customer.AsNoTracking().Select(z => z.Id).ToListAsync();
bool SaveIt = false;
//case 4192
TimeSpan ProgressAndCancelCheckSpan = new TimeSpan(0, 0, ServerBootConfig.JOB_PROGRESS_UPDATE_AND_CANCEL_CHECK_SECONDS);
DateTime LastProgressCheck = DateTime.UtcNow.Subtract(new TimeSpan(1, 1, 1, 1, 1));
var TotalRecords = idList.LongCount();
long CurrentRecord = -1;
foreach (long id in idList)
{
try
{
//Update progress / cancel requested?
CurrentRecord++;
if (DateUtil.IsAfterDuration(LastProgressCheck, ProgressAndCancelCheckSpan))
{
await JobsBiz.UpdateJobProgressAsync(job.GId, $"{CurrentRecord}/{TotalRecords}");
if (await JobsBiz.GetJobStatusAsync(job.GId) == JobStatus.CancelRequested)
{
break;
}
}
SaveIt = false;
ClearErrors();
Customer o = null;
@@ -635,7 +654,7 @@ namespace AyaNova.Biz
FailedObjectCount++;
}
}
//TEMPORARY MEASURE AWAITING BETTER SOLUTION AND CANCELLABLE EXTENSION JOBS
//TEMPORARY MEASURE AWAITING BETTER SOLUTION AND CANCELLABLE EXTENSION JOBS
//delay so we're not tying up all the resources in a tight loop
await Task.Delay(AyaNova.Util.ServerBootConfig.JOB_OBJECT_HANDLE_BATCH_JOB_LOOP_DELAY);
}

View File

@@ -11,7 +11,8 @@ namespace AyaNova.Biz
Sleeping = 1,
Running = 2,
Completed = 3,
Failed = 4
Failed = 4,
CancelRequested = 5
}

View File

@@ -73,6 +73,16 @@ namespace AyaNova.Biz
}
}
/// <summary>
/// Request the cancellation of a job, not all jobs honour this
/// </summary>
/// <param name="jobId"></param>
internal static async Task RequestCancelAsync(Guid jobId)
{
await UpdateJobStatusAsync(jobId, JobStatus.CancelRequested);
await LogJobAsync(jobId, "LT:Cancel");
}
/// <summary>
/// Remove the job and it's logs
@@ -152,6 +162,37 @@ namespace AyaNova.Biz
return o.JobStatus;
}
}
/// <summary>
/// Update the progress of a job
/// </summary>
/// <param name="jobId"></param>
/// <param name="progress"></param>
internal static async Task UpdateJobProgressAsync(Guid jobId, string progress)
{
using (AyContext ct = ServiceProviderProvider.DBContext)
{
var oFromDb = await ct.OpsJob.SingleOrDefaultAsync(z => z.GId == jobId);
if (oFromDb == null) return;
oFromDb.Progress = progress;
await ct.SaveChangesAsync();
}
}
/// <summary>
/// Get the progress and status of a job
/// </summary>
/// <param name="jobId"></param>
internal static async Task<JobProgress> GetJobProgressAsync(Guid jobId)
{
using (AyContext ct = ServiceProviderProvider.DBContext)
{
var o = await ct.OpsJob.AsNoTracking().SingleOrDefaultAsync(z => z.GId == jobId);
if (o == null) return new JobProgress() { JobStatus = JobStatus.Absent, Progress = string.Empty };
return new JobProgress() { JobStatus = o.JobStatus, Progress = o.Progress };
}
}
#endregion Job ops
#region PROCESSOR