Skip to content

Commit

Permalink
[feat]定时作业支持多个Cron表达式,逗号分隔。close #87
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 3, 2024
1 parent a70eea2 commit 81c9201
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
13 changes: 10 additions & 3 deletions NewLife.Cube/Areas/Cube/Controllers/CronJobController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ protected override Boolean Valid(CronJob entity, DataObjectMethodType type, Bool
{
if (post)
{
var cron = new Cron();
if (!cron.Parse(entity.Cron)) throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron));
var next = DateTime.MinValue;
foreach (var item in entity.Cron.Split(";"))
{
var cron = new Cron();
if (!cron.Parse(item)) throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron));

var dt = cron.GetNext(DateTime.Now);
if (next == DateTime.MinValue || dt < next) next = dt;
}

// 重算下一次的时间
if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)]) entity.NextTime = cron.GetNext(DateTime.Now);
if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)]) entity.NextTime = next;

JobService.Wake();
}
Expand Down
14 changes: 11 additions & 3 deletions NewLife.CubeNC/Areas/Cube/Controllers/CronJobController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using NewLife.Cube.Entity;
Expand Down Expand Up @@ -64,11 +65,18 @@ protected override Boolean Valid(CronJob entity, DataObjectMethodType type, Bool
{
if (post)
{
var cron = new Cron();
if (!cron.Parse(entity.Cron)) throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron));
var next = DateTime.MinValue;
foreach (var item in entity.Cron.Split(";"))
{
var cron = new Cron();
if (!cron.Parse(item)) throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron));

var dt = cron.GetNext(DateTime.Now);
if (next == DateTime.MinValue || dt < next) next = dt;
}

// 重算下一次的时间
if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)]) entity.NextTime = cron.GetNext(DateTime.Now);
if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)]) entity.NextTime = next;

JobService.Wake();
}
Expand Down
21 changes: 12 additions & 9 deletions NewLife.CubeNC/Common/EntityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,18 +720,21 @@ public virtual ActionResult DeleteAll()
/// <returns></returns>
protected virtual Int32 OnUpdate(TEntity entity)
{
// 遍历表单字段,部分字段可能有扩展
foreach (var item in EditFormFields)
if (Request.HasFormContentType)
{
if (item is FormField ef && ef.GetExpand != null)
// 遍历表单字段,部分字段可能有扩展
foreach (var item in EditFormFields)
{
// 获取参数对象,展开参数,从表单字段接收参数
var p = ef.GetExpand(entity);
if (p != null && p is not String && !(entity as IEntity).Dirtys[ef.Name])
if (item is FormField ef && ef.GetExpand != null)
{
// 保存参数对象
if (FieldCollection.ReadForm(p, Request.Form, ef.Name + "_"))
entity.SetItem(ef.Name, p.ToJson(true));
// 获取参数对象,展开参数,从表单字段接收参数
var p = ef.GetExpand(entity);
if (p != null && p is not String && !(entity as IEntity).Dirtys[ef.Name])
{
// 保存参数对象
if (FieldCollection.ReadForm(p, Request.Form, ef.Name + "_"))
entity.SetItem(ef.Name, p.ToJson(true));
}
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions NewLife.CubeNC/Services/JobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ public void Start()
var id = $"{expession}@{cmd}@{job.Argument}";
if (id == _id && _timer != null) return;

var cron = new Cron();
if (!cron.Parse(expession)) throw new InvalidOperationException($"无效表达式 {expession}");
foreach (var item in expession.Split(";"))
{
var cron = new Cron();
if (!cron.Parse(item)) throw new InvalidOperationException($"无效表达式 {item}");
}

// 找到类和方法
_type = cmd.GetTypeEx();
Expand Down Expand Up @@ -339,7 +342,17 @@ private async void DoJobWork(Object state)

job.WriteLog(job.Name, success, message);

job.NextTime = _timer.Cron.GetNext(_timer.NextTime);
//job.NextTime = _timer.Cron.GetNext(_timer.NextTime);
// 本次任务未完成,timer.NextTime不变
//job.NextTime = _timer.NextTime;
var next = DateTime.MinValue;
foreach (var cron in _timer.Crons)
{
var dt = cron.GetNext(_timer.NextTime);
if (next == DateTime.MinValue || dt < next) next = dt;
}
job.NextTime = next;

job.Update();
}
}
2 changes: 1 addition & 1 deletion NewLife.CubeNC/ViewModels/FormField.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NewLife.Cube.ViewModels;

/// <summary获取扩展字段委托</summary>
/// <summary>获取扩展字段委托</summary>
/// <param name="entity"></param>
/// <returns></returns>
public delegate Object GetExpandDelegate(Object entity);
Expand Down

0 comments on commit 81c9201

Please sign in to comment.