JobContainer.cs
4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
}
}
}