protected async override Task <JobResult> RunInternalAsync() { Log.Info().Message("Process user description job starting").Write(); int totalUserDescriptionsProcessed = 0; int totalUserDescriptionsToProcess = Context.GetWorkItemLimit(); while (!CancelPending && (totalUserDescriptionsToProcess == -1 || totalUserDescriptionsProcessed < totalUserDescriptionsToProcess)) { QueueEntry <EventUserDescription> queueEntry = null; try { queueEntry = await _queue.DequeueAsync(); } catch (Exception ex) { if (!(ex is TimeoutException)) { Log.Error().Exception(ex).Message("An error occurred while trying to dequeue the next EventUserDescription: {0}", ex.Message).Write(); return(JobResult.FromException(ex)); } } if (queueEntry == null) { continue; } _statsClient.Counter(StatNames.EventsUserDescriptionDequeued); Log.Info().Message("Processing EventUserDescription '{0}'.", queueEntry.Id).Write(); try { ProcessUserDescription(queueEntry.Value); totalUserDescriptionsProcessed++; _statsClient.Counter(StatNames.EventsUserDescriptionProcessed); } catch (DocumentNotFoundException ex) { _statsClient.Counter(StatNames.EventsUserDescriptionErrors); queueEntry.AbandonAsync().Wait(); Log.Error().Exception(ex).Message("An event with this reference id \"{0}\" has not been processed yet or was deleted. Queue Id: {1}", ex.Id, queueEntry.Id).Write(); continue; } catch (Exception ex) { _statsClient.Counter(StatNames.EventsUserDescriptionErrors); queueEntry.AbandonAsync().Wait(); // TODO: Add the EventUserDescription to the logged exception. Log.Error().Exception(ex).Message("An error occurred while processing the EventUserDescription '{0}': {1}", queueEntry.Id, ex.Message).Write(); return(JobResult.FromException(ex)); } await queueEntry.CompleteAsync(); } return(JobResult.Success); }
protected async Task DoWorkAsync(QueueEntry <SimpleWorkItem> w, AsyncCountdownEvent countdown, WorkInfo info) { Trace.WriteLine($"Starting: {w.Value.Id}"); Assert.Equal("Hello", w.Value.Data); try { // randomly complete, abandon or blowup. if (RandomData.GetBool()) { Trace.WriteLine($"Completing: {w.Value.Id}"); await w.CompleteAsync(); info.IncrementCompletedCount(); } else if (RandomData.GetBool()) { Trace.WriteLine($"Abandoning: {w.Value.Id}"); await w.AbandonAsync(); info.IncrementAbandonCount(); } else { Trace.WriteLine($"Erroring: {w.Value.Id}"); info.IncrementErrorCount(); throw new ApplicationException(); } } finally { Trace.WriteLine($"Signal {countdown.CurrentCount}"); countdown.Signal(); } }
protected async override Task <JobResult> RunInternalAsync() { Log.Info().Message("Process email message job starting").Write(); int totalEmailsProcessed = 0; int totalEmailsToProcess = Context.GetWorkItemLimit(); while (!CancelPending && (totalEmailsToProcess == -1 || totalEmailsProcessed < totalEmailsToProcess)) { QueueEntry <MailMessage> queueEntry = null; try { queueEntry = await _queue.DequeueAsync(); } catch (Exception ex) { if (!(ex is TimeoutException)) { Log.Error().Exception(ex).Message("An error occurred while trying to dequeue the next MailMessageNotification: {0}", ex.Message).Write(); return(JobResult.FromException(ex)); } } if (queueEntry == null) { continue; } _statsClient.Counter(StatNames.EmailsDequeued); Log.Info().Message("Processing MailMessageNotification '{0}'.", queueEntry.Id).Write(); try { await _mailSender.SendAsync(queueEntry.Value); totalEmailsProcessed++; _statsClient.Counter(StatNames.EmailsSent); } catch (Exception ex) { _statsClient.Counter(StatNames.EmailsSendErrors); queueEntry.AbandonAsync().Wait(); Log.Error().Exception(ex).Message("Error sending message '{0}': {1}", queueEntry.Id, ex.Message).Write(); } await queueEntry.CompleteAsync(); } return(JobResult.Success); }
private void DoWork(QueueEntry <SimpleWorkItem> w, CountDownLatch latch, ref int abandonCount, ref int errorCount) { Assert.Equal("Hello", w.Value.Data); latch.Signal(); // randomly complete, abandon or blowup. if (RandomHelper.GetBool()) { Console.WriteLine("Completing: {0}", w.Value.Id); w.CompleteAsync().Wait(); } else if (RandomHelper.GetBool()) { Console.WriteLine("Abandoning: {0}", w.Value.Id); w.AbandonAsync(); Interlocked.Increment(ref abandonCount); } else { Console.WriteLine("Erroring: {0}", w.Value.Id); Interlocked.Increment(ref errorCount); throw new ApplicationException(); } }
protected async Task DoWorkAsync(QueueEntry<SimpleWorkItem> w, AsyncCountdownEvent countdown, WorkInfo info) { Trace.WriteLine($"Starting: {w.Value.Id}"); Assert.Equal("Hello", w.Value.Data); try { // randomly complete, abandon or blowup. if (RandomData.GetBool()) { Trace.WriteLine($"Completing: {w.Value.Id}"); await w.CompleteAsync(); info.IncrementCompletedCount(); } else if (RandomData.GetBool()) { Trace.WriteLine($"Abandoning: {w.Value.Id}"); await w.AbandonAsync(); info.IncrementAbandonCount(); } else { Trace.WriteLine($"Erroring: {w.Value.Id}"); info.IncrementErrorCount(); throw new ApplicationException(); } } finally { Trace.WriteLine($"Signal {countdown.CurrentCount}"); countdown.Signal(); } }
protected async override Task <JobResult> RunInternalAsync() { Log.Info().Message("Process events job starting").Write(); int totalEventsProcessed = 0; int totalEventsToProcess = Context.GetWorkItemLimit(); while (!CancelPending && (totalEventsToProcess == -1 || totalEventsProcessed < totalEventsToProcess)) { QueueEntry <EventPost> queueEntry = null; try { queueEntry = await _queue.DequeueAsync(); } catch (Exception ex) { if (!(ex is TimeoutException)) { Log.Error().Exception(ex).Message("An error occurred while trying to dequeue the next EventPost: {0}", ex.Message).Write(); return(JobResult.FromException(ex)); } } if (queueEntry == null) { continue; } _statsClient.Counter(StatNames.PostsDequeued); Log.Info().Message("Processing EventPost '{0}'.", queueEntry.Id).Write(); List <PersistentEvent> events = null; try { _statsClient.Time(() => { events = ParseEventPost(queueEntry.Value); }, StatNames.PostsParsingTime); _statsClient.Counter(StatNames.PostsParsed); _statsClient.Gauge(StatNames.PostsBatchSize, events.Count); } catch (Exception ex) { _statsClient.Counter(StatNames.PostsParseErrors); queueEntry.AbandonAsync().Wait(); // TODO: Add the EventPost to the logged exception. Log.Error().Exception(ex).Message("An error occurred while processing the EventPost '{0}': {1}", queueEntry.Id, ex.Message).Write(); continue; } if (events == null) { queueEntry.AbandonAsync().Wait(); continue; } int eventsToProcess = events.Count; bool isSingleEvent = events.Count == 1; if (!isSingleEvent) { var project = _projectRepository.GetById(queueEntry.Value.ProjectId, true); // Don't process all the events if it will put the account over its limits. eventsToProcess = _organizationRepository.GetRemainingEventLimit(project.OrganizationId); // Add 1 because we already counted 1 against their limit when we received the event post. if (eventsToProcess < Int32.MaxValue) { eventsToProcess += 1; } // Increment by count - 1 since we already incremented it by 1 in the OverageHandler. _organizationRepository.IncrementUsage(project.OrganizationId, events.Count - 1); } int errorCount = 0; foreach (PersistentEvent ev in events.Take(eventsToProcess)) { try { _eventPipeline.Run(ev); totalEventsProcessed++; if (totalEventsToProcess > 0 && totalEventsProcessed >= totalEventsToProcess) { break; } } catch (ValidationException ex) { Log.Error().Exception(ex).Project(queueEntry.Value.ProjectId).Message("Event validation error occurred: {0}", ex.Message).Write(); } catch (Exception ex) { Log.Error().Exception(ex).Project(queueEntry.Value.ProjectId).Message("Error while processing event: {0}", ex.Message).Write(); if (!isSingleEvent) { // Put this single event back into the queue so we can retry it separately. _queue.EnqueueAsync(new EventPost { Data = Encoding.UTF8.GetBytes(ev.ToJson()).Compress(), ContentEncoding = "gzip", ProjectId = ev.ProjectId, CharSet = "utf-8", MediaType = "application/json", }).Wait(); } errorCount++; } } if (isSingleEvent && errorCount > 0) { queueEntry.AbandonAsync().Wait(); } else { queueEntry.CompleteAsync().Wait(); } } return(JobResult.Success); }
private void DoWork(QueueEntry<SimpleWorkItem> w, CountDownLatch latch, ref int abandonCount, ref int errorCount) { Assert.Equal("Hello", w.Value.Data); latch.Signal(); // randomly complete, abandon or blowup. if (RandomHelper.GetBool()) { Console.WriteLine("Completing: {0}", w.Value.Id); w.CompleteAsync().Wait(); } else if (RandomHelper.GetBool()) { Console.WriteLine("Abandoning: {0}", w.Value.Id); w.AbandonAsync(); Interlocked.Increment(ref abandonCount); } else { Console.WriteLine("Erroring: {0}", w.Value.Id); Interlocked.Increment(ref errorCount); throw new ApplicationException(); } }