-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomBatchWatchdog.cs
405 lines (344 loc) · 13.9 KB
/
CustomBatchWatchdog.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
using System;
using System.IO;
using System.ServiceProcess;
using System.Diagnostics;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Threading;
using Toolkit;
using System.Linq;
using System.Security.Principal;
using System.Text;
namespace CustomWatchdog
{
public partial class CustomBatchWatchdog : ServiceBase
{
private readonly ServiceLogManager m_log = new ServiceLogManager();
private readonly IServiceLog m_defaultLog;
private readonly string m_assemblyDirectory;
private readonly UserInfo m_user;
private readonly ManualResetEventSlim m_stopEvt = new ManualResetEventSlim(false);
private readonly ManualResetEventSlim m_doneEvt = new ManualResetEventSlim(true);
// defaults (can be overriden from a config file)
private RecoveryConfig m_config;
private string configFileName = "cbwatchdog.json";
private string eventLogSource = "Custom Batch Watchdog";
public CustomBatchWatchdog()
: this(null)
{
}
public CustomBatchWatchdog(IServiceLog log)
{
m_user = new UserInfo();
InitializeComponent();
var file = new Uri(GetType().Assembly.Location).LocalPath;
m_assemblyDirectory = Path.GetDirectoryName(file);
m_defaultLog = log ?? new ServiceLogEvent(this);
m_log.Add(m_defaultLog);
}
private IList<RecoveryConfigItem> RecoveryItems => m_config?.RecoveryItems;
internal string EventLogSource { get { return eventLogSource; } }
private void PrintWarning(string evt) => m_log.Write(ServiceLogLevel.Warning, evt);
private void PrintError(string evt) => m_log.Write(ServiceLogLevel.Error, evt);
private void PrintInfo(string evt) => m_log.Write(ServiceLogLevel.Info, evt);
private void PrintDebug(string evt) => m_log.Write(ServiceLogLevel.Debug, evt);
private void PrintTrace(string evt) => m_log.Write(ServiceLogLevel.Trace, evt);
private void LoadConfigFromFile()
{
string cfgPath = null;
try
{
JavaScriptSerializer ser = new JavaScriptSerializer();
cfgPath = GetConfigFile();
PrintInfo("Reading Configuration File :" + cfgPath);
var fi = new FileInfo(cfgPath);
var cfg = RecoveryConfig.Parse(fi);
cfg.Validate();
m_config = cfg;
ApplyLogs(cfg.Logs);
// Just print the json
var username = m_user.Name;
PrintInfo($"[{username}] Watchdog will be started with: {Environment.NewLine}{cfg}");
}
catch (IOException e)
{
var name = cfgPath ?? configFileName;
throw new Exception("Invalid format on: " + name, e);
}
}
private void ApplyLogs(IList<RecoveryConfigLog> logs)
{
if (logs != null)
{
var definedLogs = ServiceLogManager.Create(logs);
if (definedLogs.Any())
{
// We got some user defined logs, lower the level of the default log
m_defaultLog.Level = ServiceLogLevel.Error;
foreach (var log in definedLogs)
{
m_log.Add(log);
m_log.Write(ServiceLogLevel.Debug, log.ToString());
}
}
m_log.Write(ServiceLogLevel.Debug, $"[{m_log.Level}] Enabled");
}
}
/// <summary>
/// Looks for a config file in the same directory as the assembly
/// </summary>
/// <returns></returns>
private string GetConfigFile()
{
return GetFile(configFileName);
}
private string GetFile(string name)
{
// Check if the path is rooted
if (Path.IsPathRooted(name))
{
return name;
}
else
{
return Path.Combine(m_assemblyDirectory, name);
}
}
private TimeSpan Recover(RecoveryConfigItem rc)
{
ApplicationLoader.PROCESS_INFORMATION procInfo;
var timeout = m_config.RecoveryExecutionTimeout;
if (rc.OverrideRecoveryExecutionTimeout != 0)
{
timeout = rc.OverrideRecoveryExecutionTimeout;
}
var recoverTime = TimeSpan.FromMilliseconds((int)timeout);
var watch = Stopwatch.StartNew();
// This is the way to go if running as a service. But when debugging we don't have this privelege. Just spawn a new process
if (m_user.IsServiceAccount || m_user.IsSystemAccount)
{
ApplicationLoader.StartProcessAndBypassUAC(rc.RecoveryBatch, m_config.NoConsoleForRecoveryScript, timeout, PrintDebug, out procInfo);
}
else
{
ApplicationInlineLoader.Start(GetFile(rc.RecoveryBatch), m_config.NoConsoleForRecoveryScript, timeout, PrintDebug);
}
// Return the amount of time left to wait for recovery execution
return recoverTime - watch.Elapsed;
}
/// <summary>
///
/// </summary>
/// <param name="rc"></param>
/// <param name="retryTimespan">Retry to find the process for this interval</param>
/// <returns></returns>
private bool Check(RecoveryConfigItem rc, TimeSpan retryTimespan)
{
var watch = Stopwatch.StartNew();
string failed;
do
{
if (DoCheck(rc, out failed))
{
PrintDebug($"Find took {watch.Elapsed.TotalMilliseconds}ms");
return true;
}
Thread.Sleep(1);
} while (watch.Elapsed < retryTimespan);
return false;
}
private bool Check(RecoveryConfigItem rc)
{
string procName;
if (!DoCheck(rc, out procName))
{
LogCheckFailed(procName);
return false;
}
return true;
}
private void LogCheckFailed(string procName)
{
PrintWarning($"Watchdog couldn't find the process {procName}.");
}
private bool DoCheck(RecoveryConfigItem rc, out string failed)
{
// It's faster to get by name on each
foreach (string procName in rc.Processes)
{
var procs = Process.GetProcessesByName(procName);
var found = procs.Length > 0;
if (!found)
{
if (m_log.Level == ServiceLogLevel.Trace)
{
#if DBG_LOG
m_log.Write(ServiceLogLevel.Trace, $"Failed to find '{procName}'{Environment.NewLine}{GetRunning()}");
#else
m_log.Write(ServiceLogLevel.Trace, $"Failed to find '{procName}'");
#endif
}
failed = procName;
return false;
}
else if (m_log.Level == ServiceLogLevel.Trace)
{
var procInfo = string.Join(Environment.NewLine, procs.Select(p => $"[{p.ProcessName}] {p.Id}"));
m_log.Write(ServiceLogLevel.Trace, $"Found '{procName}' Processes:{Environment.NewLine}{procInfo}");
}
}
failed = null;
return CheckStarcounterApps(rc);
}
private string GetRunning()
{
var procs = Process.GetProcesses();
var sb = new StringBuilder(procs.Length * 10);
var nameWidth = procs.Max(p => p.ProcessName.Length) + 1;
var pidWidth = int.MaxValue.ToString().Length + 1;
foreach (var p in procs.OrderBy(p => p.ProcessName))
{
try
{
var paddedName = p.ProcessName.PadRight(nameWidth);
var paddedPid = p.Id.ToString().PadRight(pidWidth);
sb.AppendLine($"{paddedName}id: {paddedPid}, Session: {p.SessionId}");
}
catch
{
}
}
return sb.ToString();
}
private bool CheckStarcounterApps(RecoveryConfigItem rc)
{
var apps = rc.ScAppNames;
if (apps != null && apps.Count > 0)
{
var appNames = string.Join(", ", apps);
var db = rc.ScDatabase ?? "default";
var scFileName = "staradmin.exe";
PrintDebug($"Checking starcounter apps: {appNames}, db: {db}");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = string.IsNullOrEmpty(rc.StarcounterBinDirectory) ? scFileName : Path.Combine(rc.StarcounterBinDirectory, scFileName);
startInfo.Arguments = $"--database={db} list app";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
string stdOutput = process.StandardOutput.ReadToEnd();
bool allAppsAreRunning = apps.All(appName => stdOutput.Contains($"{appName} (in {db})"));
return allAppsAreRunning;
}
PrintDebug($"Skipped starcounter apps, not defined");
return true;
}
private void RunForever()
{
m_doneEvt.Reset();
try
{
DoRun();
}
finally
{
m_doneEvt.Set();
}
}
private void DoRun()
{
var healthCheckInterval = (int)m_config.HealthCheckInterval;
var criticalCounts = (int)m_config.CriticalCounts;
// Apply sanity check for the values
healthCheckInterval = Math.Max(1, healthCheckInterval); // Sleep at least 1 ms
criticalCounts = Math.Max(1, criticalCounts); // Make at least one attempt
while (!m_stopEvt.IsSet)
{
foreach (RecoveryConfigItem rc in RecoveryItems)
{
bool check = Check(rc);
int cntr = 0;
if (check == false)
{
do
{
TimeSpan itemRecoveryTs;
// Make at least one attempt
if (cntr++ == criticalCounts)
{
// maximum number of recovery attemps has been succeeded, abort
PrintDebug($"{(criticalCounts).ToString()} recovery attemps for {rc.RecoveryBatch} file has been made, aborting further attemps and moving on with next revoceryItem");
break;
}
else
{
// execute recovery
PrintDebug("Watchdog's recovery attempt #" + (cntr).ToString() + " procedure started: " + rc.RecoveryBatch);
itemRecoveryTs = Recover(rc);
}
check = Check(rc, itemRecoveryTs);
if (check == true)
{
PrintDebug("Watchdog's recovery attempt #" + (cntr).ToString() + " SUCCESS: " + rc.RecoveryBatch);
}
else
{
// TODO: level?
PrintWarning("Watchdog's recovery attempt #" + (cntr).ToString() + " FAILED: " + rc.RecoveryBatch);
}
} while (check == false);
}
}
m_stopEvt.Wait(healthCheckInterval);
}
}
protected override void OnStart(string[] args)
{
//System.Diagnostics.Debugger.Launch();
PrintInfo("Custom batch watchdog has been started.");
if (args.Length > 0)
{
OverrideSettings(args);
}
LoadConfigFromFile();
if (RecoveryItems.Any())
{
ThreadPool.QueueUserWorkItem(o => { RunForever(); });
}
else
{
PrintWarning("No recovery items, nothing to do");
}
}
private void OverrideSettings(string[] args)
{
if (args.Length > 0)
{
configFileName = args[0];
if (configFileName.IndexOf('/') > -1)
{
configFileName = configFileName.Replace("/", string.Empty);
}
PrintInfo("Config file updated to: " + configFileName);
}
if (args.Length > 1)
{
eventLogSource = args[1];
if (eventLogSource.IndexOf('/') > -1)
{
eventLogSource = eventLogSource.Replace("/", string.Empty);
}
PrintInfo("Event Source Name updated to: " + eventLogSource);
}
}
protected override void OnStop()
{
PrintInfo("Custom batch watchdog has been signalled to stop.");
m_stopEvt.Set();
// Give the loop some time to exit
m_doneEvt.Wait(TimeSpan.FromSeconds(5));
}
}
}