IList <Task> MakeTasks(bool useSecondaryQueue = false, int numberOfTasks = 0) { return(Enumerable.Range(0, numberOfTasks).Select((i) => { Log.Debug($"Registering Dequeuer #{i}"); return Task.Factory.StartNew(() => { var queue = QueueAdapter.Make(useSecondaryQueue, QueueSettings, MailSettings); queue.Id = i; queueAdapters.Add(queue); queue.Consume(CancellationTokenSource.Token); }, TaskCreationOptions.LongRunning); }).ToList()); }
public async Task StartAsync() { var allTasks = new List <Task>(); Log.Debug("Start dequeue tasks for primary queue..."); allTasks.AddRange(MakeTasks(true, QueueSettings.PrimaryQueueConsumers)); Log.Debug("Start dequeue tasks for secondary queue..."); allTasks.AddRange(MakeTasks(false, QueueSettings.SecondaryQueueConsumers)); await Task.WhenAll(allTasks); // Recover from restart, if necessary. QueueAdapter.Make(false, QueueSettings, MailSettings).Recover(); QueueAdapter.Make(true, QueueSettings, MailSettings).Recover(); }