Skip to content

Commit

Permalink
优化数据调度从JobContext.Data获取数据对象列表的用法
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 18, 2024
1 parent 77b56a2 commit 64ff0cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions AntJob.Extensions/DataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,32 @@ namespace AntJob.Extensions;
public DataHandler() => Factory = Entity<TEntity>.Meta.Factory;

#region 数据处理
/// <summary>分批抽取数据,一个任务内多次调用</summary>
/// <param name="ctx">上下文</param>
/// <param name="row">开始行数</param>
/// <returns></returns>
protected override Object Fetch(JobContext ctx, ref Int32 row)
{
var list = base.Fetch(ctx, ref row);
if (list is IEnumerable enumerable)
{
// 修改列表类型,由 IList<IEntity> 改为 IList<TEntity> ,方便用户使用
list = enumerable.Cast<TEntity>().ToList();
}

return list;
}

/// <summary>处理一批数据</summary>
/// <param name="ctx">上下文</param>
/// <returns></returns>
public override Int32 Execute(JobContext ctx)
{
var count = 0;
foreach (var item in ctx.Data as IEnumerable)
{
if (ProcessItem(ctx, item as TEntity)) count++;
}

return count;
}
Expand Down
19 changes: 18 additions & 1 deletion AntJob/JobContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AntJob.Data;
using System.Collections;
using AntJob.Data;
using NewLife.Collections;
using NewLife.Data;

Expand Down Expand Up @@ -59,4 +60,20 @@ public class JobContext : IExtend
/// <summary>处理速度</summary>
public Int32 Speed => (Cost <= 0 || Total == 0) ? 0 : (Int32)Math.Min(Total * 1000L / Cost, Int32.MaxValue);
#endregion

#region 方法
/// <summary>根据指定实体类型返回数据列表</summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetDatas<T>()
{
if (Data == null) return null;
if (Data is IList<T> data) return data;

// 修改列表类型,由 IList<IEntity> 改为 IList<TEntity> ,方便用户使用
if (Data is IEnumerable enumerable) return enumerable.Cast<T>().ToList();

return null;
}
#endregion
}

0 comments on commit 64ff0cc

Please sign in to comment.