private static async Task MainServiceBusAsync() { EventHubProcessor processor = new EventHubProcessor(); clients.ForEach(serviceClient => serviceClient .OnMessage(message => { var messagestring = message.GetBody <String>(); try { processor.ProcessBatchFile(JsonConvert.DeserializeObject <Request>(messagestring)); count = count + 1; Console.WriteLine("Total Count1 :- {0}", count); message.CompleteAsync(); } catch (Exception ex) { Request objRequest = new Request(); objRequest = JsonConvert.DeserializeObject <Request>(messagestring); message.AbandonAsync(); } }, new OnMessageOptions() { MaxConcurrentCalls = 100, AutoComplete = false }) ); Console.WriteLine("Receiving. Press ENTER to stop worker."); Console.ReadLine(); clients.ForEach(sbclient => sbclient.Close()); }
public AnalyzerEngineProgram(AnalyzerEngine engine, FileLogger logger, FileLogger errorLogger, StatusSnapShotGenerator snapGen, EventHubProcessor eventHubProcessor) { Engine = engine; Logger = logger; ErrorLogger = errorLogger; SnapShotGenerator = snapGen; EventHubProcessor = eventHubProcessor; SnapShotGenerator.StartGenerator(); Engine.OnStateChanged += HandleEngineStateChange; Engine.OnReportException += HandleEngineException; EventProcessor.OnUpdatedInfo += HandleEventProcessorInfo; var toMessageLoop = new ToMessageLoop() { Engine = Engine, Logger = Logger, SnapShotGenerator = SnapShotGenerator, MesseageOutputQueue = MesseageOutputQueue }; var messageTimer = new Timer(MessageLoop, toMessageLoop, 0, 1000); Program.Timers.Add(messageTimer); var snapshotUpdateThread = new Thread(() => { while (true) { UpdateSnapshotAnalyzerInfo(); CurrentState.LastSnapshot = SnapShotGenerator.LastFileGeneratedTime; Thread.Sleep(4000); } }); snapshotUpdateThread.Name = nameof(snapshotUpdateThread); snapshotUpdateThread.Priority = ThreadPriority.BelowNormal; snapshotUpdateThread.IsBackground = true; snapshotUpdateThread.Start(); }
/// <summary> /// /// </summary> /// <param name="receiverindex"></param> /// <param name="r"></param> public static void StartReceiver(int receiverindex, EventHubReceiver r) { var starttime = DateTime.Now; bool isMessageAvailable = true; var requestdata = new WEP.GSP.Data.RequestData(); EventHubProcessor processor = new EventHubProcessor(); do { try { var messages = r.Receive(10); if (messages == null || messages.Count() == 0) { isMessageAvailable = false; } else { Task.Factory.StartNew(() => processor.EventMessageHandler(messages)); requestdata.UpdatePartitionOffset(Convert.ToString(receiverindex), messages.Last().Offset); requests = requests + messages.Count(); } } catch (Exception exception) { Trace.TraceError("exception on receive {0}", exception.Message); } } while (isMessageAvailable); requestdata.UpdateReleasePartitionLock(Convert.ToString(receiverindex)); }
async static void ProcessReceivedMessage(Task <BrokeredMessage> t) { EventHubProcessor processor = new EventHubProcessor(); BrokeredMessage message = t.Result; if (message != null) { var messagestring = message.GetBody <String>(); try { processor.ProcessMessage(JsonConvert.DeserializeObject <Request>(messagestring)); //var a = 0; //var i = 1 / a; count = count + 1; Console.WriteLine("Total Count :- {0}", count); message.CompleteAsync(); } catch (Exception ex) { Console.WriteLine("Error :- {0} - {1}", ex.Message, ex.InnerException); message.Abandon(); } } }
private static void Main() { DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND); var storageConnection = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString.Listen"]); EventHubName = ConfigurationManager.AppSettings["EventHubName"]; var hubProcessor = new EventHubProcessor(builder.ToString(), EventHubName); AnalyzerEngineProgram engineProgram = new AnalyzerEngineProgram(Engine, Logger, ErrorLogger, new StatusSnapShotGenerator(ConfigurationManager.AppSettings["jsonPath"], ErrorLogger), hubProcessor); var alarmQueueConnS = ConfigurationManager.AppSettings["ServiceBus.Queue.Connectionstring"]; var alarmQueueName = ConfigurationManager.AppSettings["ServiceBus.Queue.Name"]; 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"]); var eventStore = new SQLEventStore(Logger, ConfigurationManager.AppSettings["sqleventstoreConnection"]); var restartInput = new RestartInput() { RuleStorage = ruleStorage, AlarmMessageManager = alarmManger, EngineProgram = engineProgram }; var engineRestartTimer = new Timer(RestartLoop, restartInput, 0, 5000); Timers.Add(engineRestartTimer); var eventProcessorThread = new Thread(() => { var recTask = hubProcessor.StartReceiver <EventProcessor>(storageConnection); EventProcessor.Init(Engine, Logger, ErrorLogger, eventStore); recTask.Wait(); }); eventProcessorThread.Name = nameof(eventProcessorThread); var gui = new GuiHandler(engineProgram); var guiThread = new Thread(gui.Run); guiThread.IsBackground = true; guiThread.Start(); guiThread.Name = nameof(guiThread); eventProcessorThread.Priority = ThreadPriority.AboveNormal; eventProcessorThread.Start(); }
private static async Task MainServiceBusAsync() { EventHubProcessor processor = new EventHubProcessor(); clients.ForEach(serviceClient => serviceClient .OnMessage(message => { var messagestring = message.GetBody <String>(); try { processor.ProcessMessage(JsonConvert.DeserializeObject <Request>(messagestring)); //var a = 0; //var i = 1 / a; count = count + 1; Console.WriteLine("Total Count1 :- {0}", count); message.CompleteAsync(); } catch (Exception ex) { Request objRequest = new Request(); objRequest = JsonConvert.DeserializeObject <Request>(messagestring); //Console.WriteLine("Error :- {0} - {1}", ex.Message, ex.InnerException); //Console.WriteLine("Error :- {0}", messagestring); //Console.WriteLine("Error :- {0} - {1} - {2} -{3}", ex.Message, objRequest.BlobFile,objRequest.Blob,objRequest.ApiAction); message.AbandonAsync(); //new InsertExceptionLog(ex.Message, busMessage.BlobFile); } }, new OnMessageOptions() { MaxConcurrentCalls = 100, AutoComplete = false }) ); Console.WriteLine("Receiving. Press ENTER to stop worker."); Console.ReadLine(); clients.ForEach(sbclient => sbclient.Close()); }
private static async Task MainAsync() { EventHubProcessor processor = new EventHubProcessor(); clients.ForEach(serviceClient => serviceClient .OnMessage(message => { var messagestring = message.GetBody <String>(); try { processor.ProcessMessage(JsonConvert.DeserializeObject <Request>(messagestring)); message.CompleteAsync(); } catch (Exception ex) { Console.WriteLine("Error :- {0} - {1}", ex.Message, ex.InnerException); message.AbandonAsync(); } }, new OnMessageOptions() { MaxConcurrentCalls = 100, AutoComplete = false }) ); }
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."); }
private void HandleCommand(Commands command, string[] args) { OutputQueue.Enqueue("Command " + command + " recieved."); switch (command) { case Commands.help: OutputQueue.Enqueue("Available commands:"); foreach (var avCom in Enum.GetValues(typeof(Commands))) { OutputQueue.Enqueue(avCom.ToString()); } break; case Commands.exit: Program.RunRestartLoop = false; Engine.StopEngine(); Running = false; EventHubProcessor.StopReceiver(); SnapShotGenerator.Stop(); OutputQueue.Enqueue("Command sent to engine. See log for info."); break; case Commands.reloadrulesforanal: if (args.Length < 2 || string.IsNullOrEmpty(args[1])) { OutputQueue.Enqueue("missing analyzer name"); break; } Engine.ReloadRulesForAnalyzer(args[1]); OutputQueue.Enqueue("Command sent to engine. See log for info."); break; case Commands.showrulesforanal: if (args.Length < 2 || string.IsNullOrEmpty(args[1])) { OutputQueue.Enqueue("missing analyzer name"); break; } var rules = Engine.GetRulesLoadedInAnalyzer(args[1]); var loaded = rules.Aggregate("", (current, rule) => current + rule + Environment.NewLine); OutputQueue.Enqueue(loaded); break; case Commands.state: OutputQueue.Enqueue(CurrentState.ToString()); break; case Commands.showloadedanal: OutputQueue.Enqueue(Environment.NewLine); OutputQueue.Enqueue(string.Join(Environment.NewLine, Engine.GetCurrentAnalyzersInfo().Select(item => $"{item.Name}\t\t{item.State}\tin queue {item.EventsInQueue}.\tLoaded rules {item.NumberOfRulesLoaded}."))); break; case Commands.restart: Task.Run(() => { Thread.CurrentThread.IsBackground = true; SnapShotGenerator.Reset(); Engine.StopEngine(); }); OutputQueue.Enqueue("Command sent to engine. See log for info."); break; } }
void App_Startup(object sender, StartupEventArgs e) { MainWindow mainWindow = new MainWindow(Logger); mainWindow.SnapShotGenerator = new StatusSnapShotGenerator(ConfigurationManager.AppSettings["jsonPath"], Logger); var storageConnection = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString.Listen"]); var eventhubConnS = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString.Listen"]; var alarmQueueConnS = ConfigurationManager.AppSettings["ServiceBus.Queue.Connectionstring"]; var alarmQueueName = ConfigurationManager.AppSettings["ServiceBus.Queue.Name"]; 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"]); EventHubName = ConfigurationManager.AppSettings["EventHubName"]; var engineStartCounter = 0; var maxEngineRestarts = 10; Task.Run(() => { while (RunRestartLoop) { if (Engine.State == State.ShuttingDown) { continue; } if (Engine.State == State.Stopped) { Engine.StartEngine(ruleStorage, alarmManger); if (maxEngineRestarts <= engineStartCounter++) { var message = $"AnalyzerEngine main task has been restared {engineStartCounter - 1} times. Engine is down and can not recover! Resetting start counter."; Logger.AddRow(message); mainWindow.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 < 20000) { mainWindow.MesseageOutputQueue.Enqueue("Awaiting engine start. Waited " + timer.ElapsedMilliseconds + " ms"); Task.Delay(1000).Wait(); } timer.Reset(); } } }); Task.Run(() => { var connection = new EventHubProcessor(builder.ToString(), EventHubName); var recTask = connection.StartReceiver <EventProcessor>(storageConnection); // EventProcessor.Init(Engine, Logger, storageConnection, ConfigurationManager.AppSettings["OperationStorageTable"]); recTask.Wait(); }); mainWindow.SnapShotGenerator.StartGenerator(); mainWindow.Show(); }