网站需要定时执行不同的任务,比如清理无效的数据、定时发送mail等,Nop的这个定时任务设计比较好,简单的说就是将所有任务相同的属性持久化,具体的执行通过继承接口来实现。
持久化对象:ScheduleTask
ScheduleTask定义了Seconds,Type等属性,分别记录执行周期和任务类型。
public class ScheduleTask:BaseEntity { public string Name { get; set; } /// <summary> /// Gets or sets the run period (in seconds) /// </summary> public int Seconds { get; set; } /// <summary> /// Gets or sets the type of appropriate ITask class /// </summary> public string Type { get; set; } /// <summary> /// Gets or sets the value indicating whether a task is enabled /// </summary> public bool Enabled { get; set; } /// <summary> /// Gets or sets the value indicating whether a task should be stopped on some error /// </summary> public bool StopOnError { get; set; } /// <summary> /// Gets or sets the machine name (instance) that leased this task. It's used when running in web farm (ensure that a task in run only on one machine). It could be null when not running in web farm. /// </summary> public string LeasedByMachineName { get; set; } /// <summary> /// Gets or sets the datetime until the task is leased by some machine (instance). It's used when running in web farm (ensure that a task in run only on one machine). /// </summary> public DateTime? LeasedUntilTime { get; set; } /// <summary> /// Gets or sets the datetime when it was started last time /// </summary> public DateTime? LastStartTime { get; set; } /// <summary> /// Gets or sets the datetime when it was finished last time (no matter failed ir success) /// </summary> public DateTime? LastEndTime { get; set; } /// <summary> /// Gets or sets the datetime when it was sucessfully finished last time /// </summary> public DateTime? LastSuccessTime { get; set; } }