Skip to content

Commit a377a7a

Browse files
committed
调整调度表达式
1 parent 19edc38 commit a377a7a

30 files changed

+137
-1099
lines changed

.vs/App.Scheduler/v16/.suo

15 KB
Binary file not shown.

NetFramework/App.Consoler/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static ScheduleEngine CreateEngine()
9191
Data = "http://www.baidu.com",
9292
Success = new DateSpan(0, 0, 0, 0, 0, 2),
9393
Failure = new DateSpan(0, 0, 0, 0, 0, 0, 0, 9),
94-
Schedule = new Schedule("* * * * * *")
94+
Schedule = new Schedule("* * * * * * *")
95+
//Schedule = new Schedule(days: new List<int> { 1 })
9596
});
9697
/*
9798
cfg.Jobs.Add(new Job()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

NetFramework/App.Consoler/bin/Debug/App.Scheduler.xml

+31-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NetFramework/App.Scheduler/Components/Extensions.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ namespace App.Components
1414
internal static class Extensions
1515
{
1616
/// <summary>字符串是否为空</summary>
17-
public static bool IsNullOrEmpty(this string txt)
17+
public static bool IsEmpty(this string txt)
1818
{
1919
return String.IsNullOrEmpty(txt);
2020
}
2121

2222
/// <summary>对象是否为空或为空字符串</summary>
23-
public static bool IsNullOrEmpty(this object o)
23+
public static bool IsEmpty(this object o)
2424
{
25-
return (o == null) ? true : o.ToString().IsNullOrEmpty();
25+
return (o == null) ? true : o.ToString().IsEmpty();
26+
}
27+
28+
/// <summary>列表是否为空</summary>
29+
public static bool IsEmpty<T>(this List<T> o)
30+
{
31+
return (o == null) ? true : (o.Count == 0);
2632
}
2733

2834
/// <summary>将可空对象转化为字符串</summary>

NetFramework/App.Scheduler/Job.cs

+2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ public class Job : IJobRunner
8888
//-------------------------------------------
8989
// 构造函数
9090
//-------------------------------------------
91+
/// <summary>构建任务</summary>
9192
public Job()
9293
{
9394
this.Runner = this.GetType();
9495
}
96+
/// <summary>构建任务</summary>
9597
public Job(string id, string name, string schedule, DateSpan interval, DateSpan failure=null)
9698
{
9799
this.ID = id;

NetFramework/App.Scheduler/Jobs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class DummyJob : IJobRunner
2727
{
2828
public bool Run(DateTime dt, string data)
2929
{
30-
int seconds = data.IsNullOrEmpty() ? 1 : int.Parse(data);
30+
int seconds = data.IsEmpty() ? 1 : int.Parse(data);
3131
Thread.Sleep(seconds*1000);
3232
return true;
3333
}

NetFramework/App.Scheduler/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
3434
// 方法是按如下所示使用“*”: :
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("1.2.1.*")]
37-
[assembly: AssemblyFileVersion("1.2.1.0")]
36+
[assembly: AssemblyVersion("1.2.2.*")]
37+
[assembly: AssemblyFileVersion("1.2.2.0")]

NetFramework/App.Scheduler/Schedule.cs

+45-18
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,39 @@
77
namespace App.Scheduler
88
{
99
/// <summary>
10-
/// 调度表达式:年 月 日 时 分 周
10+
/// 调度表达式:年 月 日 时 分
1111
/// </summary>
1212
[JsonConverter(typeof(ScheduleConverter))]
1313
public class Schedule
1414
{
15+
string _expression;
16+
1517
// 年月日时分秒周
16-
[JsonIgnore] public List<int> Years { get; set; } = new List<int>();
17-
[JsonIgnore] public List<int> Months { get; set; } = new List<int>();
18-
[JsonIgnore] public List<int> Days { get; set; } = new List<int>();
19-
[JsonIgnore] public List<int> Hours { get; set; } = new List<int>();
18+
[JsonIgnore] public List<int> Years { get; set; } = new List<int>();
19+
[JsonIgnore] public List<int> Months { get; set; } = new List<int>();
20+
[JsonIgnore] public List<int> Days { get; set; } = new List<int>();
21+
[JsonIgnore] public List<int> Hours { get; set; } = new List<int>();
2022
[JsonIgnore] public List<int> Minutes { get; set; } = new List<int>();
23+
[JsonIgnore] public List<int> Seconds { get; set; } = new List<int>();
2124
[JsonIgnore] public List<DayOfWeek> WeekDays { get; set; } = new List<DayOfWeek>();
2225

23-
string _expression;
2426

25-
//
27+
/// <summary>构建调度表达式</summary>
2628
public Schedule() { }
29+
30+
/// <summary>构建调度表达式</summary>
31+
/// <param name="expression">格式如:年 月 日 时 分 秒 周</param>
32+
/// <example>
33+
/// * * 1 0 0 0 * 表示每月 1 日 0 点 0 分启动任务
34+
/// * * * 0,12 0 0 * 表示每天 0 点和 12 点启动任务
35+
/// * * * 0 0 0 1 表示每周一 0 点启动任务
36+
/// </example>
2737
public Schedule(string expression)
2838
{
29-
if (expression.IsNullOrEmpty())
39+
if (expression.IsEmpty())
3040
return;
3141
var parts = expression.Trim().Split(' ');
32-
if (parts.Length != 6)
42+
if (parts.Length != 7)
3343
return;
3444

3545
this._expression = expression;
@@ -38,10 +48,26 @@ public Schedule(string expression)
3848
this.Days = parts[2] == "*" ? new List<int>() : parts[2].Split(',').CastInt();
3949
this.Hours = parts[3] == "*" ? new List<int>() : parts[3].Split(',').CastInt();
4050
this.Minutes = parts[4] == "*" ? new List<int>() : parts[4].Split(',').CastInt();
41-
this.WeekDays = parts[5] == "*" ? new List<DayOfWeek>() : parts[5].Split(',').CastEnum<DayOfWeek>();
51+
this.Seconds = parts[5] == "*" ? new List<int>() : parts[5].Split(',').CastInt();
52+
this.WeekDays = parts[6] == "*" ? new List<DayOfWeek>() : parts[6].Split(',').CastEnum<DayOfWeek>();
53+
}
54+
55+
/// <summary>构建调度表达式</summary>
56+
Schedule(
57+
List<int> years = null, List<int> months = null, List<int> days = null,
58+
List<int> hours = null, List<int> minutes = null, List<int> seconds = null,
59+
List<DayOfWeek> weekdays=null)
60+
{
61+
this.Years = years;
62+
this.Months = months;
63+
this.Days = days;
64+
this.Hours = hours;
65+
this.Minutes = minutes;
66+
this.Seconds = seconds;
67+
this.WeekDays = weekdays;
4268
}
4369

44-
// 转化为字符串
70+
/// <summary>转化为字符串</summary>
4571
public override string ToString()
4672
{
4773
return this._expression;
@@ -50,12 +76,13 @@ public override string ToString()
5076
/// <summary>是否处于运行时间</summary>
5177
public bool InTime(DateTime dt)
5278
{
53-
if (this.Years.Count == 0 || this.Years.Contains(dt.Year))
54-
if (this.Months.Count == 0 || this.Months.Contains(dt.Month))
55-
if (this.Days.Count == 0 || this.Days.Contains(dt.Day))
56-
if (this.Hours.Count == 0 || this.Hours.Contains(dt.Hour))
57-
if (this.Minutes.Count == 0 || this.Minutes.Contains(dt.Minute))
58-
if (this.WeekDays.Count == 0 || this.WeekDays.Contains(dt.DayOfWeek))
79+
if (this.Years.IsEmpty() || this.Years.Contains(dt.Year))
80+
if (this.Months.IsEmpty() || this.Months.Contains(dt.Month))
81+
if (this.Days.IsEmpty() || this.Days.Contains(dt.Day))
82+
if (this.Hours.IsEmpty() || this.Hours.Contains(dt.Hour))
83+
if (this.Minutes.IsEmpty() || this.Minutes.Contains(dt.Minute))
84+
if (this.Seconds.IsEmpty() || this.Seconds.Contains(dt.Second))
85+
if (this.WeekDays.IsEmpty() || this.WeekDays.Contains(dt.DayOfWeek))
5986
return true;
6087
return false;
6188
}
@@ -65,7 +92,7 @@ public bool InTime(DateTime dt)
6592
/// <summary>
6693
/// 调度表达式格式化转化器
6794
/// </summary>
68-
public class ScheduleConverter : JsonConverter
95+
internal class ScheduleConverter : JsonConverter
6996
{
7097
public override bool CanConvert(Type objectType)
7198
{

NetFramework/App.Scheduler/ScheduleEngine.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace App.Scheduler
1111
/// <summary>任务事件代理</summary>
1212
/// <param name="job">任务对象</param>
1313
/// <param name="info">附加数据</param>
14+
/// <param name="success">是否成功</param>
1415
public delegate void JobDelegate(Job job, string info, bool success);
1516

1617
/// <summary>
@@ -84,7 +85,7 @@ public void Stop()
8485
public void Start()
8586
{
8687
// 读取配置文件
87-
if (!_configFile.IsNullOrEmpty())
88+
if (!_configFile.IsEmpty())
8889
{
8990
try
9091
{
Binary file not shown.
Binary file not shown.

NetFramework/App.Scheduler/bin/Debug/App.Scheduler.xml

+31-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Readme.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
- 调度表达式参照 cron
8-
(1) 顺序调整为:年 月 日 时 分 周
8+
(1) 顺序调整为:年 月 日 时 分
99
(2) 每个部分可用逗号分隔
1010
(3) 只保留 * 通配符
1111
- 含依赖逻辑
@@ -57,6 +57,15 @@ public class MyJob : IJobRunner
5757
用代码创建 ScheduleConfig 对象
5858
或修改 Scheduler.config json 文件(详见后)
5959

60+
61+
其中 Schedule 表达式格式如
62+
63+
```
64+
* * 1 0 0 0 * 表示每月 1 日 0 点 0 分启动任务
65+
* * * 0,12 0 0 * 表示每天 0 点和 12 点启动任务
66+
* * * 0 0 0 1 表示每周一 0 点启动任务
67+
```
68+
6069
### 运行
6170

6271
运行App.Consoler.exe(或实现自己的宿主程序)

Shares/Common.cs

-21
This file was deleted.

0 commit comments

Comments
 (0)