Skip to content

Commit 8f48052

Browse files
committed
兼容新旧调度表达式
1 parent a377a7a commit 8f48052

File tree

8 files changed

+52
-32
lines changed

8 files changed

+52
-32
lines changed

NetFramework/App.Scheduler/Components/Extensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public static DateTime ToDateTime(this object o)
6767
return o == null ? new DateTime() : DateTime.Parse(o.ToString());
6868
}
6969

70+
/// <summary>转化为列表</summary>
71+
public static List<T> ToList<T>(this T[] source)
72+
{
73+
var result = new List<T>();
74+
foreach (var item in source)
75+
result.Add(item);
76+
return result;
77+
}
78+
79+
7080
/// <summary>转化为逗号分隔的字符串</summary>
7181
public static string ToCommaString(this IEnumerable source)
7282
{

NetFramework/App.Scheduler/Job.cs

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ public Job(string id, string name, string schedule, DateSpan interval, DateSpan
103103
this.Failure = failure ?? new DateSpan(0, 0, 0, 0, 0, 0, 0, 0);
104104
this.Runner = this.GetType();
105105
}
106+
/// <summary>构建任务</summary>
107+
public Job(Type runnerType, string schedule, DateSpan success, DateSpan failure = null)
108+
{
109+
this.Runner = runnerType;
110+
this.Name = runnerType.Name;
111+
this.Schedule = new Schedule(schedule);
112+
this.Success = success;
113+
this.Failure = failure ?? new DateSpan(0, 0, 0, 0, 0, 0, 0, 0);
114+
this.Runner = this.GetType();
115+
}
106116

107117

108118
//-------------------------------------------

NetFramework/App.Scheduler/Schedule.cs

+23-19
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,38 @@ public Schedule(string expression)
3838
{
3939
if (expression.IsEmpty())
4040
return;
41+
this._expression = expression;
4142
var parts = expression.Trim().Split(' ');
42-
if (parts.Length != 7)
43+
if (parts.Length < 6)
4344
return;
4445

45-
this._expression = expression;
46+
// 6参数为年月日时分周;7参数为年月日时分秒周
4647
this.Years = parts[0] == "*" ? new List<int>() : parts[0].Split(',').CastInt();
4748
this.Months = parts[1] == "*" ? new List<int>() : parts[1].Split(',').CastInt();
4849
this.Days = parts[2] == "*" ? new List<int>() : parts[2].Split(',').CastInt();
4950
this.Hours = parts[3] == "*" ? new List<int>() : parts[3].Split(',').CastInt();
5051
this.Minutes = parts[4] == "*" ? new List<int>() : parts[4].Split(',').CastInt();
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>();
52+
if (parts.Length == 6)
53+
{
54+
this.WeekDays = parts[5] == "*" ? new List<DayOfWeek>() : parts[5].Split(',').CastEnum<DayOfWeek>();
55+
}
56+
if (parts.Length == 7)
57+
{
58+
this.Seconds = parts[5] == "*" ? new List<int>() : parts[5].Split(',').CastInt();
59+
this.WeekDays = parts[6] == "*" ? new List<DayOfWeek>() : parts[6].Split(',').CastEnum<DayOfWeek>();
60+
}
5361
}
5462

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;
68-
}
63+
// 链式表达式方法
64+
public Schedule SetYears(params int[] n) { this.Years = n.ToList(); return this;}
65+
public Schedule SetMonths(params int[] n) { this.Months = n.ToList(); return this; }
66+
public Schedule SetDays(params int[] n) { this.Days = n.ToList(); return this; }
67+
public Schedule SetHours(params int[] n) { this.Hours = n.ToList(); return this; }
68+
public Schedule SetMinutes(params int[] n) { this.Minutes = n.ToList(); return this; }
69+
public Schedule SetSeconds(params int[] n) { this.Seconds = n.ToList(); return this; }
70+
public Schedule SetWeekdays(params DayOfWeek[] n) { this.WeekDays = n.ToList(); return this; }
71+
72+
6973

7074
/// <summary>转化为字符串</summary>
7175
public override string ToString()
@@ -83,7 +87,7 @@ public bool InTime(DateTime dt)
8387
if (this.Minutes.IsEmpty() || this.Minutes.Contains(dt.Minute))
8488
if (this.Seconds.IsEmpty() || this.Seconds.Contains(dt.Second))
8589
if (this.WeekDays.IsEmpty() || this.WeekDays.Contains(dt.DayOfWeek))
86-
return true;
90+
return true;
8791
return false;
8892
}
8993
}
Binary file not shown.
Binary file not shown.

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

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

Readme.md

-9
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,6 @@ static ScheduleEngine CreateEngine()
173173
});
174174
return new ScheduleEngine(cfg);
175175
}
176-
177-
/// <summary>从配置中构建引擎对象</summary>
178-
private static ScheduleEngine CreateEngineFromConfig()
179-
{
180-
var folder = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
181-
string configFile = string.Format("{0}\\scheduler.config", folder);
182-
return new ScheduleEngine(configFile);
183-
}
184176
```
185177

186178
## 8. 数据库上下文如何处理
@@ -196,7 +188,6 @@ private static ScheduleEngine CreateEngineFromConfig()
196188

197189
``` csharp
198190

199-
UtilConfig.Instance.MachineId = IO.GetAppSetting<int>("MachineID");
200191
EntityConfig.Instance.OnGetDb += () => JobContext.Current["db"] as AppContext;
201192
var engine = CreateEngine();
202193
engine.JobRunning += (job, info, _) =>

Task.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---------------------------------------------
22
任务
33
---------------------------------------------
4-
调度循环支持到秒
54
用 ShareProject 的方式共享代码
65

76
开发以下客户端
@@ -14,6 +13,9 @@
1413
---------------------------------------------
1514
完成
1615
---------------------------------------------
16+
2020-12-04
17+
/调度启动表达式支持到秒
18+
1719
2020-12-03
1820
/任务上下文 JobContext.Get("db", ()=>....);
1921
/Job 增加 Id 属性,可被多任务共同依赖

0 commit comments

Comments
 (0)