JobContainer.cs 4.16 KB
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using MySqlConnector;
using Quartz;
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using WebRepository;

namespace WebMvc
{
    public class JobContainer
    {

        private IJobExecutionContext Context { set; get; }
        public string ConnString { set; get; }
        public string JobName { set; get; }
        public string JobGroup { set; get; }
        public string MethodName { set; get; }
        public string MethodParams { set; get; }
        public string JobMessage { set; get; }
        public string ExceptionInfo { set; get; }
        public DateTime? LastFireTime { set; get; }
        public DateTime? NextFireTime { set; get; }

        private Stopwatch stopwatch = new Stopwatch();

        public BaseDbContext _dBContext;
        public UnitWork _unitWork;

        public JobContainer(IJobExecutionContext _Context)
        {
            var config = AppSettingsJson.GetAppSettings();

            stopwatch.Start();
            Context = _Context;

            JobName = JobGroup = MethodName = MethodParams = JobMessage = ExceptionInfo = "";
            JobName = Context.JobDetail.Key.Name;

            try
            {
                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(config.GetSection("ConnectionStrings:BaseDbContext").Value);
                connectionStringBuilder.Password = Encryption.Decrypt(connectionStringBuilder.Password);
                string ConnString = connectionStringBuilder.ConnectionString;
                var optionsBuilder = new DbContextOptionsBuilder<BaseDbContext>().UseSqlServer(ConnString);
                _dBContext = new BaseDbContext(optionsBuilder.Options);
                _unitWork = new UnitWork(_dBContext);
            }
            catch
            {
                MySqlConnectionStringBuilder connectionStringBuilder = new MySqlConnectionStringBuilder(config.GetSection("ConnectionStrings:BaseDbContext").Value);
                connectionStringBuilder.Password = Encryption.Decrypt(connectionStringBuilder.Password);
                string ConnString = connectionStringBuilder.ConnectionString;
                var optionsBuilder = new DbContextOptionsBuilder<BaseDbContext>().UseMySql(ConnString, new MySqlServerVersion(new Version(8, 0, 28)));
                _dBContext = new BaseDbContext(optionsBuilder.Options);
                _unitWork = new UnitWork(_dBContext);
            }

            InitContainer();
        }

        public void InitContainer()
        {
            try
            {
                SysJob sysJob = _unitWork.FindSingle<SysJob>(u => u.JobName.Equals(JobName));
                if (sysJob != null)
                {
                    JobGroup = sysJob.JobGroup;
                    MethodName = sysJob.MethodName;
                    MethodParams = sysJob.MethodParams;
                }

                LastFireTime = TimeZoneInfo.ConvertTimeFromUtc(Context.FireTimeUtc.DateTime, TimeZoneInfo.Local);
                NextFireTime = TimeZoneInfo.ConvertTimeFromUtc(Context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local);
            }
            catch (Exception ex)
            {
                ExceptionInfo = ex.Message;
            }
        }

        public void LoggerJob()
        {
            #region 更新任务时间
            SysJob sysJob = _unitWork.FindSingle<SysJob>(u => u.JobName.Equals(JobName));
            sysJob.LastFireTime = LastFireTime;
            sysJob.NextFireTime = NextFireTime;
            _unitWork.Update(sysJob);
            #endregion

            #region 记录任务日志
            stopwatch.Stop();
            JobMessage = "总共耗时:" + stopwatch.Elapsed.TotalMilliseconds.ToString() + " 毫秒";

            _unitWork.Add<SysJobLog>(new SysJobLog
            {
                JobName = JobName,
                JobGroup = JobGroup,
                MethodName = MethodName,
                MethodParams = MethodParams,
                JobMessage = JobMessage,
                ExceptionInfo = ExceptionInfo,
                CreateTime = DateTime.Now,
                CreateBy = "System"
            });
            #endregion
        }

    }
}