当前位置: 移动技术网 > IT编程>开发语言>.net > 基于Quartz.net的远程任务管理系统 一

基于Quartz.net的远程任务管理系统 一

2018年11月05日  | 移动技术网IT编程  | 我要评论

韩天卓,回到未来2qvod,保定热线网

在上一篇绪中,已经介绍了整个项目的情况下了,接下来就是开始一步步做起来了。

首先:先整个我们的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", "group1")
                .withcronschedule("0 0/5 * * * ?")     //30分种执行一次
                .startat(new datetimeoffset(datetime.now.addseconds(5)))
                .build();

                jobconfig.scheduler.schedulejob(job, trigger);
            }
            else
            {
                jobconfig.scheduler.start();
            }
            return true;
        }

        public bool stop(hostcontrol hostcontrol)
        {
            if (jobconfig.factory != null)
            {
                jobconfig.scheduler.shutdown(false);
                jobconfig.factory = null;
                jobconfig.scheduler = null;
            }
            return true;
        }
view code

好了,这一节就先到这,下面的我喝杯茶再来~

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网