-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobControl.cs
More file actions
390 lines (339 loc) · 17.8 KB
/
Copy pathJobControl.cs
File metadata and controls
390 lines (339 loc) · 17.8 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
namespace KXTX.IT.BICenter {
class JobControl {
public static string strSqlConn = ConfigurationManager.ConnectionStrings["JobControl"].ToString();
public static string DtexecPath = ConfigurationManager.AppSettings["DtexecPath"].ToString();
public static string KitchenPath = ConfigurationManager.AppSettings["KitchenPath"].ToString();
public static string strTolist = ConfigurationManager.AppSettings["ToList"].ToString();
public static string strCCList = ConfigurationManager.AppSettings["CCList"].ToString();
public static string strMailProfile = ConfigurationManager.AppSettings["MailProfile"].ToString();
public static string RetryTimes = ConfigurationManager.AppSettings["RetryTimes"].ToString();
public static int ParallelDegree = Convert.ToInt16(ConfigurationManager.AppSettings["ParallelDegree"]);
public static string logFileFolder = ConfigurationManager.AppSettings["JobControlLogPath"].ToString();
public static int ThreadTimeout = Convert.ToInt16(ConfigurationManager.AppSettings["ThreadTimeout"].ToString());
//P1: int -> PackageID
//P2: JobMeta
public static Dictionary<int,JobMeta> childrenProcs = new Dictionary<int, JobMeta>();
public int JobID;
public JobControl(int JobID)
{
this.JobID = JobID;
}
public List<DataRow> getPackages(int JobID, string strExecutionGuid) {
try {
List<DataRow> packageList = SQLManager.ExecuteProc(strSqlConn, "usp_Job_GetPackagesToRun"
, new SqlParameter("@JobID", JobID)
, new SqlParameter("@ParallelDegree", ParallelDegree)
, new SqlParameter("@ExecutionGuid", strExecutionGuid)
).Select().ToList();
return packageList;
}
catch (Exception ex) {
LogManager.AppendLog(DateTime.Now.ToString() + " getPackages Failed");
LogManager.AppendLog("Error Message: getPackages Failed, " + ex.Message);
Console.WriteLine(ex.Message);
throw new Exception("getPackages failed", ex);
}
}
public void execPackage(int JobID, int packageID, string executionGuid) {
int retriedTimes = 0;
int retryTargetTimes = Int32.Parse(RetryTimes);
if (retryTargetTimes > 3)
{
retryTargetTimes = 3;
}
bool canRetry = false;
while (retriedTimes <= retryTargetTimes)
{
try
{
string str = "";
string str2 = "";
List<DataRow> source = SQLManager.ExecuteQuery(strSqlConn, "SELECT ExecutionPath FROM DBO.Job_PackageMeta WHERE PackageID=" + packageID, new SqlParameter[0]).Select().ToList<DataRow>();
string objectname = SQLManager.ExecuteQuery(strSqlConn, "SELECT TOP 1 PackageName FROM DBO.Job_PackageMeta WHERE PackageID=" + packageID, new SqlParameter[0]).Rows[0][0].ToString();
string strPackageLogPath = logFileFolder + "Package\\" + DateTime.Now.ToString("yyyyMMdd") + "\\Log_" + objectname + ".log";
if (source.Count<DataRow>() > 0)
{
str = source[0][0].ToString();
}
if (str.Substring(str.IndexOf('.', 0) + 1) != "kjb")
{
str2 = string.Format(" /F {0} /SET \"\\package.Variables[User::_executionGuid].Properties[Value]\";\"{1}\"", str, executionGuid);
//str2 = string.Format(" /F {0} /SET \"\\package.Variables[User::_executionGuid].Properties[Value]\";\"{1}\" /Rep v > {2}.txt", str, executionGuid, strPackageLogPath);
}
else
{
str2 = string.Format(@" /norep /file {0} /logfile=F:\KXTX-ETL\BUS2ODS_KETTLE\Log\CollectBulkFile_%date.log", str);
}
//Console.WriteLine("[Debug]: -->" + str.Substring(str.IndexOf('.', 0) + 1));
//Console.WriteLine("[Debug]: KitchenPath-->"+ Path.Combine(KitchenPath, "Kitchen.bat"));
Console.WriteLine(str2);
UpdateStatusToRunning(packageID, executionGuid);
Process item = new Process {
StartInfo = {
FileName = (str.Substring(str.IndexOf('.', 0) + 1) == "kjb") ? Path.Combine(KitchenPath, "Kitchen.bat") : Path.Combine(DtexecPath, "DTExec.exe"),
Arguments = str2,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
item.Start();
childrenProcs.Add(packageID,new JobMeta(JobID, executionGuid, packageID, item));
//output to logfile for each Package
FileStream fs = new FileStream(strPackageLogPath, FileMode.OpenOrCreate);
try
{
while (!item.StandardOutput.EndOfStream)
{
string line = item.StandardOutput.ReadLine();
byte[] data = System.Text.Encoding.Default.GetBytes("\r\n" + line);
fs.Write(data, 0, data.Length);
}
fs.Flush();
fs.Close();
}
catch (Exception ex)
{
//once happen any exception , still need to close the system object to avoid resouce occupancy
fs.Flush();
fs.Close();
}
item.WaitForExit();
int exitCode = item.ExitCode;
Console.WriteLine("execute result:" + exitCode);
if (exitCode == 0)
{
SetStatusToSuccess(packageID, executionGuid);
childrenProcs.Remove(packageID);
break;
}
else
{
//SetStatusToFailure(packageID, executionGuid);
//check conneciton issue
var retryFlag = SQLManager.ExecuteProc(strSqlConn, "usp_Job_CanRetry"
, new SqlParameter("@PackageID", packageID)
, new SqlParameter("@ExecutionGuid", executionGuid)).Select().FirstOrDefault();
if (retryFlag != null && retryFlag[0].ToString() == "1")
{
canRetry = true;
retriedTimes++;
if (canRetry && retriedTimes <= retryTargetTimes)
{
SetStatusToRetry(packageID, executionGuid);
}
else
{
SetStatusToFailure(packageID, executionGuid);
childrenProcs.Remove(packageID);
break;
}
}
else
{
SetStatusToFailure(packageID, executionGuid);
childrenProcs.Remove(packageID);
break;
}
}
}
catch (Exception exception)
{
SetStatusToFailure(packageID, executionGuid);
Console.WriteLine("Executed package failed: " + exception.Message);
LogManager.AppendLog(DateTime.Now.ToString() + " Executed package failed.");
LogManager.AppendLog("Error Message: Execute package Failed. " + exception.Message);
}
}
}
/*
* Feature : Timeout handler logic
* Date : 2017/07/11
* Author : David
*/
public void threadStatusChecker(object stateinfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateinfo;
if (childrenProcs.Count == 0 && IsJobRunning(this.JobID))
{
Console.WriteLine("No available Process is Running,but the Job is still runing. Waiting ...");
}
if (childrenProcs.Count == 0 && !IsJobRunning(this.JobID))
{
Console.WriteLine("No available Process is Running");
autoEvent.Set();
}
else
{
for (int i = 0; i < childrenProcs.Count(); i++)
{
if (DateTime.Now > childrenProcs.Values.ToArray<JobMeta>()[i].startTime.AddSeconds(ThreadTimeout)
//Except ODS JOB which total duration exceed the 7200s
//&& this.JobID != 1
)
{
try
{
if (childrenProcs.Values.ToArray<JobMeta>()[i].proc.HasExited == false)
{
Console.WriteLine("Kill Process which ID is : " + childrenProcs.Values.ToList<JobMeta>()[i].proc.Id);
childrenProcs.Values.ToArray<JobMeta>()[i].proc.Kill();
childrenProcs.Values.ToArray<JobMeta>()[i].proc.WaitForExit();
}
}
catch (InvalidOperationException invalidException)
{
// process has already exited - might be able to let this one go
Console.WriteLine("Process Kill Failed , the exception message is : " + invalidException.Message.ToString());
LogManager.AppendLog(DateTime.Now.ToString() + "############Process Kill Failed , the exception message is : " + invalidException.Message.ToString());
}
finally {
if (childrenProcs.Values.ToArray<JobMeta>()[i].proc.HasExited)
{
childrenProcs.Values.ToArray<JobMeta>()[i].proc.Dispose();
childrenProcs.Values.ToArray<JobMeta>()[i].proc.Close();
}
}
Console.WriteLine("Due to exceed the Timeout period . Killed the Process which PackageID: {0}", childrenProcs.Values.ToArray<JobMeta>()[i].PackageID.ToString());
LogManager.AppendLog(DateTime.Now.ToString() + string.Format("Due to exceed the Timeout period . Killed the Process which PackageID:{0}"
, childrenProcs.Values.ToArray<JobMeta>()[i].PackageID.ToString()));
SetStatusToFailure(childrenProcs.Values.ToArray<JobMeta>()[i].PackageID, childrenProcs.Values.ToArray<JobMeta>()[i].ExecutionGUID);
childrenProcs.Remove(childrenProcs.Values.ToArray<JobMeta>()[i].PackageID);
}
}
}
}
private static void UpdateStatusToRunning(int packageID, string executionGuid) {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_UpdateStatus_PreExecute"
, new SqlParameter("@PackageID", packageID)
, new SqlParameter("@ExecutionGuid", executionGuid));
}
private static void SetStatusToSuccess(int packageID, string executionGuid) {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_UpdateStatus_OnSuccess"
, new SqlParameter("@PackageID", packageID)
, new SqlParameter("@ExecutionGuid", executionGuid));
}
private static void SetStatusToRetry(int packageID, string executionGuid) {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_UpdateStatus_OnRetry"
, new SqlParameter("@PackageID", packageID)
, new SqlParameter("@ExecutionGuid", executionGuid));
}
private static void SetStatusToFailure(int packageID, string executionGuid) {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_UpdateStatus_OnFailure"
, new SqlParameter("@PackageID", packageID)
, new SqlParameter("@ExecutionGuid", executionGuid));
}
public bool IsJobRunning(int jobID) {
try {
string status = "";
List<DataRow> list = SQLManager.ExecuteQuery(strSqlConn, "IF EXISTS (SELECT 1 FROM Job_Executionlog WHERE JobID=" + jobID + " AND IsCurrent=1 AND Status='Running') SELECT 1 ELSE SELECT 0").Select().ToList();
if (list.Count() > 0) {
status = list[0][0].ToString();
}
if (status == "1") {
return true;
}
else {
return false;
}
}
catch (Exception e) {
LogManager.AppendLog(DateTime.Now.ToString() + " Execute JobControl Failed");
LogManager.AppendLog("Error Message: Failed to check job status. " + e.Message);
Console.WriteLine(e.Message);
throw new Exception("Failed to check job status", e);
}
}
public bool IsVaildArguments(int JobID, int IsStartOver) {
try {
string status = "";
List<DataRow> list = SQLManager.ExecuteQuery(strSqlConn, "SELECT TOP 1 JobID FROM Job_PackageMeta WHERE JobID=" + JobID).Select().ToList();
if (list.Count() > 0) {
status = list[0][0].ToString();
}
if (status == "") {
return false;
}
else if (IsStartOver != 0 && IsStartOver != 1) {
return false;
}
else {
return true;
}
}
catch (Exception e) {
LogManager.AppendLog(DateTime.Now.ToString() + " Execute JobControl Failed");
LogManager.AppendLog("Error Message: Verify argument failed, " + e.Message);
Console.WriteLine("Error Message: Verify argument failed: " + e.Message);
throw new Exception("Verify argument failed", e);
}
}
public static void ExitJob() {
try {
LogManager.AppendLog(DateTime.Now.ToString() + " Exit job.");
LogManager.AppendLog("Exit job");
LogManager.LogFile();
System.Environment.Exit(1);
}
catch (Exception e) {
LogManager.AppendLog(DateTime.Now.ToString() + " Job control failed");
LogManager.AppendLog("Error Message: " + e.Message);
LogManager.LogFile();
Console.WriteLine("Job control failed. " + e.Message);
//throw new Exception("Job control failed", e);
}
}
public static void AbortJob(int JobID, string executionGuid) {
try {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_EndJob"
, new SqlParameter("@JobID", JobID)
, new SqlParameter("@ExecutionGuid", executionGuid)
);
LogManager.AppendLog(DateTime.Now.ToString() + " Abort job.");
LogManager.AppendLog("Abort job");
LogManager.LogFile();
foreach (var meta in childrenProcs.Values.ToArray<JobMeta>()) {
if (meta.proc.HasExited == false) {
Console.WriteLine("Kill Process: " + meta.proc.Id.ToString());
meta.proc.Kill();
}
}
System.Environment.Exit(1);
}
catch (Exception e) {
LogManager.AppendLog(DateTime.Now.ToString() + " Job control failed");
LogManager.AppendLog("Error Message: " + e.Message);
LogManager.LogFile();
Console.WriteLine("Job control failed. " + e.Message);
//throw new Exception("Job control failed", e);
}
}
public static void SendMail(string ExecutionGuid) {
try {
SQLManager.ExecuteProc(strSqlConn, "usp_Job_SendMail"
, new SqlParameter("@ExecutionGuid", ExecutionGuid)
, new SqlParameter("@Tolist", strTolist)
, new SqlParameter("@CClist", strCCList)
, new SqlParameter("@MailProfile", strMailProfile)
);
}
catch (Exception e) {
LogManager.AppendLog(DateTime.Now.ToString() + " Send mail failed");
LogManager.AppendLog("Error Message: " + e.Message);
Console.WriteLine("Send mail failed. " + e.Message);
throw new Exception("Send mail failed", e);
}
}
}
}