Skip to content

Commit

Permalink
[feat]获取最小数据时间。初始化作业时自动设置首个数据时间
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 30, 2024
1 parent 4b32b4c commit ec40ec3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
40 changes: 32 additions & 8 deletions AntJob.Extensions/DataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ public abstract class DataHandler : Handler
#endregion

#region 方法
/// <summary>开始</summary>
public override Boolean Start()
/// <summary>初始化。作业处理器启动之前</summary>
public override void Init()
{
if (Active) return false;

if (Factory == null) throw new ArgumentNullException(nameof(Factory));

// 自动识别雪花Id字段
Expand All @@ -123,12 +121,38 @@ public override Boolean Start()
var job = Job;
if (job.Step == 0) job.Step = 30;

//// 获取最小时间
//if (job.DataTime.Year < 2000) throw new InvalidOperationException("数据任务必须设置开始时间");

//todo 如果DataTime为空,则自动获取最小时间,并设置到DataTime,以减轻平台设置负担

return base.Start();
// 获取最小数据时间
if (job.DataTime.Year < 2000)
{
//throw new InvalidOperationException("数据任务必须设置开始时间");
job.DataTime = GetMinDataTime();
}
}

/// <summary>获取最小数据时间。初始化作业时自动设置首个数据时间</summary>
/// <returns></returns>
public virtual DateTime GetMinDataTime()
{
var field = Field;
if (field == null) return DateTime.MinValue;

// 按时间字段升序,取第一个
var list = Factory.FindAll(null, field.Asc(), field, 0, 1);
if (list.Count > 0)
{
var value = list[0][field.Name];
if (field.Type == typeof(Int64))
{
// 雪花Id
return Factory.Snow.TryParse(value.ToLong(), out var dt, out _, out _) ? dt.ToLocalTime() : DateTime.MinValue;
}

return value.ToDateTime();
}

return DateTime.MinValue;
}
#endregion

Expand Down
3 changes: 3 additions & 0 deletions AntJob/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public Handler()
#endregion

#region 基本方法
/// <summary>初始化。作业处理器启动之前</summary>
public virtual void Init() { }

/// <summary>开始</summary>
public virtual Boolean Start()
{
Expand Down
13 changes: 12 additions & 1 deletion AntJob/Providers/NetworkJobProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public override void Start()
var list = new List<IJob>();
foreach (var handler in bs)
{
// 初始化处理器
try
{
handler.Init();
}
catch (Exception ex)
{
Log?.Error(ex.Message);
}

var job = handler.Job ?? new JobModel();

job.Name = handler.Name;
Expand All @@ -74,7 +84,8 @@ public override void Start()
}

// 改为UTC通信
job.DataTime = job.DataTime.ToUniversalTime();
if (job.DataTime.Year > 1000)
job.DataTime = job.DataTime.ToUniversalTime();
if (job.End.Year > 1000)
job.End = job.End.ToUniversalTime();

Expand Down

0 comments on commit ec40ec3

Please sign in to comment.