public async Task Execute(IDocumentSession session) { if (!await session.TryGetGlobalTxLock(IncomingMessageLockId)) { return; } var incoming = await session.Connection.CreateCommand(_findAtLargeEnvelopesSql) .ExecuteToEnvelopes(); if (!incoming.Any()) { return; } session.MarkOwnership(_marker.Incoming, _settings.UniqueNodeId, incoming); await session.SaveChangesAsync(); _logger.RecoveredIncoming(incoming); foreach (var envelope in incoming) { envelope.OwnerId = _settings.UniqueNodeId; await _workers.Enqueue(envelope); } if (incoming.Count == _settings.Retries.RecoveryBatchSize && _workers.QueuedCount < _settings.MaximumLocalEnqueuedBackPressureThreshold) { _schedulingAgent.RescheduleIncomingRecovery(); } }
public async Task Execute(SqlConnection conn, ISchedulingAgent agent) { if (_workers.QueuedCount > _settings.MaximumLocalEnqueuedBackPressureThreshold) { return; } var tx = conn.BeginTransaction(); List <Envelope> incoming = null; try { if (!await conn.TryGetGlobalTxLock(tx, IncomingMessageLockId)) { tx.Rollback(); return; } incoming = await conn.CreateCommand(tx, _findAtLargeEnvelopesSql) .ExecuteToEnvelopes(); if (!incoming.Any()) { tx.Rollback(); return; } await markOwnership(conn, tx, incoming); tx.Commit(); } catch (Exception) { tx.Rollback(); throw; } _logger.RecoveredIncoming(incoming); foreach (var envelope in incoming) { envelope.OwnerId = _settings.UniqueNodeId; await _workers.Enqueue(envelope); } if (incoming.Count == _settings.Retries.RecoveryBatchSize && _workers.QueuedCount < _settings.MaximumLocalEnqueuedBackPressureThreshold) { agent.RescheduleIncomingRecovery(); } }