JobContainer.cs
4.51 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
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using HslCommunication.Enthernet;
using Infrastructure;
using Quartz;
using WebRepository;
namespace WebMvc
{
public class JobContainer
{
public static NetPushServer pushServer = null;
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 JobContainer(IJobExecutionContext _Context)
{
var config = AppSettingsJson.GetAppSettings();
SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(config.GetSection("ConnectionStrings:BaseDBContext").Value);
sqlConnectionStringBuilder.Password = Encryption.Decrypt(sqlConnectionStringBuilder.Password);
ConnString = sqlConnectionStringBuilder.ConnectionString;
stopwatch.Start();
Context = _Context;
JobName = JobGroup = MethodName = MethodParams = JobMessage = ExceptionInfo = "";
JobName = Context.JobDetail.Key.Name;
InitContainer();
}
public void InitContainer()
{
string sql = "";
DbHelp dbHelp = new DbHelp(ConnString);
try
{
sql = string.Format("SELECT [jobName] ,[jobGroup] ,[methodName] ,[methodParams] ,[cronExpression] FROM [dbo].[sys_job] WHERE jobName = '{0}';", JobName);
DataSet dataSet = dbHelp.SelectGet(sql);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
JobGroup = dataSet.Tables[0].Rows[0]["jobGroup"].ToString();
MethodName = dataSet.Tables[0].Rows[0]["methodName"].ToString();
MethodParams = dataSet.Tables[0].Rows[0]["methodParams"].ToString();
}
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()
{
string sql = "";
DbHelp dbHelp = new DbHelp(ConnString);
#region 更新任务时间
sql = string.Format("UPDATE [dbo].[sys_job] SET lastFireTime = '{0}', nextFireTime = '{1}' WHERE jobName = '{2}';", LastFireTime, NextFireTime, JobName);
dbHelp.DataOperator(sql);
#endregion
#region 记录任务日志
stopwatch.Stop();
JobMessage = "总共耗时:" + stopwatch.Elapsed.TotalMilliseconds.ToString() + " 毫秒";
JobName = JobName.Replace("'", "''");
JobGroup = JobGroup.Replace("'", "''");
MethodName = MethodName.Replace("'", "''");
MethodParams = MethodParams.Replace("'", "''");
JobMessage = JobMessage.Replace("'", "''");
ExceptionInfo = ExceptionInfo.Replace("'", "''");
sql = string.Format(@"INSERT INTO [dbo].[sys_job_log]
([jobName]
,[jobGroup]
,[methodName]
,[methodParams]
,[jobMessage]
,[exceptionInfo]
,[createTime]
,[createBy])
VALUES
('{0}'
,'{1}'
,'{2}'
,'{3}'
,'{4}'
,'{5}'
, GETDATE()
, 'System')", JobName, JobGroup, MethodName, MethodParams, JobMessage, ExceptionInfo);
dbHelp.DataOperator(sql);
#endregion
}
public static void InitBroadCaster()
{
if (pushServer == null)
{
pushServer = new NetPushServer();
pushServer.ServerStart(23467);
}
}
}
}