优化调整

parent 074ef3c1
...@@ -328,7 +328,7 @@ public ApiResponse GenerateReport([CustomizeValidator(RuleSet = "Delete"), FromB ...@@ -328,7 +328,7 @@ public ApiResponse GenerateReport([CustomizeValidator(RuleSet = "Delete"), FromB
// await Task.Delay(TimeSpan.FromSeconds(5), token); // await Task.Delay(TimeSpan.FromSeconds(5), token);
// } // }
//}); //});
_taskService.Add(Background.JobType.生成报表, allot.ID.ToString()); _taskService.Add(Background.JobType.报表, allot.ID.ToString());
return new ApiResponse(ResponseType.OK, "统计报表数据任务开始"); return new ApiResponse(ResponseType.OK, "统计报表数据任务开始");
} }
......
...@@ -33,7 +33,7 @@ public class BackgroundJob : IJob ...@@ -33,7 +33,7 @@ public class BackgroundJob : IJob
{ {
new BackgroundSetting { JobType = Background.JobType.提取数据, MaxThread = 1, Timeout = 90 }, new BackgroundSetting { JobType = Background.JobType.提取数据, MaxThread = 1, Timeout = 90 },
new BackgroundSetting { JobType = Background.JobType.生成测算表, MaxThread = 2, Timeout = 20 }, new BackgroundSetting { JobType = Background.JobType.生成测算表, MaxThread = 2, Timeout = 20 },
new BackgroundSetting { JobType = Background.JobType.生成报表, MaxThread = 1, Timeout = 20 }, new BackgroundSetting { JobType = Background.JobType.报表, MaxThread = 5, Timeout = 20 },
new BackgroundSetting { JobType = Background.JobType.自定义抽取, MaxThread = 1, Timeout = 90 }, new BackgroundSetting { JobType = Background.JobType.自定义抽取, MaxThread = 1, Timeout = 90 },
}; };
} }
...@@ -49,20 +49,35 @@ public void Execute() ...@@ -49,20 +49,35 @@ public void Execute()
if (tasks == null || tasks.Count == 0) if (tasks == null || tasks.Count == 0)
return; return;
tasks = tasks.OrderBy(w => w.ID).ToList();
Timeout(service, tasks); Timeout(service, tasks);
Repeat(service, tasks); Repeat(service, tasks);
foreach (var item in EnumHelper.GetItems<Background.JobType>().Select(w => w.Value)) foreach (var jobType in EnumHelper.GetItems<Background.JobType>().Select(w => w.Value))
{ {
var sett = _settings.FirstOrDefault(w => (int)w.JobType == item); var sett = _settings.FirstOrDefault(w => (int)w.JobType == jobType);
if (sett != null) if (sett != null && tasks.Count(w => w.JobType == jobType && (int)Background.Status.执行中 == w.Status) >= sett.MaxThread)
{ {
if (tasks.Count(w => w.JobType == item && (int)Background.Status.执行中 == w.Status) >= sett.MaxThread) continue;
continue;
} }
var task = tasks.OrderBy(w => w.ID).FirstOrDefault(w => w.JobType == item && w.Status == (int)Background.Status.等待); #region 相同参数同时只允许一个执行
if (task == null) continue; bg_task task = null;
foreach (var item in tasks.Where(w => w.JobType == jobType && w.Status == (int)Background.Status.等待))
{
if (!tasks.Any(w => w.JobType == jobType && (int)Background.Status.执行中 == w.Status && w.Argument == item.Argument))
{
task = item;
break;
}
}
if (task == null)
{
continue;
}
#endregion
switch (task.JobType) switch (task.JobType)
{ {
...@@ -72,7 +87,7 @@ public void Execute() ...@@ -72,7 +87,7 @@ public void Execute()
case (int)Background.JobType.提取数据: case (int)Background.JobType.提取数据:
Execute_Allot_ExtractData(service, task); Execute_Allot_ExtractData(service, task);
break; break;
case (int)Background.JobType.生成报表: case (int)Background.JobType.报表:
Execute_Allot_Generate_Report(service, task); Execute_Allot_Generate_Report(service, task);
break; break;
case (int)Background.JobType.自定义抽取: case (int)Background.JobType.自定义抽取:
...@@ -96,31 +111,32 @@ private void Execute_Allot_Generate(TaskService service, bg_task task) ...@@ -96,31 +111,32 @@ private void Execute_Allot_Generate(TaskService service, bg_task task)
using (var scope = _serviceScopeFactory.CreateScope()) using (var scope = _serviceScopeFactory.CreateScope())
{ {
var allotService = scope.ServiceProvider.GetService<AllotService>(); var allotService = scope.ServiceProvider.GetService<AllotService>();
try
if (int.TryParse(task.Argument, out int allotId))
{ {
if (int.TryParse(task.Argument, out int allotId)) var allot = allotService.GetAllot(allotId);
if (allot == null)
{
service.Update(task.ID, Background.Status.无效);
return;
}
try
{ {
var allot = allotService.GetAllot(allotId);
if (allot == null)
{
service.Update(task.ID, Background.Status.无效);
return;
}
service.Update(task.ID, Background.Status.执行中); service.Update(task.ID, Background.Status.执行中);
Stopwatch stopwatch = Stopwatch.StartNew(); Stopwatch stopwatch = Stopwatch.StartNew();
allotService.Generate(allot); allotService.Generate(allot);
allotService.GenerateReport(allot);
stopwatch.Stop(); stopwatch.Stop();
service.Update(task.ID, Background.Status.完成, milliseconds: stopwatch.ElapsedMilliseconds); service.Update(task.ID, Background.Status.完成, seconds: stopwatch.Elapsed.TotalSeconds);
} }
catch (Exception ex)
{
service.Update(task.ID, Background.Status.失败, ex.ToString());
}
allotService.GenerateReport(allot);
} }
catch (Exception ex)
{
service.Update(task.ID, Background.Status.失败, ex.ToString());
}
} }
} }
/// <summary> /// <summary>
...@@ -151,7 +167,7 @@ private void Execute_Allot_Generate_Report(TaskService service, bg_task task) ...@@ -151,7 +167,7 @@ private void Execute_Allot_Generate_Report(TaskService service, bg_task task)
allotService.GenerateReport(allot); allotService.GenerateReport(allot);
stopwatch.Stop(); stopwatch.Stop();
service.Update(task.ID, Background.Status.完成, milliseconds: stopwatch.ElapsedMilliseconds); service.Update(task.ID, Background.Status.完成, seconds: stopwatch.Elapsed.TotalSeconds);
} }
} }
catch (Exception ex) catch (Exception ex)
...@@ -205,7 +221,7 @@ private void Execute_Allot_CustomExtract(TaskService service, bg_task task) ...@@ -205,7 +221,7 @@ private void Execute_Allot_CustomExtract(TaskService service, bg_task task)
stopwatch.Stop(); stopwatch.Stop();
service.Update(task.ID, Background.Status.完成, milliseconds: stopwatch.ElapsedMilliseconds); service.Update(task.ID, Background.Status.完成, seconds: stopwatch.Elapsed.TotalSeconds);
} }
} }
...@@ -289,7 +305,7 @@ private void Execute_Allot_ExtractData(TaskService service, bg_task task) ...@@ -289,7 +305,7 @@ private void Execute_Allot_ExtractData(TaskService service, bg_task task)
stopwatch.Stop(); stopwatch.Stop();
service.Update(task.ID, Background.Status.完成, milliseconds: stopwatch.ElapsedMilliseconds); service.Update(task.ID, Background.Status.完成, seconds: stopwatch.Elapsed.TotalSeconds);
} }
} }
catch (Exception ex) catch (Exception ex)
......
...@@ -2172,6 +2172,20 @@ ...@@ -2172,6 +2172,20 @@
<param name="service"></param> <param name="service"></param>
<param name="task"></param> <param name="task"></param>
</member> </member>
<member name="M:Performance.Api.BackgroundJob.Execute_Allot_Generate_Report(Performance.Services.TaskService,Performance.EntityModels.bg_task)">
<summary>
生成报表
</summary>
<param name="service"></param>
<param name="task"></param>
</member>
<member name="M:Performance.Api.BackgroundJob.Execute_Allot_CustomExtract(Performance.Services.TaskService,Performance.EntityModels.bg_task)">
<summary>
提取绩效数据
</summary>
<param name="service"></param>
<param name="task"></param>
</member>
<member name="M:Performance.Api.BackgroundJob.Timeout(Performance.Services.TaskService,System.Collections.Generic.List{Performance.EntityModels.bg_task})"> <member name="M:Performance.Api.BackgroundJob.Timeout(Performance.Services.TaskService,System.Collections.Generic.List{Performance.EntityModels.bg_task})">
<summary> <summary>
超时关闭 超时关闭
......
...@@ -142,7 +142,7 @@ public enum JobType ...@@ -142,7 +142,7 @@ public enum JobType
{ {
生成测算表 = 1, 生成测算表 = 1,
提取数据 = 2, 提取数据 = 2,
生成报表 = 3, 报表 = 3,
自定义抽取 = 4, 自定义抽取 = 4,
} }
public enum Status public enum Status
......
...@@ -28,6 +28,6 @@ public class bg_task ...@@ -28,6 +28,6 @@ public class bg_task
public int Status { get; set; } public int Status { get; set; }
public string Argument { get; set; } public string Argument { get; set; }
public string Remark { get; set; } public string Remark { get; set; }
public long ElapsedTime { get; set; } public double ElapsedTime { get; set; }
} }
} }
...@@ -40,7 +40,7 @@ public bool Add(Background.JobType type, string argument = "") ...@@ -40,7 +40,7 @@ public bool Add(Background.JobType type, string argument = "")
}); });
} }
public bool Update(int taskId, Background.Status status, string remark = "", long milliseconds = 0) public bool Update(int taskId, Background.Status status, string remark = "", double seconds = 0)
{ {
var task = _taskRepository.GetEntity(w => w.ID == taskId); var task = _taskRepository.GetEntity(w => w.ID == taskId);
if (task == null) return false; if (task == null) return false;
...@@ -48,7 +48,7 @@ public bool Update(int taskId, Background.Status status, string remark = "", lon ...@@ -48,7 +48,7 @@ public bool Update(int taskId, Background.Status status, string remark = "", lon
task.Status = (int)status; task.Status = (int)status;
if (status == Background.Status.执行中) if (status == Background.Status.执行中)
task.BeginTime = DateTime.Now; task.BeginTime = DateTime.Now;
task.ElapsedTime = milliseconds / 1000; task.ElapsedTime = seconds;
task.Remark = $"{(Background.JobType)task.JobType} - {status} {remark}"; task.Remark = $"{(Background.JobType)task.JobType} - {status} {remark}";
if (status == Background.Status.完成 || status == Background.Status.失败 || status == Background.Status.超时) if (status == Background.Status.完成 || status == Background.Status.失败 || status == Background.Status.超时)
task.EndTime = DateTime.Now; task.EndTime = DateTime.Now;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment