public void Write(Job job) { var suspendedCount = 0; var suspended = false; lock (_queueAccess) { if (_reader != null && _reader.TrySetResult(job)) { return; } // Suspend if this is not the default job queue and this is currently overlfowed. if ((_suspendedCount > 0 || _items.Count >= Configuration.MaxQueueLength)) { _suspendedCount++; suspended = true; suspendedCount = _suspendedCount; } else { _items.Enqueue(job); } } if (!suspended) { return; } _recoverableAction.Run(() => _jobMutator.Mutate <JobQueue>(job, suspended: true)); _eventStream.Publish <JobQueue>(EventType.JobSuspended, EventProperty.JobSnapshot(job), EventProperty.Named("SuspendedCount", suspendedCount)); }
void PublishQueueInitializedEvent(int activityCount, int suspendedCount, Type activityType = null) { _eventStream.Publish <JobQueueFactory>( EventType.Activity, EventProperty.ActivityName("JobQueueInitialized"), EventProperty.Named("ActivityType", activityType == null ? "All" : activityType.FullName), EventProperty.Named("ActivityCount", activityCount), EventProperty.Named("SuspendedCount", suspendedCount)); }
void OnTick(object state) { var ready = new Collection <Tuple <Guid, DateTime?> >(); var notReady = new Collection <Tuple <Guid, DateTime?> >(); try { Tuple <Guid, DateTime?> item; _eventStream.Publish <FailedJobQueue>( EventType.TimerActivity, EventProperty.ActivityName("RescheduleFailedJobs"), EventProperty.Named("FailedItemsQueueLength", _jobFailures.Count)); while (_jobFailures.TryTake(out item)) { if (item.Item2 < _now()) { ready.Add(item); } else { notReady.Add(item); } } foreach (var job in ready.Select(i => _persistenceStore.Load(i.Item1))) { _router.Route(job); } ready.Clear(); } catch (Exception e) { if (e.IsFatal()) { throw; } _eventStream.Publish <FailedJobQueue>(e); } finally { foreach (var item in ready.Concat(notReady)) { _jobFailures.Add(item); } _timer.Change(_configuration.RetryTimerInterval, Timeout.InfiniteTimeSpan); } }
async Task <Job[]> LoadSuspended() { var list = new List <Job>(); while (list.Count == 0) { try { var max = Configuration.MaxQueueLength; var items = (Configuration.Type != null ? _persistenceStore.LoadSuspended(Configuration.Type, max) : _persistenceStore.LoadSuspended(_allActivityConfiguration.Select(c => c.Type), max)) .ToArray(); _eventStream.Publish <JobQueue>(EventType.Activity, EventProperty.ActivityName("LoadSuspendedItemsStarted"), EventProperty.Named("NumberOfItems", items.Length)); list.AddRange(items.Select(item => _jobMutator.Mutate <JobQueue>(item, suspended: false))); } catch (Exception e) { if (e.IsFatal()) { throw; } _eventStream.Publish <JobQueue>(e); } if (!list.Any()) { await Task.Delay(Configuration.RetryDelay); } } _eventStream.Publish <JobQueue>(EventType.Activity, EventProperty.ActivityName("LoadSuspendedItemsFinished"), EventProperty.Named("NumberOfItems", list.Count)); return(list.ToArray()); }