/// <summary> /// Thread entry point for asynchronous processing /// </summary> public virtual void Run() { if (_isNotFrameworkAlgorithm) { return; } IsActive = true; _cancellationTokenSource = new CancellationTokenSource(); // run main loop until canceled, will clean out work queues separately while (!_cancellationTokenSource.IsCancellationRequested) { try { ProcessAsynchronousEvents(); } catch (Exception err) { Log.Error(err); throw; } Thread.Sleep(1); } // finish alpha scoring analysis _alphaQueue.ProcessUntilEmpty(item => AlphaManager.Step(item.FrontierTimeUtc, item.SecurityValues, item.GeneratedAlphas)); // send final alpha scoring updates before we exit var alphas = AlphaManager.GetUpdatedContexts().Select(context => context.Alpha).ToList(); _messages.Enqueue(new AlphaResultPacket(AlgorithmId, Job.UserId, alphas)); // finish sending packets _messages.ProcessUntilEmpty(packet => _messagingHandler.Send(packet)); // persist alphas at exit StoreAlphas(); Log.Trace("DefaultAlphaHandler.Run(): Ending Thread..."); IsActive = false; }
/// <summary> /// Performs asynchronous processing, including broadcasting of alphas to messaging handler /// </summary> protected void ProcessAsynchronousEvents() { // step the alpha manager forward in time AlphaQueueItem item; while (_alphaQueue.TryDequeue(out item)) { AlphaManager.Step(item.FrontierTimeUtc, item.SecurityValues, item.GeneratedAlphas); } // send alpha upate messages Packet packet; while (_messages.TryDequeue(out packet)) { _messagingHandler.Send(packet); } // persist generated alphas to storage if (DateTime.UtcNow > _nextPersistenceUpdate) { StoreAlphas(); _nextPersistenceUpdate = DateTime.UtcNow + PersistenceUpdateInterval; } // push updated alphas through messaging handler if (DateTime.UtcNow > _nextMessagingUpdate) { var alphas = AlphaManager.GetUpdatedContexts().Select(context => context.Alpha).ToList(); if (alphas.Count > 0) { _messages.Enqueue(new AlphaResultPacket { AlgorithmId = AlgorithmId, Alphas = alphas }); } _nextMessagingUpdate = DateTime.UtcNow + MessagingUpdateInterval; } }