public MainWindow(FileLogger logger) { Logger = logger; InitializeComponent(); ShutDownButton.IsEnabled = false; EventProcessor.OnUpdatedInfo += HandleEventProcessorInfo; App.Engine.OnStateChanged += HandleEngineStateChange; App.Engine.OnNewAnalyzerInfo += HandleAnalyzerInfo; Task.Run(() => { while (true) { MessageAggregator.Collection.Clear(); for (var i = 0; i < 50; ++i) { if (App.Engine.EngineMessages.TryDequeue(out var msg)) { MessageAggregator.AddMessage(msg, msg.Message.GenerateMessageIdentifierFromString()); } } foreach (var messageTracker in MessageAggregator.Collection) { MesseageOutputQueue.Enqueue($"{messageTracker.Value.Message} | {messageTracker.Value.AmountCounter} times from {messageTracker.Value.FirstOccurrence} to {messageTracker.Value.LastOccurrence}"); } for (int i = 1; i <= 14; ++i) { if (MesseageOutputQueue.TryDequeue(out string message)) { //UpdateMessageBox($"{DateTime.UtcNow}\t{message}{Environment.NewLine}"); Logger.AddRow(message); SnapShotGenerator.AddMessageToSnapShot(DateTime.UtcNow, message); } } } }); Task.Run(() => { while (true) { UpdateSnapshotAnalyzerInfo(); Task.Delay(4000).Wait(); } }); StartUpdateSelectedAnalyzerTask(); }
private static void Main(string[] args) { SetWindowSize(WindowWidth, WindowHeight); Clear(); var storageConnection = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; var eventhubConnS = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString.Listen"]; Eventhubpath = ConfigurationManager.AppSettings["EventHubPath"]; var alarmQueueConnS = ConfigurationManager.AppSettings["ServiceBus.Queue.Connectionstring"]; var alarmQueueName = ConfigurationManager.AppSettings["ServiceBus.Queue.Name"]; WriteLineAndLog("Starting analyzer for hub: " + Eventhubpath); var alarmQueue = new ServiceBusConnection <AlarmMessage>(alarmQueueConnS, alarmQueueName); var alarmManger = new AlarmMessageManager(alarmQueue); var ruleStorage = new DocumentDBRuleStorage(ConfigurationManager.AppSettings["DocDBEndPointUrl"], ConfigurationManager.AppSettings["AuthorizationKey"], ConfigurationManager.AppSettings["RuleDatabaseId"], ConfigurationManager.AppSettings["RuleCollectionId"]); Engine = new AnalyzerEngine(); ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(eventhubConnS); builder.TransportType = TransportType.Amqp; var connection = new EventHubProcessor(builder.ToString(), Eventhubpath); WriteLineAndLog("Starting event receiver."); var recTask = connection.StartReceiver <EventProc>(storageConnection); recTask.Wait(); WriteLineAndLog("Receiver waiting."); var engineStartCounter = 0; var maxEngineRestarts = 10; try { new Thread(() => { Thread.CurrentThread.IsBackground = true; while (true) { if (!AwaitingInput) { Render(); Thread.Sleep(500); } } }).Start(); new Thread(() => { Thread.CurrentThread.IsBackground = true; while (true) { ParseInput(); } }).Start(); while (true) { MessageAggregator.Collection.Clear(); for (var i = 0; i < 500; ++i) { TimeStampedMessage <string> msg; if (Engine.EngineMessages.TryDequeue(out msg)) { MessageAggregator.AddMessage(msg, msg.Message.GenerateMessageIdentifierFromString()); } } foreach (var messageTracker in MessageAggregator.Collection) { MesseageOutputQueue.Enqueue($"{messageTracker.Value.Message} | {messageTracker.Value.AmountCounter} times from {messageTracker.Value.FirstOccurrence} to {messageTracker.Value.LastOccurrence}"); } if (Engine.State == State.ShuttingDown) { continue; } if (Engine.State == State.Stopped) { Engine.StartEngine(ruleStorage, alarmManger); if (maxEngineRestarts <= engineStartCounter++) { var message = $"AnalyserEngine main task has been restared {engineStartCounter - 1} times. Engine is down and can not recover! Resetting start counter."; Logger.AddRow(message); MesseageOutputQueue.Enqueue(message); var alarm = new AlarmMessage(AlarmLevel.High, AppDomain.CurrentDomain.FriendlyName, message); alarmManger.RaiseAlarm(alarm); engineStartCounter = 0; } } var timer = new Stopwatch(); timer.Start(); while (!Engine.EngineIsRunning && timer.ElapsedMilliseconds < 30000) { MesseageOutputQueue.Enqueue("Awaiting engine start. Waited " + timer.ElapsedMilliseconds + " ms"); Thread.Sleep(1000); } timer.Reset(); } } catch (Exception ex) { var alarm = new AlarmMessage(AlarmLevel.High, AppDomain.CurrentDomain.FriendlyName, $"Exception in main loop.", ex.Message); alarmManger.RaiseAlarm(alarm); WriteLineAndLog($"Exception in main loop."); WriteLineAndLog(ex.ToString()); } WriteLineAndLog("End of program."); }