Ejemplo n.º 1
0
        public async Task CheckUndispatchedAsync(IBucket <T> bucket, Guid streamId)
        {
            var sameCommitWait   = TimeSpan.Zero;
            var totalWait        = TimeSpan.Zero;
            var filterByStreamId = SameStreamOnly
                                ? (Guid?)streamId
                                : null;

            var prevUndispachedRevision = long.MinValue;

            while (true)
            {
                // Check if there is an undispatched
                var undispatched = await bucket.GetFirstUndispatchedCommitAsync(filterByStreamId)
                                   .ConfigureAwait(false);

                // No more undispatched
                if (undispatched == null)
                {
                    break;
                }

                // Is the same commit as before? if not reset the wait time
                if (prevUndispachedRevision != undispatched.BucketRevision)
                {
                    sameCommitWait          = TimeSpan.Zero;
                    prevUndispachedRevision = undispatched.BucketRevision;
                }

                // Wait
                await Task.Delay(AutoDispatchCheckInterval)
                .ConfigureAwait(false);

                totalWait      += AutoDispatchCheckInterval;
                sameCommitWait += AutoDispatchCheckInterval;

                if (totalWait >= MaxWaitTime)
                {
                    throw new UndispatchedEventsFoundException("Undispatched events found");
                }

                if (sameCommitWait >= AutoDispatchWaitTime)
                {
                    await DispatchLastCommitAsync(bucket, filterByStreamId, undispatched.BucketRevision)
                    .ConfigureAwait(false);
                }
            }
        }