-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITFCMultiChannel.cs
556 lines (456 loc) · 21.2 KB
/
ITFCMultiChannel.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
using ITFCProxy;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Class created by Tiago de Souza Oliveira
/// Date: 26 October de 2016 01:33
/// This class decorated by the Orchestrator SDK allows it to be installed in Farm Orchestrator
///
/// It is endowed with written audit and error logs as well as telemetry in the Machine's Performance Counters
/// Performs multi-channel requests (parallel execution and serial execution channels within each channel) with end-time limiters and number of items to process per channel
///
/// Before running this DLL inside the Orchestrator, ensure that on all Orch farm servers, PerfCounter has been created (powershell script commented below)
///
/// Activity responsible for triggering SOAP requests for ITFC Web Services via .Net Parallels.
/// </summary>
namespace Orchestrator_ITFC_ParallelActivity
{
//Parallel Programming in the .NET Framework
//https://msdn.microsoft.com/en-us/library/dd460693(v=vs.110).aspx
[Activity("ITFC Multi Channel Request")]
public class ITFCMultiChannel
{
public enum ProcessStatusMultiChannel { Success, AllError, PartialError };
enum WSAction { Request, Response };
#region private declarations
private ProcessStatusMultiChannel _processStatus = ProcessStatusMultiChannel.Success;
private int _requestExecutedTotal = 0;
private int _responseReturnTotal = 0;
private int _totalTimeExecution = 0;
private int _workerThreads = 0;
private int _portThreads = 0;
private int _timeout = 3;
private int _channelNumber = 1;
private string _url = "http://contoso.com/aia/Service.svc";
private string _interfaceName = string.Empty;
private string _stepName = string.Empty;
private bool _auditLog = false;
private DateTime _endTimeExecution = DateTime.MinValue;
private int _queueOrCycleSize = 0;
private string _runbookServerName = string.Empty;
private string _logPathFile = @"C:\temp\";
#endregion private declarations
#region properties
[ActivityInput("Timeout (sec)")]
public int TimeOut
{
set { _timeout = value; }
}
[ActivityInput("Channel number")]
public int ChannelNumber
{
set { _channelNumber = value; }
}
[ActivityInput("URL (.svc)")]
public string URL
{
set { _url = value; }
}
[ActivityInput("Interface Name")]
public string InterfaceName
{
set { _interfaceName = value; }
}
[ActivityInput("Step Name")]
public string StepName
{
set { _stepName = value; }
}
[ActivityInput("Audit Log flag")]
public bool AuditLog
{
set { _auditLog = value; }
}
[ActivityInput("End time for execution")]
public DateTime EndTimeExecution
{
set { _endTimeExecution = value; }
}
[ActivityInput("Queue Size")]
public int QueueOrCycleSize
{
set { _queueOrCycleSize = value; }
}
[ActivityInput("Runbook Server Name")]
public string RunbookServerName
{
set { _runbookServerName = value; }
}
[ActivityInput("Pathfile directory")]
public string LogPathfile
{
set { _logPathFile = value; }
}
[ActivityOutput("Process Status")]
public ProcessStatusMultiChannel ProcessStatus
{
get
{
return RunMultiChannell();
}
}
[ActivityOutput("Request Executed Total")]
public int RequestExecutedTotal
{
get
{
return _requestExecutedTotal;
}
}
[ActivityOutput("Response Return Total")]
public int ResponseReturnTotal
{
get
{
return _responseReturnTotal;
}
}
[ActivityOutput("Total Time Execution (sec)")]
public int TotalTimeExecution
{
get
{
return _totalTimeExecution;
}
}
#endregion properties
/// <summary>
///
/// </summary>
/// <returns></returns>
public ProcessStatusMultiChannel RunMultiChannell()
{
StringBuilder sberroraudit = new StringBuilder();
Guid guidParent = Guid.NewGuid();
int splitmsgcyclesupper = 0;
Stopwatch timeTotal = new Stopwatch();
timeTotal.Start();
#region adjusting ThreadPool
//Obtendo disponibilidade de threads
System.Threading.ThreadPool.GetAvailableThreads(out _workerThreads, out _portThreads);
//Limitando o pedido de threads pela quantidade disponível
_channelNumber = (_channelNumber > _workerThreads) ? _workerThreads : _channelNumber;
System.Threading.ThreadPool.SetMinThreads(_channelNumber, _channelNumber);
#endregion ajustando ThreadPool
#region calculating message distribution per channel
decimal splitmsgscycles = decimal.Divide(_queueOrCycleSize, _channelNumber);
splitmsgcyclesupper = (Int32)(Math.Ceiling(splitmsgscycles));
#endregion
//number of channels to open to switch to Parallel
var channells = Enumerable.Range(1, _channelNumber);
Parallel.ForEach
(
channells,
new ParallelOptions { MaxDegreeOfParallelism = -1 },
item => ProcessChannell(guidParent, item, splitmsgcyclesupper, ref sberroraudit)
);
//Consolidated cycles
TimeSpan ts = timeTotal.Elapsed;
string tempototalrequisicaoHMSM = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
_totalTimeExecution = (int)ts.TotalSeconds;
#region Consolidated log
string msgcons = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
string.Empty,
string.Empty,
_interfaceName,
_stepName,
tempototalrequisicaoHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
0,
_runbookServerName,
string.Empty
);
#endregion log consolidado
sberroraudit.AppendLine(msgcons);
#region Log file
string fullfilepath = string.Format(@"{0}LogsServiceRequester_{1}{2}.csv", _logPathFile, _interfaceName, _stepName);
using (StreamWriter sw = File.AppendText(fullfilepath))
{
sw.WriteLine(sberroraudit.ToString().Trim());
}
#endregion Log file
return ProcessStatusMultiChannel.Success;
}
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <param name="guidParent"></param>
/// <param name="channell"></param>
/// <param name="totalCycles"></param>
private void ProcessChannell(Guid guidParent, int channell, int totalCycles, ref StringBuilder sb)
{
int counter = 0;
//If necessary, use this constructor's overload to pass parameters in the WebServices header
System.ServiceModel.EndpointAddress remoteAddress = new System.ServiceModel.EndpointAddress(_url);
//creating an HTTP connection per channel
//This is done so that the F5 understands a connection to smaller groups of request.
//It is from distinct connections that F5 performs load distribution on balanced nodes.
using (RequestProcessorClient itfc = new RequestProcessorClient(new System.ServiceModel.BasicHttpBinding(), remoteAddress))
{
//Setting the requisition timeout
itfc.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, _timeout);
//Do not exceed request timeout
while (_endTimeExecution > DateTime.Now)
{
if (totalCycles > counter)
{
Guid guidChild = Guid.NewGuid();
ExecutionStatus response = ExecutionStatus.NotExecuted;
string timeRequestHMSM = string.Empty;
try
{
//Informing PerfCounter of request
IncrementCounter(WSAction.Request, channell);
Stopwatch timeRequest = new Stopwatch();
string mensagemLog = string.Empty;
_requestExecutedTotal++;
timeRequest.Start();
//Running the request against the server
response = itfc.ExecuteStep(guidChild, _interfaceName, _stepName);
TimeSpan tsspan = timeRequest.Elapsed;
_responseReturnTotal++;
timeRequestHMSM = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", tsspan.Hours, tsspan.Minutes, tsspan.Seconds, tsspan.Milliseconds / 10);
//Informing PerfCounter of the request response
IncrementCounter(WSAction.Response, channell);
#region Msg Log Error and Audit
mensagemLog = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
response.ToString(),
guidChild.ToString(),
_interfaceName,
string.Format("{0}-{1}", _stepName, channell),
timeRequestHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
totalCycles,
_runbookServerName,
string.Empty
);
#endregion Msg Log Erro and Audit
//If the request is not successful, error logging
if (response != ExecutionStatus.Success)
{
sb.AppendLine(mensagemLog);
}
//If auditing is turned on, write same success log
if (_auditLog && response == ExecutionStatus.Success)
{
sb.AppendLine(mensagemLog);
}
}
catch (TimeoutException te)
{
//Contains at least 1 error the requisition batch
///TODO
///Calculate when it is total error
_processStatus = ProcessStatusMultiChannel.PartialError;
#region Log exception
string exception = string.Format("Timeout exception\n\n", te.Message);
string msgerr = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
response.ToString(),
guidChild.ToString(),
_interfaceName,
string.Format("{0}-{1}", _stepName, channell),
timeRequestHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
totalCycles,
_runbookServerName,
exception
);
#endregion Log exception
sb.AppendLine(msgerr);
}
catch (AggregateException ae)
{
//Contains at least 1 error the requisition batch
///TODO
///Calculate when it is total error
_processStatus = ProcessStatusMultiChannel.PartialError;
#region Log exception
string exception = string.Format("Parallel.ForEach exception\n\n", ae.Message);
string msgerr = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
response.ToString(),
guidChild.ToString(),
_interfaceName,
string.Format("{0}-{1}", _stepName, channell),
timeRequestHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
totalCycles,
_runbookServerName,
exception
);
#endregion Log exception
sb.AppendLine(msgerr);
}
catch (WebException wexc)
{
//Contains at least 1 error the requisition batch
///TODO
///Calculate when it is total error
_processStatus = ProcessStatusMultiChannel.PartialError;
#region Log exception
string exception = string.Format("HTTPCode:{0} \n\n Description:{1} \n\n EMessage:{2}",
((HttpWebResponse)wexc.Response).StatusCode.ToString(),
((HttpWebResponse)wexc.Response).StatusDescription.ToString(),
wexc.Message
);
string msgerr = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
response.ToString(),
guidChild.ToString(),
_interfaceName,
string.Format("{0}-{1}", _stepName, channell),
timeRequestHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
totalCycles,
_runbookServerName,
exception
);
#endregion Log exception
sb.AppendLine(msgerr);
}
catch (Exception exc)
{
//Contains at least 1 error the requisition batch
///TODO
///Calculate when it is total error
_processStatus = ProcessStatusMultiChannel.PartialError;
#region Log exception
string msgerr = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
guidParent.ToString(),
response.ToString(),
guidChild.ToString(),
_interfaceName,
string.Format("{0}-{1}", _stepName, channell),
timeRequestHMSM,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
_workerThreads,
_portThreads,
_channelNumber,
_queueOrCycleSize,
totalCycles,
_runbookServerName,
exc.Message
);
#endregion Log exception
sb.AppendLine(msgerr);
}
}
else
{
return;
}
counter++;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="action"></param>
/// <param name="channell"></param>
private void IncrementCounter(WSAction action, int channell)
{
PerformanceCounter performanceCounter = null;
PerformanceCounter totalPerformanceCounter = null;
try
{
string instanceNameMChannell = string.Format("{0}_{1}_{2}_{3}", _interfaceName, _stepName, action, channell);
//Counter to get total for each interface / step / request-response / execution channel
performanceCounter = new PerformanceCounter("ITFC Orch Call Counter", "Call/Sec", instanceNameMChannell, false);
performanceCounter.Increment();
if (action == WSAction.Request)
{
string instanceName = string.Format("Call Total - {0}_{1}", _interfaceName, _stepName);
//Counter for all interfaces / steps
totalPerformanceCounter = new PerformanceCounter("ITFC Orch Call Counter", "Call/Sec", instanceName, false);
totalPerformanceCounter.Increment();
//Counter for all interfaces / steps
totalPerformanceCounter = new PerformanceCounter("ITFC Orch Call Counter", "Call/Sec", "Call Total", false);
totalPerformanceCounter.Increment();
}
}
catch (Exception exc)
{
WriteLogError(exc);
}
finally
{
if (performanceCounter != null)
{
performanceCounter.Dispose();
}
if (totalPerformanceCounter != null)
{
totalPerformanceCounter.Dispose();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="exc"></param>
private void WriteLogError(Exception exc)
{
#region Log file
#region log general
string msgerro = string.Format("'{0}','{1}','{2}','{3}','{4}','{5}','{6}'",
string.Empty,
string.Empty,
string.Empty,
string.Empty,
string.Empty,
string.Empty,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
exc.Message
);
#endregion log consolidado
string fullfilepath = string.Format(@"{0}LogsServiceRequester_{1}{2}.csv", _logPathFile, _interfaceName, _stepName);
using (StreamWriter sw = File.AppendText(fullfilepath))
{
sw.WriteLine(msgerro);
}
#endregion Log file
}
}
}