JobBase.cs 1.31 KB
using Hh.Mes.Service.Logs;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Threading.Tasks;

namespace Quartz.Job.Jobs
{
    /// <summary>
    ///  定时器基类  
    ///  命名空间 Quartz.Job.Jobs 保持一致
    /// </summary>
    /// <remarks>所有Job实现类应当继承于当前类</remarks>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public abstract class JobBase : IJob
    {
        protected readonly QueueJobLog _log = QueueJobLog.GetInstance;
        protected readonly ISqlSugarClient _dbContext;

        protected JobBase()
        {
            SysJobRepository sysJobRepository = new SysJobRepository();
            _dbContext = sysJobRepository.Context;
        }

        public virtual Task Execute(IJobExecutionContext context)
        {
            JobContainer jobContainer = null;
            try
            {
                jobContainer = new JobContainer(context);
                ExecuteJob(context);
            }
            catch (Exception ex)
            {
                jobContainer.ExceptionInfo = ex.Message;
                jobContainer.LoggerJob();
            }
            jobContainer.UpdateJob();
            return Task.CompletedTask;
        }

        public abstract void ExecuteJob(IJobExecutionContext context);
    }
}