private static void RunWorker() { while (true) { try { var resultsBatch = default(List <AuditorResultMessage>); lock (results) { if (results.Count != 0) { resultsBatch = results.Take(Global.MaxMessageBatchSize).ToList(); var removeCount = Math.Min(Global.MaxMessageBatchSize, results.Count); results.RemoveRange(0, removeCount); } } if (resultsBatch != default) { OutgoingMessageStorage.EnqueueMessage(new AuditorResultsBatch { AuditorResultMessages = resultsBatch }); continue; } Thread.Sleep(50); } catch (Exception exc) { logger.Error(exc); } } }
public AuditorContext(AuditorSettings settings, IStorage storage, StellarDataProviderBase stellarDataProvider, bool useLegacyOrderbook = false) : base(settings, storage, stellarDataProvider, useLegacyOrderbook) { AppState = new AuditorStateManager(this); AppState.StateChanged += AppState_StateChanged; QuantumHandler = new AuditorQuantumHandler(this); OutgoingMessageStorage = new OutgoingMessageStorage(); OutgoingResultsStorage = new OutgoingResultsStorage(this); MessageHandlers = new MessageHandlers <AuditorWebSocketConnection, AuditorContext>(this); }
protected override void ProcessTransactionTx(TransactionResponse tx) { try { if (Global.AppState.State == ApplicationState.Failed) { listener.Shutdown(); return; } var pagingToken = long.Parse(tx.PagingToken); var payments = AddVaultPayments(Transaction.FromEnvelopeXdr(tx.EnvelopeXdr), tx.Result.IsSuccess); var payment = new TxNotification { TxCursor = pagingToken, Payments = payments }; logger.Trace($"Tx with hash {tx.Hash} is handled. Number of payments for account {Global.Constellation.Vault} is {payment.Payments.Count}."); OutgoingMessageStorage.OnTransaction(payment); } catch (Exception exc) { var e = exc; if (exc is AggregateException) { e = exc.GetBaseException(); } logger.Error(e, "Transaction listener failed."); //if worker is broken, the auditor should quit consensus Global.AppState.State = ApplicationState.Failed; listener?.Shutdown(); throw; } }
private async Task ProcessOutgoingMessageQueue() { await Task.Factory.StartNew(async() => { while (!cancellationToken.IsCancellationRequested) { try { if (ConnectionState == ConnectionState.Ready && !cancellationToken.IsCancellationRequested && OutgoingMessageStorage.TryPeek(out MessageEnvelope message) ) { try { await base.SendMessage(message); if (!OutgoingMessageStorage.TryDequeue(out message)) { logger.Error("Unable to dequeue"); } } catch (Exception exc) { logger.Error(exc); } } else { Thread.Sleep(50); } } catch (Exception e) { logger.Error(e); } } }, cancellationToken); }
public override async Task <ResultMessage> Process(ProcessorContext context) { var initQuantum = (ConstellationInitQuantum)context.Envelope.Message; context.EffectProcessors.AddConstellationInit(initQuantum); var initSnapshot = PersistenceManager.GetSnapshot( (ConstellationInitEffect)context.EffectProcessors.Effects[0], context.Envelope.ComputeHash() ); await Global.Setup(initSnapshot); Global.AppState.State = ApplicationState.Running; if (!Global.IsAlpha) //set auditor to Ready state after init { //send new apex cursor message to notify Alpha that the auditor was initialized OutgoingMessageStorage.EnqueueMessage(new SetApexCursor { Apex = 1 }); Global.AppState.State = ApplicationState.Ready; } return(context.Envelope.CreateResult(ResultStatusCodes.Success, context.EffectProcessors.Effects)); }
private void PerformanceStatisticsManager_OnUpdates(PerformanceStatistics statistics) { OutgoingMessageStorage.EnqueueMessage(statistics.ToModel()); }