|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.WMS.Core.TaskInteraction.Dtos;
|
|
|
|
|
using DS.WMS.Core.TaskInteraction.Entity;
|
|
|
|
|
using DS.WMS.Core.TaskInteraction.Interface;
|
|
|
|
@ -16,7 +17,7 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
public sealed class BackgroundTaskJob : IJob
|
|
|
|
|
{
|
|
|
|
|
IServiceProvider serviceProvider;
|
|
|
|
|
ISqlSugarClient? db;
|
|
|
|
|
ISqlSugarClient db;
|
|
|
|
|
ILogger<BackgroundTaskJob> logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -37,11 +38,13 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
var dbLinks = await db.Queryable<Module.SqlSugar.SysTenantLink>().ToListAsync();
|
|
|
|
|
var exceptions = new ConcurrentBag<Exception>();
|
|
|
|
|
var options = new ParallelOptions { MaxDegreeOfParallelism = 10 };
|
|
|
|
|
var list = await db.Queryable<Module.SqlSugar.SysTenantLink>().ToListAsync();
|
|
|
|
|
await Parallel.ForEachAsync(list, options, async (dbLink, ct) =>
|
|
|
|
|
{
|
|
|
|
|
SqlSugarClient? tenantDb = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var dbLink in dbLinks)
|
|
|
|
|
{
|
|
|
|
|
tenantDb = new SqlSugarClient(new ConnectionConfig
|
|
|
|
|
{
|
|
|
|
@ -53,7 +56,7 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
tenantDb.QueryFilter.Clear();
|
|
|
|
|
var jobs = await tenantDb.Queryable<TaskJob>()
|
|
|
|
|
.InnerJoin<BusinessTask>((x, y) => x.TaskId == y.Id)
|
|
|
|
|
.Where((x, y) => y.TaskStatus == TaskStatusEnum.Create && !x.IsExecuted && SqlFunc.GetDate() >= x.NextTriggerTime)
|
|
|
|
|
.Where((x, y) => y.TaskStatus == TaskStatusEnum.Create && x.Status == TaskJobStatus.Ready)
|
|
|
|
|
.Select((x, y) => new TaskJob
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
@ -62,50 +65,53 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
TypeName = x.TypeName,
|
|
|
|
|
BusinessId = y.BusinessId,
|
|
|
|
|
BusinessType = y.BusinessType,
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
}).ToListAsync(ct);
|
|
|
|
|
if (jobs.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var jobContext = new TaskJobContext
|
|
|
|
|
{
|
|
|
|
|
AdditionalData = context.MergedJobDataMap!
|
|
|
|
|
AdditionalData = context.MergedJobDataMap
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var item in jobs)
|
|
|
|
|
{
|
|
|
|
|
var type = Type.GetType(item.TypeName, false);
|
|
|
|
|
if (type == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (ConstructorExtensions.CreateInstance(type, serviceProvider) is ITaskJob job)
|
|
|
|
|
{
|
|
|
|
|
jobContext.JobInfo = item;
|
|
|
|
|
jobContext.Result = DataResult.Success;
|
|
|
|
|
await job.Execute(jobContext);
|
|
|
|
|
|
|
|
|
|
if (jobContext.Result.Succeeded)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
item.IsExecuted = true;
|
|
|
|
|
item.NextTriggerTime = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await job.ExecuteAsync(jobContext);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
item.LastException = $"执行作业({item.Id})时发生错误:" + ex.Message + Environment.NewLine + ex.StackTrace;
|
|
|
|
|
jobContext.Result = DataResult.Failed(item.LastException);
|
|
|
|
|
exceptions.Add(new ApplicationException(item.LastException, ex));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (jobContext.Result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
item.Status = TaskJobStatus.Completed;
|
|
|
|
|
item.NextTriggerTime = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await tenantDb.Updateable(jobs).UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.IsExecuted,
|
|
|
|
|
x.Status,
|
|
|
|
|
x.NextTriggerTime,
|
|
|
|
|
x.LastException
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
}).ExecuteCommandAsync(ct);
|
|
|
|
|
|
|
|
|
|
var finishedJobs = jobs.FindAll(x => x.IsExecuted);
|
|
|
|
|
var finishedJobs = jobs.FindAll(x => x.Status == TaskJobStatus.Completed);
|
|
|
|
|
if (finishedJobs.Count > 0) //尝试更新关联任务
|
|
|
|
|
{
|
|
|
|
|
List<BusinessTask> tasks = [];
|
|
|
|
@ -115,13 +121,13 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
x.TaskId,
|
|
|
|
|
x.IsExecuted
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
x.Status
|
|
|
|
|
}).ToListAsync(ct);
|
|
|
|
|
var groups = list.GroupBy(x => x.TaskId);
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
|
{
|
|
|
|
|
if (group.Count() == group.Count(x => x.IsExecuted))
|
|
|
|
|
if (group.Count() == group.Count(x => x.Status == TaskJobStatus.Completed))
|
|
|
|
|
{
|
|
|
|
|
tasks.Add(new BusinessTask
|
|
|
|
|
{
|
|
|
|
@ -137,14 +143,21 @@ namespace DS.WMS.Core.QuarztJobs
|
|
|
|
|
x.TaskStatus,
|
|
|
|
|
x.UpdateBy,
|
|
|
|
|
x.UpdateTime
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}).ExecuteCommandAsync(ct);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.LogError(ex, "执行定时作业时发生异常");
|
|
|
|
|
exceptions.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
tenantDb?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!exceptions.IsEmpty)
|
|
|
|
|
throw new AggregateException(exceptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|