public static void ReportEnvelopeWithException(QueueEnvelope envelope, Exception exception) { SystemError error = GetErrorFromExcetion(exception); error.MessageContent = envelope; ReportError(error); }
public static void PutToOperationQueue(params OperationRequest[] operationRequests) { if (operationRequests == null) { throw new ArgumentNullException("operationRequests"); } if (operationRequests.Length == 0) { return; } QueueEnvelope envelope = new QueueEnvelope(); envelope.ActiveContainerName = StorageSupport.CurrActiveContainer.Name; if (operationRequests.Length == 1) { envelope.SingleOperation = operationRequests[0]; } else { envelope.OrderDependentOperationSequence = OperationRequestCollection.CreateDefault(); envelope.OrderDependentOperationSequence.CollectionContent. AddRange(operationRequests.Where(oper => oper != null)); } PutToDefaultQueue(envelope); }
public static void PutToDefaultQueue(QueueEnvelope queueEnvelope) { string xmlString = queueEnvelope.SerializeToXml(); CloudQueueMessage message = new CloudQueueMessage(xmlString); CurrDefaultQueue.AddMessage(message); }
private static void RunQueueWorker(QueueEnvelope givenEnvelope) { bool loop = true; while (loop) { loop = givenEnvelope == null; QueueEnvelope envelope; if (givenEnvelope == null) { CloudQueueMessage message = null; envelope = QueueSupport.GetFromDefaultQueue(out message); QueueSupport.CurrDefaultQueue.DeleteMessage(message); } else { envelope = givenEnvelope; } try { WorkerSupport.ProcessMessage(envelope); } catch (Exception ex) { string msg = ex.Message; } //Thread.Sleep(5000); } }
private bool PollAndHandleMessage(Task[] tasks, int availableIx, Task availableTask) { CloudQueueMessage message; QueueEnvelope envelope = QueueSupport.GetFromDefaultQueue(out message); if (envelope != null) { Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope)); tasks[availableIx] = executing; QueueSupport.CurrDefaultQueue.DeleteMessage(message); if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } return(true); } else { if (message != null) { QueueSupport.CurrDefaultQueue.DeleteMessage(message); ErrorSupport.ReportMessageError(message); } GC.Collect(); return(false); } }
private static void ProcessErrors(bool useWorker) { if (useWorker) { List <QueueEnvelope> envelopes = new List <QueueEnvelope>(); List <CloudQueueMessage> messages = new List <CloudQueueMessage>(); CloudQueueMessage message = null; QueueEnvelope envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); while (envelope != null) { //WorkerSupport.ProcessMessage(envelope, false); //QueueSupport.CurrErrorQueue.DeleteMessage(message); messages.Add(message); envelope.CurrentRetryCount++; envelopes.Add(envelope); envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); } envelopes.ForEach(QueueSupport.PutToDefaultQueue); messages.ForEach(msg => QueueSupport.CurrErrorQueue.DeleteMessage(msg)); } else { CloudQueueMessage message = null; QueueEnvelope envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); while (envelope != null) { WorkerSupport.ProcessMessage(envelope, false); QueueSupport.CurrErrorQueue.DeleteMessage(message); envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); } } }
public static QueueEnvelope GetTarget_RequestEnvelope(string processId, IContainerOwner owner, string activeContainerName) { var envelope = new QueueEnvelope { OwnerPrefix = owner.ToFolderName(), ActiveContainerName = activeContainerName, SingleOperation = new OperationRequest { ProcessIDToExecute = processId } }; return envelope; }
public static void ProcessMessage(QueueEnvelope envelope, bool reportEnvelopeError = true) { try { InformationContext.Current.InitializeCloudStorageAccess(envelope.ActiveContainerName); if (envelope.SingleOperation != null) { ProcessSingleOperation(envelope.SingleOperation); } if (envelope.OrderDependentOperationSequence != null) { Exception firstException = null; //envelope.OrderDependentOperationSequence.CollectionContent.ForEach(ProcessSingleOperation); foreach (var singleOperation in envelope.OrderDependentOperationSequence.CollectionContent) { try { ProcessSingleOperation(singleOperation); } catch (Exception ex) { firstException = ex; ErrorSupport.ReportException(ex); } } if (firstException != null) { throw firstException; } } } catch (Exception ex) { if (reportEnvelopeError) { ErrorSupport.ReportEnvelopeWithException(envelope, ex); } throw; } finally { InformationContext.ProcessAndClearCurrent(); } counter++; if (counter >= 1000) { QueueSupport.ReportStatistics("Processed " + counter + " messages..."); counter = 0; } }
public static QueueEnvelope GetFromDefaultQueue(out CloudQueueMessage message) { message = CurrDefaultQueue.GetMessage(TimeSpan.FromMinutes(5)); if (message == null) { return(null); } try { QueueEnvelope queueEnvelope = QueueEnvelope.DeserializeFromXml(message.AsString); return(queueEnvelope); } catch { return(null); } }
public byte[] Serialize(object message) { var envelope = new QueueEnvelope(message); return _innerSerializer.SerializeToByteArray(envelope); }
private static void executeProcessRequestEnvelope(QueueEnvelope envelope, bool reportEnvelopeError) { var processID = envelope.SingleOperation.ProcessIDToExecute; string containerName = envelope.ActiveContainerName; string ownerPrefix = envelope.OwnerPrefix; if (String.IsNullOrEmpty(containerName) || String.IsNullOrEmpty(ownerPrefix)) throw new InvalidDataException("Container or owner missing from ProcessIDExecute envelope"); IContainerOwner owner = VirtualOwner.FigureOwner(ownerPrefix); try { InformationContext.Current.InitializeCloudStorageAccess(containerName: containerName); InformationContext.Current.Owner = owner; InformationContext.StartResourceMeasuringOnCurrent(InformationContext.ResourceUsageType.WorkerRole); ExecuteProcess.Execute(new ExecuteProcessParameters { ProcessID = processID }); } catch (Exception ex) { if (reportEnvelopeError) ErrorSupport.ReportEnvelopeWithException(envelope, ex); throw; } finally { InformationContext.ProcessAndClearCurrent(); } }
private static void RunQueueWorker(QueueEnvelope givenEnvelope) { bool loop = true; while (loop) { loop = givenEnvelope == null; QueueEnvelope envelope; if (givenEnvelope == null) { CloudQueueMessage message = null; envelope = QueueSupport.GetFromDefaultQueue(out message); QueueSupport.CurrDefaultQueue.DeleteMessage(message); } else { envelope = givenEnvelope; } try { WorkerSupport.ProcessMessage(envelope); } catch(Exception ex) { string msg = ex.Message; } //Thread.Sleep(5000); } }
public static void PutToOperationQueue(params OperationRequest[] operationRequests) { if (operationRequests == null) throw new ArgumentNullException("operationRequests"); if (operationRequests.Length == 0) return; QueueEnvelope envelope = new QueueEnvelope(); envelope.ActiveContainerName = StorageSupport.CurrActiveContainer.Name; if (operationRequests.Length == 1) envelope.SingleOperation = operationRequests[0]; else { envelope.OrderDependentOperationSequence = OperationRequestCollection.CreateDefault(); envelope.OrderDependentOperationSequence.CollectionContent. AddRange(operationRequests.Where(oper => oper != null)); } PutToDefaultQueue(envelope); }
public static void ExecuteMethod_PutEnvelopeToDefaultQueue(QueueEnvelope requestEnvelope) { QueueSupport.PutToDefaultQueue(requestEnvelope); }
public static void ProcessMessage(QueueEnvelope envelope, bool reportEnvelopeError = true) { if (envelope.SingleOperation != null && envelope.SingleOperation.ProcessIDToExecute != null) { executeProcessRequestEnvelope(envelope, reportEnvelopeError); return; } try { InformationContext.Current.InitializeCloudStorageAccess(envelope.ActiveContainerName); if (envelope.SingleOperation != null) ProcessSingleOperation(envelope.SingleOperation); if (envelope.OrderDependentOperationSequence != null) { Exception firstException = null; //envelope.OrderDependentOperationSequence.CollectionContent.ForEach(ProcessSingleOperation); foreach(var singleOperation in envelope.OrderDependentOperationSequence.CollectionContent) { try { ProcessSingleOperation(singleOperation); } catch(Exception ex) { firstException = ex; ErrorSupport.ReportException(ex); } } if (firstException != null) throw firstException; } } catch (Exception ex) { if (reportEnvelopeError) ErrorSupport.ReportEnvelopeWithException(envelope, ex); throw; } finally { InformationContext.ProcessAndClearCurrent(); } counter++; if (counter >= 1000) { QueueSupport.ReportStatistics("Processed " + counter + " messages..."); counter = 0; } }
private static void RunTaskedQueueWorker() { Task[] tasks = new Task[] { Task.Factory.StartNew(() => {}), Task.Factory.StartNew(() => {}), Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), }; bool IsStopped = false; int initialCount = 0; while (true) { try { Task.WaitAny(tasks); if (IsStopped) { Task.WaitAll(tasks); foreach (var task in tasks) { if (task.Exception != null) { ErrorSupport.ReportException(task.Exception); } } break; } int availableIx; Task availableTask = WorkerSupport.GetFirstCompleted(tasks, out availableIx); CloudQueueMessage message; QueueEnvelope envelope = QueueSupport.GetFromDefaultQueue(out message); if (envelope != null) { Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope)); tasks[availableIx] = executing; QueueSupport.CurrDefaultQueue.DeleteMessage(message); if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } } else { if (message != null) { QueueSupport.CurrDefaultQueue.DeleteMessage(message); ErrorSupport.ReportMessageError(message); } Thread.Sleep(1000); } } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { ErrorSupport.ReportException(e); } Thread.Sleep(10000); // or ... // ae.Flatten().Handle((ex) => ex is MyCustomException); } /* * catch (MessagingException e) * { * if (!e.IsTransient) * { * Trace.WriteLine(e.Message); * throw; * } * Thread.Sleep(10000); * }*/ catch (OperationCanceledException e) { if (!IsStopped) { Trace.WriteLine(e.Message); throw; } } catch (Exception ex) { ErrorSupport.ReportException(ex); throw; } } }