在上一篇緒中,已經介紹了整個項目的情況下了,接下來就是開始一步步做起來了。 首先:先整個我們的Job任務表,以及Job執行日誌表。SQL如下: 1 drop table if exists job_info; 2 create table job_info 3 ( 4 id int not null ...
在上一篇緒中,已經介紹了整個項目的情況下了,接下來就是開始一步步做起來了。
首先:先整個我們的Job任務表,以及Job執行日誌表。SQL如下:
1 drop table if exists job_info; 2 create table job_info 3 ( 4 id int not null AUTO_INCREMENT comment '主鍵', 5 job_name varchar(150) null default null comment '任務名稱', 6 job_assembly varchar(200) null default null comment '執行的方式的dll名稱', 7 job_class varchar(300) null default null comment '執行的方法類', 8 job_corn varchar(100) null default null comment '執行任務的corn表達式', 9 job_type int not null default 1 comment '任務類型,預設為1 簡單,2 複雜', 10 job_execount int null default 0 comment '任務的執行總次數,0表示無限次', 11 job_starttime datetime null default null comment '任務開始時間', 12 job_state int null default 0 comment '任務的狀態 0 準備中,1 執行中,2 暫定,3 停止,4 結束', 13 addtime TIMESTAMP null default CURRENT_TIMESTAMP comment '任務的創建時間', 14 PRIMARY KEY (`id`) 15 ); 16 drop table if exists job_log; 17 create table job_log 18 ( 19 id int not null AUTO_INCREMENT comment '主鍵', 20 job_name varchar(150) null default null comment '任務名稱', 21 job_result varchar(200) null default null comment '執行的結果', 22 job_exception text null default null comment '執行任務的異常信息', 23 job_exetime int not null default 0 comment '執行耗時,單位ms', 24 job_exedate datetime null default 0 comment '任務的執行的日期時間', 25 job_exestate int null default 0 comment '執行結果 0 正常,1 異常', 26 addtime TIMESTAMP null default CURRENT_TIMESTAMP comment '任務的執行時間', 27 PRIMARY KEY (`id`) 28 );View Code
相關實體類與操作方法,如下:
1 /// <summary> 2 /// Job信息表 3 /// </summary> 4 public class Job_Info : BaseEntity 5 { 6 private int _id = 0; 7 private string _job_name; 8 private string _job_assembly; 9 private string _job_class; 10 private string _job_corn; 11 private int _job_type = 1; 12 private int _job_execount = 0; 13 private DateTime _job_starttime; 14 private int _job_state = 0; 15 private DateTime _addtime; 16 17 /// <summary> 18 /// 主鍵 19 /// </summary> 20 public int Id { get => _id; set => _id = value; } 21 /// <summary> 22 /// 任務名稱 23 /// </summary> 24 public string Job_name { get => _job_name; set => _job_name = value; } 25 /// <summary> 26 /// 執行的方式的dll名稱 27 /// </summary> 28 public string Job_assembly { get => _job_assembly; set => _job_assembly = value; } 29 /// <summary> 30 /// 執行的方法類 31 /// </summary> 32 public string Job_class { get => _job_class; set => _job_class = value; } 33 /// <summary> 34 /// 執行任務的corn表達式 35 /// </summary> 36 public string Job_corn { get => _job_corn; set => _job_corn = value; } 37 /// <summary> 38 /// 任務類型,預設為1 簡單,2 複雜 39 /// </summary> 40 public int Job_type { get => _job_type; set => _job_type = value; } 41 /// <summary> 42 /// 任務的執行總次數,0表示無限次 43 /// </summary> 44 public int Job_execount { get => _job_execount; set => _job_execount = value; } 45 /// <summary> 46 /// 任務開始時間 47 /// </summary> 48 public DateTime Job_starttime { get => _job_starttime; set => _job_starttime = value; } 49 /// <summary> 50 /// 任務的狀態 0 準備中,1 執行中,2 暫定,3 停止,4 結束 51 /// </summary> 52 public int Job_state { get => _job_state; set => _job_state = value; } 53 /// <summary> 54 /// 任務的創建時間 55 /// </summary> 56 public DateTime Addtime { get => _addtime; set => _addtime = value; } 57 58 }View Code
/// <summary> /// Job運行日誌表 /// </summary> public class Job_Log : BaseEntity { private int _id = 0; private string _job_name; private string _job_result; private string _job_exception; private int _job_exetime; private DateTime _job_exedate; private int _job_exestate; private DateTime _addtime; /// <summary> /// 主鍵 /// </summary> public int Id { get => _id; set => _id = value; } /// <summary> /// 任務名稱 /// </summary> public string Job_name { get => _job_name; set => _job_name = value; } /// <summary> /// 執行的結果 /// </summary> public string Job_result { get => _job_result; set => _job_result = value; } /// <summary> /// 執行任務的異常信息 /// </summary> public string Job_exception { get => _job_exception; set => _job_exception = value; } /// <summary> /// 執行耗時,單位ms /// </summary> public int Job_exetime { get => _job_exetime; set => _job_exetime = value; } /// <summary> /// 任務的執行的日期時間 /// </summary> public DateTime Job_exedate { get => _job_exedate; set => _job_exedate = value; } /// <summary> /// 執行結果 0 正常,1 異常 /// </summary> public int Job_exestate { get => _job_exestate; set => _job_exestate = value; } /// <summary> /// 任務的執行時間 /// </summary> public DateTime Addtime { get => _addtime; set => _addtime = value; } }View Code
1 /// <summary> 2 /// Job信息邏輯類 3 /// </summary> 4 public class JobInfoBLL 5 { 6 /// <summary> 7 /// 添加Job信息 8 /// </summary> 9 /// <param name="jobInfo"></param> 10 /// <returns></returns> 11 public int AddInfo(Job_Info jobInfo) 12 { 13 int result = 0; 14 15 if (jobInfo == null) 16 return result; 17 try 18 { 19 string SqlStr = string.Format("insert into job_info(job_name,job_assembly,job_class,job_corn,job_type,job_execount,job_starttime,job_state) values('{0}','{1}','{7}','{2}',{3},{4},'{5}',{6})", 20 jobInfo.Job_name, jobInfo.Job_assembly, jobInfo.Job_corn, jobInfo.Job_type, jobInfo.Job_execount, jobInfo.Job_starttime.ToString("yyyy-MM-dd HH:mm:ss"), 0, jobInfo.Job_class); 21 result = MySqlHelper.ExceuteSql(SqlStr); 22 } 23 finally 24 { 25 26 } 27 28 return result; 29 } 30 31 /// <summary> 32 /// 更新Job的狀態 33 /// </summary> 34 /// <param name="jobInfo"></param> 35 /// <returns></returns> 36 public int UpdateJobState(Job_Info jobInfo) 37 { 38 int result = 0; 39 40 if (jobInfo == null) 41 return result; 42 try 43 { 44 string SqlStr = string.Format("update job_info set job_state={0} where ", jobInfo.Job_state); 45 if(jobInfo.Id > 0) 46 { 47 SqlStr += string.Format(" id={0};", jobInfo.Id); 48 }else if (!string.IsNullOrEmpty(jobInfo.Job_name)) 49 { 50 SqlStr += string.Format(" job_name='{0}'", jobInfo.Job_name); 51 } 52 result = MySqlHelper.ExceuteSql(SqlStr); 53 } 54 finally 55 { 56 57 } 58 59 return result; 60 } 61 62 /// <summary> 63 /// job列表 64 /// </summary> 65 /// <returns></returns> 66 public List<Job_Info> GetJobList() 67 { 68 List<Job_Info> list = null; 69 70 try 71 { 72 string SqlStr = "select * from job_info"; 73 var ds = MySqlHelper.GetDataSet(SqlStr); 74 if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 75 { 76 list = new List<Job_Info>(); 77 Job_Info info = new Job_Info(); 78 foreach (System.Data.DataRow row in ds.Tables[0].Rows) 79 { 80 info = Job_Info.ToEntity<Job_Info>(row, info); 81 if (info != null) 82 list.Add(info); 83 } 84 } 85 } 86 finally 87 { 88 89 } 90 91 return list; 92 } 93 94 /// <summary> 95 /// job列表 96 /// </summary> 97 /// <returns></returns> 98 public List<Job_Info> GetJobList(int pageIndex,int pageSize) 99 { 100 List<Job_Info> list = null; 101 102 try 103 { 104 string SqlStr = "select * from job_info"; 105 SqlStr += string.Format(" limit {0},{1};", (pageIndex - 1) * pageSize, pageSize); 106 var ds = MySqlHelper.GetDataSet(SqlStr); 107 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 108 { 109 list = new List<Job_Info>(); 110 Job_Info info = new Job_Info(); 111 foreach (System.Data.DataRow row in ds.Tables[0].Rows) 112 { 113 info = Job_Info.ToEntity<Job_Info>(row, info); 114 if (info != null) 115 list.Add(info); 116 } 117 } 118 } 119 finally 120 { 121 122 } 123 124 return list; 125 } 126 127 public Job_Info GetOne(int id) 128 { 129 Job_Info job = null; 130 131 try 132 { 133 string SqlStr = "select * from job_info where id="+id; 134 var ds = MySqlHelper.GetDataSet(SqlStr); 135 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 136 { 137 job = new Job_Info(); 138 foreach (System.Data.DataRow row in ds.Tables[0].Rows) 139 { 140 job = Job_Info.ToEntity<Job_Info>(row, job); 141 } 142 } 143 } 144 catch (Exception ex) 145 { 146 147 } 148 149 return job; 150 } 151 152 /// <summary> 153 /// job數量 154 /// </summary> 155 /// <returns></returns> 156 public int GetJobCount() 157 { 158 int list = 0; 159 160 try 161 { 162 string SqlStr = "select count(0) from job_log"; 163 var ds = MySqlHelper.GetReader(SqlStr); 164 if (ds != null) 165 { 166 int.TryParse(ds.ToString(), out list); 167 } 168 } 169 finally 170 { 171 172 } 173 174 return list; 175 } 176 177 }View Code
/// <summary> /// Job 日誌邏輯類 /// </summary> public class JobLogBLL { /// <summary> /// 添加Job日誌信息 /// </summary> /// <param name="jobLog"></param> /// <returns></returns> public int AddLog(Job_Log jobLog) { int result = 0; if (jobLog == null) return result; try { string SqlStr = string.Format("insert into job_log(job_name,job_result,job_exception,job_exetime,job_exedate,job_exestate) values('{0}','{1}','{2}',{3},'{4}',{5})", jobLog.Job_name, jobLog.Job_result, jobLog.Job_exception, jobLog.Job_exetime, jobLog.Job_exedate.ToString("yyyy-MM-dd HH:mm:ss"), jobLog.Job_exestate); result = MySqlHelper.ExceuteSql(SqlStr); } finally { } return result; } /// <summary> /// job日誌列表 /// </summary> /// <returns></returns> public List<Job_Log> GetJobLogList() { List<Job_Log> list = null; try { string SqlStr = "select * from job_log"; var ds = MySqlHelper.GetDataSet(SqlStr); if (ds != null && ds.Tables[0].Rows.Count > 0) { list = new List<Job_Log>(); Job_Log info = new Job_Log(); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { info = Job_Log.ToEntity<Job_Log>(row, info); if (info != null) list.Add(info); } } } finally { } return list; } /// <summary> /// job日誌列表 /// </summary> /// <returns></returns> public List<Job_Log> GetJobLogList(int pageIndex, int pageSize) { List<Job_Log> list = null; try { string SqlStr = "select * from job_log"; SqlStr += string.Format(" limit {0},{1};", (pageIndex - 1) * pageSize, pageSize); var ds = MySqlHelper.GetDataSet(SqlStr); if (ds != null && ds.Tables[0].Rows.Count > 0) { list = new List<Job_Log>(); Job_Log info = new Job_Log(); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { info = Job_Log.ToEntity<Job_Log>(row, info); if (info != null) list.Add(info); } } } finally { } return list; } public int GetJobLogCount(int state = -1) { int count = 0; try { string SqlStr = "select count(0) from job_log"; if (state > -1) { SqlStr += " where job_exestate=" + state; } var ds = MySqlHelper.GetReader(SqlStr); if (ds != null) { int.TryParse(ds.ToString(), out count); } } finally { } return count; } }View Code
這些都是資料庫的基本操作,我就直接貼出來了。為了規範,我們就定了一個介面,供所有Job任務來實際業務,介面很簡單,具體如下:
/// <summary> /// 基Job /// </summary> public interface IJob { /// <summary> /// 執行任務 /// </summary> bool Exceute(); }View Code
好了,下麵我們就開始我們的Job托管器-Window Server。為了方便,我就使用Topshelf,因為創建JobManage.Service這個項目時,一定要選擇Console項目(我一開始就直接使用了Service項目,然後一直運行不起來)。
那現在來創建一個JobServer類,實現ServiceControl, ServiceSuspend 兩大介面的方法,在服務啟動的時候,實例化Schedule,然後加入檢查Job的Job任務,定期增加或刪除Schedule里的Job。
實現如下:
public bool Start(HostControl hostControl) { if (JobConfig.factory == null) { JobConfig.factory = new StdSchedulerFactory(); JobConfig.scheduler = JobConfig.factory.GetScheduler(); JobConfig.scheduler.Start(); //創建迴圈Job IJobDetail job = JobBuilder.Create<LoopJob>().WithIdentity("LoopJob", "group1").Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("LoopJobtrigger