/// <summary> /// This is where your processing happens /// </summary> /// <param name="manager"></param> private void controller_ExecuteStart(QueueMessageManager manager) { // get active queue item var item = manager.Item; // Typically perform tasks based on some Action/request if (item.Action == "PRINTIMAGE") { // recommend you offload processing //PrintImage(manager); } else if (item.Action == "RESIZETHUMBNAIL") { //ResizeThumbnail(manager); } // just for kicks Interlocked.Increment(ref RequestCount); // every other request should throw exception, trigger ExecuteFailed if (RequestCount % 2 == 0) { // Execption: object obj = null; obj.ToString(); } // Complete request manager.CompleteRequest(messageText: "Completed request " + DateTime.Now, autoSave: true); }
// global instances that keep controller and windows service alive public void Start(QueueMessageManager manager = null) { if (manager == null) { manager = new QueueMessageManagerSql(); } LogManager.Current.LogInfo("Start called"); var config = QueueMessageManagerConfiguration.Current; Controller = new QueueController() { ConnectionString = config.ConnectionString, QueueName = config.QueueName, WaitInterval = config.WaitInterval, ThreadCount = config.ControllerThreads }; LogManager.Current.LogInfo("Controller created."); // asynchronously start the SignalR hub SignalR = WebApplication.Start <SignalRStartup>("http://*:8080/"); // *** Spin up n Number of threads to process requests Controller.StartProcessingAsync(); LogManager.Current.LogInfo(String.Format("QueueManager Controller Started with {0} threads.", Controller.ThreadCount)); // Set static instances so that these 'services' stick around GlobalService.Controller = Controller; GlobalService.Service = this; }
protected override void OnExecuteFailed(QueueMessageManager manager, Exception ex) { var qm = manager.Item; WriteMessage(qm); base.OnExecuteFailed(manager, ex); }
private void controller_ExecuteComplete(QueueMessageManager manager) { // grab the active queue item var item = manager.Item; // Log or otherwise complete request Console.WriteLine(item.Id + " - " + item.QueueName + " - Item Completed"); }
protected void OnError(QueueMessageManager manager, string message = null) { if (message == null) { message = manager.ErrorMessage; } // clear out the message // don't do this manager.CancelRequest(messageText: "Failed: " + message, autoSave: true); LogManager.Current.LogError(message); // send email //AppUtils.SendAdminEmail("MPWFQMM Failure", errorMessage); }
protected override void ExecuteSteps(QueueMessageManager manager) { // show a started message Stopwatch watch = new Stopwatch(); watch.Start(); // store on a thread LocalDataStoreSlot threadData = Thread.GetNamedDataSlot(STR_STARTTIME_KEY); Thread.SetData(threadData, watch); QueueMonitorServiceHub.WriteMessage(manager.Item); base.ExecuteSteps(manager); }
/// <summary> /// Implement OnExecuteStart to run queued actions/operations. /// Call manager.CompleteRequest() or manager.CancelRequest() to /// complete queue items. /// /// Below is a commented simple example /// </summary> /// <param name="manager"></param> //protected override void OnExecuteStart(QueueMessageManager manager) //{ // base.OnExecuteStart(manager); // // string action = manager.Item.Action; // try // { // switch (action) // { // case "HelloWorld": // { // Thread.Sleep(2000); // manager.CompleteRequest(messageText: queueItem.Message + " - NEW completed at " + DateTime.Now, // autoSave: true); // break; // } // default: // { // manager.CancelRequest(messageText: "Failed: No matching action", autoSave: true); // break; // } // } // } // catch (Exception ex) // { // manager.CancelRequest(messageText: "Failed: " + ex.Message, autoSave: true); // } //} protected override void OnExecuteComplete(QueueMessageManager manager) { base.OnExecuteComplete(manager); // Get Starting timestamp from TextInput int elapsed = 0; DateTime time = DateTime.UtcNow; if (DateTime.TryParse(manager.Item.TextInput, out time)) { elapsed = (int)DateTime.UtcNow.Subtract(time.ToUniversalTime()).TotalMilliseconds; } // also send down a waiting message count int waitingMessages = GetWaitingMessageCount(manager); WriteMessage(manager.Item, elapsed, waitingMessages); }
protected override void OnExecuteFailed(QueueMessageManager manager, Exception ex) { base.OnExecuteFailed(manager, ex); //timestamp from text input int elapsed = 0; DateTime time = DateTime.UtcNow; if (DateTime.TryParse(manager.Item.TextInput, out time)) { elapsed = (int)DateTime.UtcNow.Subtract(time.ToUniversalTime()).TotalMilliseconds; } var qm = manager.Item; int waitingMessages = GetWaitingMessageCount(manager); WriteMessage(qm, elapsed, waitingMessages); }
protected override void OnExecuteStart(QueueMessageManager manager) { base.OnExecuteStart(manager); var queueItem = manager.Item; try { string action = queueItem.Action; if (!string.IsNullOrEmpty(action)) { //Initialize Anything action = action.Trim(); } switch (action) { default: // TODO: Remove for production Thread.Sleep((int)(DateTime.Now.Ticks % 500)); // use this instead to ensure that messages get updated properly and consistently // that is: All flags are set, date is always UTC date, etc. //if (!manager.CancelRequest(messageText: "Unknown Action", autoSave: true)) if (!manager.CompleteRequest(messageText: "Processing complete.", autoSave: true)) { // this is pointless - if this save fails // it's likely the save you are doing in // onError will also fail OnError(manager); return; } //manager.CompleteCancelRequest(messageText: "Invalid message action provided."); break; } } catch (Exception ex) { OnError(manager, ex.GetBaseException().Message); return; } }
protected override void OnExecuteComplete(QueueMessageManager manager) { base.OnExecuteComplete(manager); LocalDataStoreSlot threadData = Thread.GetNamedDataSlot(STR_STARTTIME_KEY); var watch = Thread.GetData(threadData); int elapsed = 0; if (watch != null) { ((Stopwatch)watch).Stop(); elapsed = (int)((Stopwatch)watch).ElapsedMilliseconds; } watch = null; Thread.SetData(threadData, watch); int waitingMessages = manager.GetWaitingQueueMessageCount(QueueName); WriteMessage(manager.Item, elapsed, waitingMessages); }
protected void OnError(QueueMessageManager manager, string message = null, Exception ex = null) { if (message == null) { message = manager.ErrorMessage; } manager.CancelRequest(messageText: "Queue Error: " + message); manager.Save(); string details = null; if (ex != null) { details = ex.Source + "\r\n" + ex.StackTrace; } //LogManager.Current.LogError(message, details); // send email //AppUtils.SendAdminEmail("MPWFQMM Failure", errorMessage); }
protected override void OnExecuteStart(QueueMessageManager manager) { // write a 'request starting' message to QueueMonitor manager.Item.TextInput = DateTime.UtcNow.ToString("u"); QueueMonitorServiceHub.WriteMessage(manager.Item); // always call base to allow any explicit events to also process base.OnExecuteStart(manager); var queueItem = manager.Item; try { // Action can be used as a routing mechanism // to determine which operation to perfom string action = queueItem.Action; switch (action) { case "HELLOWORLD": { // call whatever long running operations you need to run Thread.Sleep(2000); // always either complete or cancel the request manager.CompleteRequest(messageText: queueItem.Message + " - HELLOWORLD completed at " + DateTime.Now, autoSave: true); break; } case "NEWXMLORDER": { // call whatever long running operations you need to run Thread.Sleep(2000); // always either complete or cancel the request manager.CompleteRequest(messageText: queueItem.Message + " - NEWXMLORDER completed at " + DateTime.Now, autoSave: true); break; } case "GOBIG": { // call whatever long running operations you need to run Thread.Sleep(4000); // always either complete or cancel the request manager.CompleteRequest(messageText: queueItem.Message + " - GO BIG OR GO HOME completed at " + DateTime.Now, autoSave: true); break; } default: // All requests that get picked up by the queue get their started properties set, // so we MUST mark them complete, even if we did not have any local action code here, // because we cannot leave them in the half-way complete state. manager.CancelRequest(messageText: "Processing failed - action not supported: " + action, autoSave: true); break; } } catch (Exception ex) { var ex2 = ex.GetBaseException(); // route to OnError (base) which logs error // and cancels the request OnError(manager, ex2.Message, ex2); } }
void Controller_ExecuteStart(QueueMessageManager Message) { this.WriteEntry("Started: " + Message.Item.Started + "->" + Message.Item.Id); }
void Controller_ExecuteComplete(QueueMessageManager Message) { this.WriteEntry("Complete: " + Message.Item.Completed + "->" + Message.Item.Id + " - " + Message.Item.Message); }
void Controller_ExecuteFailed(QueueMessageManager Message, Exception ex) { this.WriteEntry("Failed: " + Message.Item.Completed + "->" + Message.Item.Id + " - " + ex.Message); }
private void controller_ExecuteFailed(QueueMessageManager manager, Exception ex) { Console.WriteLine("Failed (on purpose): " + manager.Item.QueueName + " - " + manager.Item.Id + " - " + ex.Message); }
int GetWaitingMessageCount(QueueMessageManager manager, int delay = 10) { return(manager.GetWaitingQueueMessageCount(QueueName)); }