-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cs
158 lines (134 loc) · 5.84 KB
/
test.cs
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
using Microsoft.Hpc.Scheduler;
using Microsoft.Hpc.Scheduler.Properties;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace JobSample
{
class Program
{
public static string InputConnectionString = @"Data Source=SQL\DB;Initial Catalog=Input;Integrated Security=True;timeout=0;";
public static List<string> LoadRecordList(string type, string InputConnString)
{
List<string> RecordList = new List<string>();
const string readRecordSql = @"select * from Input.dbo.TB where BLOK = @type AND CODE_01='111'";
// Making record's keys case insensitive.
Dictionary<string, object> dbRecord = new Dictionary<string, object>();
// Expecting to read exactly 1 record for this record ID.
using (var conn = new SqlConnection(InputConnString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = readRecordSql;
cmd.Parameters.AddWithValue("@type", type);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
RecordList.Add(reader["ID"].ToString());
}
}
}
}
return RecordList;
}
static ManualResetEvent running = new ManualResetEvent(false);
static void WaitForJob(IScheduler scheduler, ISchedulerJob job)
{
const JobState exitStates = JobState.Finished | JobState.Failed | JobState.Canceled;
ManualResetEvent checkJobState = new ManualResetEvent(false);
// Event handler for when the job state changes
EventHandler<JobStateEventArg> jobStatusCheck = (sender, e) =>
{
Console.WriteLine(String.Format(" Job {0} state is now {1}.", job.Id, e.NewState));
if ((e.NewState & exitStates) != 0)
checkJobState.Set();
};
// Event handler for when the eventing channel gets reconnected after a failure
EventHandler<ConnectionEventArg> schedulerConnectionEvent = (sender, e) =>
{
if (e.Code == ConnectionEventCode.EventReconnect)
{
Console.WriteLine(" Reconnect event detected");
//signal the thread to recheck the job state since the job state event may have been missed
// while we were disconnected.
checkJobState.Set();
}
else
{
Console.WriteLine(String.Format(" schedulerConnectionEvent {0}.", e.Code));
}
};
Console.WriteLine(String.Format("Waiting for job {0}...", job.Id));
// Register event handlers before checkJobState is Reset
job.OnJobState += jobStatusCheck;
scheduler.OnSchedulerReconnect += schedulerConnectionEvent;
try
{
do
{
checkJobState.Reset(); // Always Reset before job.Refresh to avoid losing state transitions
job.Refresh();
if ((job.State & exitStates) != 0)
{
Console.WriteLine(String.Format("Job {0} completed with state {1}.", job.Id, job.State));
return;
}
checkJobState.WaitOne();
} while (true);
}
finally
{
// must unregester handlers using the same job and scheduler objects that were used to register them above
// see comment "Register event handlers"
job.OnJobState -= jobStatusCheck;
scheduler.OnSchedulerReconnect -= schedulerConnectionEvent;
}
}
static void Main(string[] args)
{
List<string> RecordList = LoadRecordList("1", InputConnectionString);
List<string> RecordSet = new List<string>();
int num = 44 * 74;
for (int i = 0; i < RecordList.Count; i++)
{
if (i < num)
RecordSet.Add("'" + RecordList[i] + "'");
else
RecordSet[i % num] = RecordSet[i % num] + ",'" + RecordList[i] + "'";
}
string clusterName = Environment.GetEnvironmentVariable("CCP_SCHEDULER");
List<string> valuationdates = new List<string>();
valuationdates.Add("2021-06-28");
valuationdates.Add("2021-06-29");
valuationdates.Add("2021-06-30");
for (int j = 0; j < valuationdates.Count; j++)
{
using (IScheduler scheduler = new Scheduler())
{
scheduler.Connect(clusterName);
ISchedulerJob job = scheduler.CreateJob();
job.Name = "vvv";
job.UnitType = JobUnitType.Core;
for (int i = 0; i < num; i++)
{
ISchedulerTask task = job.CreateTask();
task.Name = "vvv";
task.CommandLine = "Project01.exe " + "/RecordIds:" + RecordSet[i] + " /ValuationDate:" + valuationdates[j];
task.WorkDirectory = @"C:\Run\";
job.AddTask(task);
}
scheduler.SubmitJob(job, null, null);
WaitForJob(scheduler, job);
}
}
}
}
}