JobApp.cs 1.96 KB
using System;
using Quartz;
using WebRepository;

namespace WebMvc
{
    public static class JobApp
    {
        #region 增加计划
        /// <summary>
        /// 新增定时器任务
        /// </summary>
        /// <param name="Table_entity">任务实体</param>
        public static void AddJob(IScheduler _sched, SysJob Table_entity)
        {
            try
            {
                string methodName = Table_entity.MethodName ?? "";
                IJobDetail job = null;
                //if (methodName.ToLower().Equals("ClearLogJob".ToLower()))
                //{
                //    job = JobBuilder.Create<ClearLogJob>().WithIdentity(Table_entity.JobName).Build();
                //}
                //else if (methodName.ToLower().Equals("AGVJob".ToLower()))
                //{
                //    job = JobBuilder.Create<AGVJob>().WithIdentity(Table_entity.JobName).Build();
                //}

                if (job != null)
                {
                    var trigger = TriggerBuilder.Create().WithIdentity(Table_entity.JobName).WithCronSchedule(Table_entity.CronExpression).Build();
                    _sched.ScheduleJob(job, trigger).Wait();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region 删除计划
        /// <summary>
        /// 删除定时器任务
        /// </summary>
        /// <param name="Table_entity">任务实体</param>
        public static void DeleteJob(IScheduler _sched, SysJob Table_entity)
        {
            try
            {
                _sched.PauseTrigger(new TriggerKey(Table_entity.JobName)).Wait();
                _sched.UnscheduleJob(new TriggerKey(Table_entity.JobName)).Wait();
                _sched.DeleteJob(new JobKey(Table_entity.JobName)).Wait();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}