Exemplo n.º 1
0
        public static IEnumerable <T> Scan <T>(
            [NotNull] this IHerculesTimelineClient <T> client,
            [NotNull] ScanTimelineQuery query,
            TimeSpan perRequestTimeout,
            CancellationToken cancellationToken = default)
        {
            var nextCoordinates = TimelineCoordinates.Empty;

            while (true)
            {
                var readQuery = new ReadTimelineQuery(query.Name)
                {
                    From             = query.From,
                    To               = query.To,
                    Limit            = query.BatchSize,
                    ClientShard      = query.ClientShard,
                    ClientShardCount = query.ClientShardCount,
                    Coordinates      = nextCoordinates
                };

                var readPayload = client.Read(readQuery, perRequestTimeout, cancellationToken).Payload;

                foreach (var @event in readPayload.Events)
                {
                    yield return(@event);
                }

                if (readPayload.Events.Count < query.BatchSize)
                {
                    break;
                }

                nextCoordinates = readPayload.Next;
            }
        }
        /// <inheritdoc />
        public async Task <ReadTimelineResult <T> > ReadAsync(ReadTimelineQuery query, TimeSpan timeout, CancellationToken cancellationToken = default)
        {
            try
            {
                var url = new RequestUrlBuilder("timeline/read")
                {
                    { Constants.QueryParameters.Timeline, query.Name },
                    { Constants.QueryParameters.Limit, query.Limit },
                    { Constants.QueryParameters.ClientShard, query.ClientShard },
                    { Constants.QueryParameters.ClientShardCount, query.ClientShardCount },
                    { "from", EpochHelper.ToUnixTimeUtcTicks(query.From.UtcDateTime) },
                    { "to", EpochHelper.ToUnixTimeUtcTicks(query.To.UtcDateTime) }
                }
                .Build();

                var body = CreateRequestBody(query.Coordinates ?? TimelineCoordinates.Empty);

                var request = Request
                              .Post(url)
                              .WithContentTypeHeader(Constants.ContentTypes.OctetStream)
                              .WithContent(body);

                var result = await client
                             .SendAsync(request, timeout, cancellationToken : cancellationToken)
                             .ConfigureAwait(false);

                try
                {
                    var operationStatus = responseAnalyzer.Analyze(result.Response, out var errorMessage);
                    if (operationStatus != HerculesStatus.Success)
                    {
                        return(new ReadTimelineResult <T>(operationStatus, null, errorMessage));
                    }

                    return(new ReadTimelineResult <T>(operationStatus, ParseResponseBody(result.Response)));
                }
                finally
                {
                    if (result.Response.HasContent)
                    {
                        bufferPool.Return(result.Response.Content.Buffer);
                    }
                }
            }
            catch (Exception error)
            {
                log.Error(error);

                return(new ReadTimelineResult <T>(HerculesStatus.UnknownError, null, error.Message));
            }
        }
Exemplo n.º 3
0
        public async Task <ReadTimelineResult> ReadAsync(ReadTimelineQuery query, TimeSpan timeout, CancellationToken cancellationToken = new CancellationToken())
        {
            var result = await client.ReadAsync(query, timeout, cancellationToken).ConfigureAwait(false);

            return(result.FromGenericResult());
        }
Exemplo n.º 4
0
 public static ReadTimelineResult <T> Read <T>(
     [NotNull] this IHerculesTimelineClient <T> client,
     [NotNull] ReadTimelineQuery query,
     TimeSpan timeout,
     CancellationToken cancellationToken = default) =>
 client.ReadAsync(query, timeout, cancellationToken).GetAwaiter().GetResult();
            public async Task <ReadTimelineResult <HerculesEvent> > ReadAsync(ReadTimelineQuery query, TimeSpan timeout, CancellationToken cancellationToken = default)
            {
                var result = await client.ReadAsync(query, timeout, cancellationToken).ConfigureAwait(false);

                return(result.ToGenericResult());
            }