Skip to content

Commit

Permalink
查询延迟任务和错误任务时,按最大错误数来取
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 26, 2024
1 parent 413916b commit 20a2b9a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion AntJob.Data/Ant.htm
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ <h3>作业(Job)</h3>
<td></td>
<td></td>
<td>N</td>
<td>任务项保留天数,超过天数的任务项将被删除,默认3天</td>
<td>任务项保留天数,超过天数的任务项将被删除,默认30天</td>
</tr>

<tr>
Expand Down
4 changes: 2 additions & 2 deletions AntJob.Data/Entity/作业.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public override void Valid(Boolean isNew)
// 参数默认值
var step = Step;
if (step == 0) step = Step = 5;
if (MaxRetain == 0) MaxRetain = 3;
if (MaxRetain == 0) MaxRetain = 30;
if (MaxIdle == 0) MaxIdle = GetDefaultIdle();

if (isNew)
{
if (!Dirtys[nameof(MaxRetry)]) MaxRetry = 10;
if (!Dirtys[nameof(MaxRetry)]) MaxRetry = 100;
if (!Dirtys[nameof(MaxTime)]) MaxTime = 600;
if (!Dirtys[nameof(ErrorDelay)]) ErrorDelay = 60;
if (!Dirtys[nameof(MaxIdle)]) MaxIdle = GetDefaultIdle();
Expand Down
10 changes: 5 additions & 5 deletions AntJob.Data/Entity/作业.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ public partial class Job
public Int32 MaxTime { get => _MaxTime; set { if (OnPropertyChanging("MaxTime", value)) { _MaxTime = value; OnPropertyChanged("MaxTime"); } } }

private Int32 _MaxRetain;
/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认3天</summary>
/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认30天</summary>
[Category("控制参数")]
[DisplayName("保留")]
[Description("保留。任务项保留天数,超过天数的任务项将被删除,默认3天")]
[Description("保留。任务项保留天数,超过天数的任务项将被删除,默认30天")]
[DataObjectField(false, false, false, 0)]
[BindColumn("MaxRetain", "保留。任务项保留天数,超过天数的任务项将被删除,默认3天", "")]
[BindColumn("MaxRetain", "保留。任务项保留天数,超过天数的任务项将被删除,默认30天", "")]
public Int32 MaxRetain { get => _MaxRetain; set { if (OnPropertyChanging("MaxRetain", value)) { _MaxRetain = value; OnPropertyChanged("MaxRetain"); } } }

private Int32 _MaxIdle;
Expand Down Expand Up @@ -539,7 +539,7 @@ public partial class _
/// <summary>最大执行时间。默认600秒,超过该时间则认为执行器故障,将会把该任务分配给其它执行器</summary>
public static readonly Field MaxTime = FindByName("MaxTime");

/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认3天</summary>
/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认30天</summary>
public static readonly Field MaxRetain = FindByName("MaxRetain");

/// <summary>最大空闲时间。默认3600秒,超过该时间不更新则认为应用程序故障,系统触发告警</summary>
Expand Down Expand Up @@ -665,7 +665,7 @@ public partial class __
/// <summary>最大执行时间。默认600秒,超过该时间则认为执行器故障,将会把该任务分配给其它执行器</summary>
public const String MaxTime = "MaxTime";

/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认3天</summary>
/// <summary>保留。任务项保留天数,超过天数的任务项将被删除,默认30天</summary>
public const String MaxRetain = "MaxRetain";

/// <summary>最大空闲时间。默认3600秒,超过该时间不更新则认为应用程序故障,系统触发告警</summary>
Expand Down
9 changes: 5 additions & 4 deletions AntJob.Data/Entity/作业任务.Biz.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using NewLife;
Expand Down Expand Up @@ -160,13 +160,14 @@ public static IEnumerable<JobTask> Search(Int32 id, Int32 appid, Int32 jobid, Jo

/// <summary>获取该任务下特定状态的任务项</summary>
/// <param name="taskid"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="maxRetry"></param>
/// <param name="maxError"></param>
/// <param name="status"></param>
/// <param name="count">要申请的任务个数</param>
/// <returns></returns>
public static IList<JobTask> Search(Int32 taskid, DateTime end, Int32 maxRetry, Int32 maxError, JobStatus[] status, Int32 count)
public static IList<JobTask> Search(Int32 taskid, DateTime start, DateTime end, Int32 maxRetry, Int32 maxError, JobStatus[] status, Int32 count)
{
var exp = new WhereExpression();
if (taskid > 0) exp &= _.JobID == taskid;
Expand All @@ -176,8 +177,8 @@ public static IList<JobTask> Search(Int32 taskid, DateTime end, Int32 maxRetry,
// 限制任务的错误次数,避免无限执行
if (maxError > 0) exp &= _.Error < maxError;

exp &= _.UpdateTime >= DateTime.Now.AddDays(-7);
if (end > DateTime.MinValue) exp &= _.UpdateTime < end;
if (start.Year > 2000) exp &= _.UpdateTime >= start;
if (end.Year > 2000) exp &= _.UpdateTime < end;

return FindAll(exp, _.ID.Asc(), null, 0, count);
}
Expand Down
2 changes: 1 addition & 1 deletion AntJob.Data/Model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<Column Name="MaxError" DataType="Int32" Description="最大错误。连续错误达到最大错误数时停止" Category="控制参数" />
<Column Name="MaxRetry" DataType="Int32" Description="最大重试。默认10次,超过该次数后将不再重试" Category="控制参数" />
<Column Name="MaxTime" DataType="Int32" Description="最大执行时间。默认600秒,超过该时间则认为执行器故障,将会把该任务分配给其它执行器" Category="控制参数" />
<Column Name="MaxRetain" DataType="Int32" Description="保留。任务项保留天数,超过天数的任务项将被删除,默认3天" Category="控制参数" />
<Column Name="MaxRetain" DataType="Int32" Description="保留。任务项保留天数,超过天数的任务项将被删除,默认30天" Category="控制参数" />
<Column Name="MaxIdle" DataType="Int32" Description="最大空闲时间。默认3600秒,超过该时间不更新则认为应用程序故障,系统触发告警" Category="控制参数" />
<Column Name="ErrorDelay" DataType="Int32" Description="错误延迟。默认60秒,出错延迟后重新发放" Category="控制参数" />
<Column Name="Deadline" DataType="DateTime" Description="最后期限。超过该时间后,任务将不再执行" Category="控制参数" />
Expand Down
18 changes: 11 additions & 7 deletions AntJob.Server/Services/JobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ private JobError SetJobError(Job job, JobTask task)
private void CheckMaxError(App app, Job job)
{
// 出错时判断如果超过最大错误数,则停止作业
var maxError = job.MaxError < 1 ? 100 : job.MaxError;
var maxError = job.MaxError <= 0 ? 100 : job.MaxError;
if (job.Enable && job.Error > maxError)
{
job.MaxError = maxError;
Expand Down Expand Up @@ -695,8 +695,9 @@ public IList<JobTask> AcquireDelay(Job job, String server, String ip, Int32 pid,

using var ts = Job.Meta.CreateTrans();

var dt = DateTime.Now;
var list = JobTask.Search(job.ID, dt, job.MaxRetry, 32, [JobStatus.取消, JobStatus.延迟], count);
var now = DateTime.Now;
var maxError = job.MaxError - job.Error;
var list = JobTask.Search(job.ID, now.AddDays(-7), now, job.MaxRetry, maxError, [JobStatus.取消, JobStatus.延迟], count);
foreach (var task in list)
{
task.Server = server;
Expand Down Expand Up @@ -730,19 +731,22 @@ public IList<JobTask> AcquireOld(Job job, String server, String ip, Int32 pid, I
using var ts = Job.Meta.CreateTrans();
var list = new List<JobTask>();

var now = DateTime.Now;
var maxError = job.MaxError - job.Error;

// 查找历史错误任务
if (job.ErrorDelay > 0)
{
var dt = DateTime.Now.AddSeconds(-job.ErrorDelay);
var list2 = JobTask.Search(job.ID, dt, job.MaxRetry, 32, [JobStatus.错误], count);
var end = now.AddSeconds(-job.ErrorDelay);
var list2 = JobTask.Search(job.ID, now.AddDays(-7), end, job.MaxRetry, maxError, [JobStatus.错误], count);
if (list2.Count > 0) list.AddRange(list2);
}

// 查找历史中断任务,持续10分钟仍然未完成
if (job.MaxTime > 0 && list.Count < count)
{
var dt = DateTime.Now.AddSeconds(-job.MaxTime);
var list2 = JobTask.Search(job.ID, dt, job.MaxRetry, 32, [JobStatus.就绪, JobStatus.抽取中, JobStatus.处理中], count - list.Count);
var end = now.AddSeconds(-job.MaxTime);
var list2 = JobTask.Search(job.ID, now.AddDays(-7), end, job.MaxRetry, maxError, [JobStatus.就绪, JobStatus.抽取中, JobStatus.处理中], count - list.Count);
if (list2.Count > 0) list.AddRange(list2);
}
if (list.Count > 0)
Expand Down

0 comments on commit 20a2b9a

Please sign in to comment.