/// <summary> /// /// </summary> /// <returns>Null.</returns> public DockingStationEvent Execute() { Log.Info("PopQueueOperation: Deleting oldest message from iNet upload queue."); bool deleted = PersistedQueue.CreateInetInstance().Delete(); Log.Info("PopQueueOperation:" + (deleted ? "Message deleted." : "No messages to delete.")); return(null); }
/// <summary> /// This method implements the thread start for this service. /// </summary> protected override void Run() { // For logging, keep track of number of items found on queue, which of // those successfully uploaded, and which that were ignored/skipped. int queuedCount = 0, uploadedCount = 0, ignoredCount = 0; InetUploader inetUploader = null; try { PersistedQueue inetQueue = PersistedQueue.CreateInetInstance(); while (true) { if (!Runnable()) // Keep sending until the message queue is empty or we're stopped/paused. { break; } try { Log.Trace(">INET: Checking for non-empty queue."); bool isEmpty = inetQueue.IsEmpty(); if (isEmpty) { Log.Trace(">INET: No data in upload queue."); PendingUploads = false; break; } Log.Debug(">INET: Retrieving queued item...."); // Get the oldest object on the queue (the object is not removed). object queObject = inetQueue.Peek(); if (queObject == null) { Log.Debug(">INET: No data in upload queue."); PendingUploads = false; break; } PendingUploads = true; string deviceLocation = string.Empty; object wsParmObject = null; queuedCount++; QueueData queueData = null; try { queueData = (QueueData)queObject; double kB = (double)queueData.WebServiceParameterSize / 1024.0d; // convert bytes to kilobytes Log.Debug(string.Format(">INET: {0} ({1},\"{2}\",{3}), {4} KB)...", queueData, queueData.Id, Log.DateTimeToString(queueData.Timestamp), queueData.InetAccountNum, kB.ToString("f1"))); wsParmObject = queueData.WebServiceParameter; } catch (Exception e) { // If this failed, then something was on the queue which // was not the right type. Could have been old data. // Just ignore it. ignoredCount++; Log.Debug(">INET: " + e.ToString()); // If debug build, DO NOT delete poison data. We need to keep // it so that we can investigate what's wrong with it. #if DEBUG Log.Debug(">INET: Found non-conforming data on queue: "); Log.Debug(">INET: " + queObject.ToString()); break; #else Log.Debug(">INET: Found non-conforming data on queue. Purging it:"); Log.Debug(">INET: " + queObject.ToString()); //_msmqTransaction.Commit(); inetQueue.Delete(queueData.Id); continue; #endif } // We don't instantiate iNet until we know we need it And, if we make it // to here, then that's now. We then will continue to re-use it for the duration // that we remain in the loop. if (inetUploader == null) { inetUploader = new InetUploader(); } string errorCode = inetUploader.Upload(wsParmObject, queueData.Label, queueData.InetAccountNum); // On any error, just break out and we'll retry on next iteration of Run(). // The intent is that if there's some network issue, or we're just offline, // then we watn to let some time pass for the issue to clear up or to go back online. if (errorCode != string.Empty) { break; } // Now that we've successfully uploaded it to iNet, we can remove it from the queue Log.Debug(string.Format(">INET: Deleting uploaded {0} from queue.", queueData)); inetQueue.Delete(queueData.Id); uploadedCount++; } // catch catch (Exception ex1) { Log.Debug(">INET: Exception caught, - " + ex1.ToString()); } } // end-while !empty queue } finally { if (inetUploader != null) { inetUploader.Dispose(); } } if (queuedCount > 0) { Log.Debug(">INET: Finished. " + queuedCount + " found in queue, " + uploadedCount + " uploaded, " + ignoredCount + " ignored"); } }