private bool PollAndHandleSubscriptionChain(Task[] tasks, int availableIx, Task availableTask) { var result = SubscribeSupport.GetOwnerChainsInOrderOfSubmission(); if (result.Length == 0) { return(false); } string acquiredEtag = null; var firstLockedOwner = result.FirstOrDefault( lockCandidate => SubscribeSupport.AcquireChainLock(lockCandidate, out acquiredEtag)); if (firstLockedOwner == null) { return(false); } var executing = Task.Factory.StartNew( () => WorkerSupport.ProcessOwnerSubscriptionChains(firstLockedOwner, acquiredEtag, CURRENT_HARDCODED_CONTAINER_NAME)); tasks[availableIx] = executing; if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } return(true); }
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 void RefreshStockCompanyData() { InformationContext.Current.InitializeCloudStorageAccess(CURRENT_HARDCODED_CONTAINER_NAME); var oldDay = -1; while (true) { var refreshCharts = oldDay != DateTime.Now.DayOfYear || oldDay == -1; oldDay = DateTime.Now.Day; foreach (var groupId in TBRGroupRoot.GetAllGroupIDs()) { var group = TBRGroupRoot.RetrieveFromDefaultLocation(groupId).Group; var titanLock = GetLock(group); if (titanLock.IsLocked && titanLock.LastLocked > DateTime.Now.AddHours(-1) || titanLock.LastLocked > DateTime.Now.AddMinutes(-5)) { continue; } try { titanLock.IsLocked = true; titanLock.LastLocked = DateTime.Now; titanLock.StoreInformation(); RefreshStockCompaniesOfOwner(group); if (refreshCharts) { UpdateChartsOfOwner(group); } } catch (Exception ex) { ErrorSupport.ReportException(ex); } finally { titanLock.IsLocked = false; titanLock.StoreInformation(); } } Thread.Sleep(60000); } }
public override void Run() { GracefullyStopped = false; //ThreadPool.SetMinThreads(3, 3); Task[] tasks = new Task[] { Task.Factory.StartNew(() => { }), Task.Factory.StartNew(() => { }), Task.Factory.StartNew(() => { }), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), }; QueueSupport.ReportStatistics("Starting worker: " + CurrWorkerID, TimeSpan.FromDays(1)); Task.Factory.StartNew(RefreshStockCompanyData); while (!IsStopped) { try { Task.WaitAny(tasks); if (IsStopped) { break; } int availableIx; Task availableTask = WorkerSupport.GetFirstCompleted(tasks, out availableIx); bool handledSubscriptionChain = PollAndHandleSubscriptionChain(tasks, availableIx, availableTask); if (handledSubscriptionChain) { // TODO: Fix return value check Thread.Sleep(1000); continue; } bool handledMessage = PollAndHandleMessage(tasks, availableIx, availableTask); if (handledMessage) { continue; } 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; } } Task.WaitAll(tasks); foreach (var task in tasks.Where(task => task.Exception != null)) { ErrorSupport.ReportException(task.Exception); } QueueSupport.ReportStatistics("Stopped: " + CurrWorkerID, TimeSpan.FromDays(1)); GracefullyStopped = true; }
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; } } }