-
Notifications
You must be signed in to change notification settings - Fork 11
/
QJob.cs
203 lines (198 loc) · 6.33 KB
/
QJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quartz
{
/// <summary>
/// quartz.net对象
/// 作者:王延领
/// 时间:2016/5/5
/// </summary>
public class QJob : IQJob
{
/// <summary>
/// 构造函数
/// </summary>
public QJob() { }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="syscode">系统编码</param>
/// <param name="id">任务id【需系统内唯一】</param>
/// <param name="name">任务名称</param>
/// <param name="group">任务群组</param>
public QJob(string syscode, string id, string name = "", string group = "")
{
JobId = id;
SysCode = syscode;
Name = name;
Group = group;
Seconds = 60 * 60;
MaxTimes = 0;
StartTime = DateTime.Now.AddMinutes(1);
Handler = null;
DetailHandler = null;
}
public string SysCode { get; set; }
public string JobId { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int Seconds { get; set; }
public int MaxTimes { get; set; }
public DateTime StartTime { get; set; }
public int Times { get; set; }
public JobState State { get; set; }
public JobAction Action { get; set; }
public DateTime LastTime { get; set; }
[System.Xml.Serialization.XmlIgnore]
public Action Handler { get; set; }
[System.Xml.Serialization.XmlIgnore]
public Action<IQJob> DetailHandler { get; set; }
/// 持久化保存
/// </summary>
public bool Save()
{
try
{
string filepath = JobFactory.Instance.GetPath();
if (!File.Exists(Path())) return false;
IQJob myjob = new QJob()
{
SysCode = this.SysCode,
JobId = this.JobId,
Group = this.Group,
Name = this.Name,
LastTime = this.LastTime,
Handler = this.Handler,
MaxTimes = this.MaxTimes,
Seconds = this.Seconds,
State = this.State,
Times = this.Times,
StartTime = this.StartTime,
DetailHandler = this.DetailHandler,
Action = this.Action
};
string xml = XMLProcess.Serializer(typeof(QJob), myjob);
XMLProcess.Write(xml, Path());
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool Load()
{
try
{
if (!File.Exists(Path())) return false;
var job = XMLProcess.Deserialize<QJob>(XMLProcess.ReadXml(Path()));
JobId = job.JobId;
SysCode = job.SysCode;
Name = job.Name;
Group = job.Group;
Seconds = job.Seconds;
MaxTimes = job.MaxTimes;
StartTime = job.StartTime;
Times = job.Times;
State = job.State;
Action = job.Action;
LastTime = job.LastTime;
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 任务的jobkey规则
/// </summary>
/// <returns></returns>
public string Key()
{
return SysCode + "_" + JobId;
}
/// <summary>
/// 开始执行任务
/// </summary>
public void Start()
{
JobFactory.Instance.Build(this);
}
/// <summary>
/// 开始执行任务
/// </summary>
/// <param name="starttime">开始执行时间</param>
/// <param name="internaltimes">时间间隔(s)</param>
/// <param name="maxtimes">执行次数</param>
public void Start(DateTime starttime, int internaltimes = 60*60, int maxtimes = 0)
{
StartTime = starttime;
Seconds = internaltimes;
MaxTimes = maxtimes;
JobFactory.Instance.Build(this);
}
/// <summary>
/// 下次执行时间
/// </summary>
/// <returns></returns>
public DateTime NextTime()
{
return LastTime.AddSeconds(Seconds);
}
/// <summary>
///任务触发动作
/// </summary>
/// <param name="handler">需要任务信息的动作</param>
/// <returns>IMyJob</returns>
public IQJob Handle(Action handler)
{
Handler = handler;
return this;
}
/// <summary>
/// 任务触发动作
/// </summary>
/// <param name="handler">需要任务信息的动作</param>
/// <returns>IMyJob</returns>
public IQJob Handle(Action<IQJob> handler)
{
DetailHandler = handler;
return this;
}
/// <summary>
/// 持久化地址
/// </summary>
/// <returns>【例:../job/syscode_name_group_jobid.xml】</returns>
public string Path()
{
return System.IO.Path.Combine(JobFactory.Instance.GetPath(), string.Format("{0}_{1}_{2}_{3}.xml", SysCode, Group, Name, JobId));
}
/// <summary>
/// 移除任务
/// </summary>
/// <returns></returns>
public bool Remove()
{
return JobFactory.Instance.Remove(Key());
}
/// <summary>
/// 暂停任务
/// </summary>
public void Pause()
{
JobFactory.Instance.Pause(Key());
}
/// <summary>
/// 继续执行任务
/// </summary>
public void Resume()
{
JobFactory.Instance.Resume(Key());
}
}
}