Path: redb-route/redb.Route.Firebase/Firestore/FirestorePollingConsumer.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Firebase/Firestore/FirestorePollingConsumer.cs ---- using System.Diagnostics; using System.Text.Json; using Google.Cloud.Firestore; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Telemetry; namespace redb.Route.Firebase; /// /// Firestore polling consumer (Realtime=false) — runs the query every Delay ms /// and diffs the result against the previous poll (DocumentId → UpdateTime): /// a new id is Added, a changed UpdateTime is Modified, an id that left /// the result set is Removed (body is null — the old data is not retained). /// The first poll delivers everything as Added; the diff lives in memory, so a restart /// starts from a clean snapshot — same as the realtime listener's initial snapshot. /// Note: Removed is only visible within the query result window (Where/Limit shift it). /// internal sealed class FirestorePollingConsumer : DrainableConsumer { private readonly FirestoreEndpoint _endpoint; private readonly FirestoreEndpointOptions _options; private Google.Cloud.Firestore.Query? _query; private Dictionary _lastSeen = new(); /// protected override IEndpoint ConsumerEndpoint => _endpoint; /// protected override string ConsumerName => $"fstore://{_endpoint.CollectionPath} (polling)"; internal FirestorePollingConsumer(FirestoreEndpoint endpoint, IProcessor processor, FirestoreEndpointOptions options) : base(processor) { _endpoint = endpoint; _options = options; } /// protected override async Task OnStarting(CancellationToken ct) { var db = await _endpoint.GetOrCreateDbAsync(ct).ConfigureAwait(false); Google.Cloud.Firestore.Query query = db.Collection(_endpoint.CollectionPath); if (_options.Where is not null) query = FirestoreQueryHelper.ApplyWhereFilters(query, _options.Where); if (_options.OrderBy is not null) query = FirestoreQueryHelper.ApplyOrderBy(query, _options.OrderBy); if (_options.Limit is not null) query = query.Limit(_options.Limit.Value); _query = query; } /// protected override async Task RunAsync(CancellationToken pollCt, CancellationToken processingCt) { if (_options.InitialDelay > 0) await Task.Delay(_options.InitialDelay, pollCt).ConfigureAwait(false); while (!pollCt.IsCancellationRequested) { try { await PollAsync(processingCt).ConfigureAwait(false); } catch (OperationCanceledException) when (pollCt.IsCancellationRequested) { break; } catch (Exception ex) { Logger?.LogError(ex, "Firestore polling failed: {Collection}", _endpoint.CollectionPath); _endpoint.RecordError(ex); } try { await Task.Delay(_options.Delay, pollCt).ConfigureAwait(false); } catch (OperationCanceledException) when (pollCt.IsCancellationRequested) { break; } } } private async Task PollAsync(CancellationToken ct) { var snapshot = await _query!.GetSnapshotAsync(ct).ConfigureAwait(false); var current = new Dictionary(snapshot.Count); foreach (var doc in snapshot.Documents) { if (ct.IsCancellationRequested) return; current[doc.Id] = doc.UpdateTime; string changeType; if (!_lastSeen.TryGetValue(doc.Id, out var prevUpdate)) changeType = "Added"; else if (!Equals(prevUpdate, doc.UpdateTime)) changeType = "Modified"; else continue; // unchanged await EmitAsync(doc, changeType, snapshot.ReadTime, ct).ConfigureAwait(false); } // Ids that left the query result window foreach (var (docId, _) in _lastSeen) { if (ct.IsCancellationRequested) return; if (!current.ContainsKey(docId)) await EmitRemovedAsync(docId, snapshot.ReadTime, ct).ConfigureAwait(false); } _lastSeen = current; } private async Task EmitAsync(DocumentSnapshot doc, string changeType, Timestamp readTime, CancellationToken ct) { var data = doc.ToDictionary(); object body = _options.RawJson ? JsonSerializer.Serialize(data) : data; var exchange = Exchange.Create(new Message(body), _endpoint.ScopeFactory); exchange.In.Headers[FirestoreHeaders.DocumentId] = doc.Id; exchange.In.Headers[FirestoreHeaders.DocumentPath] = doc.Reference.Path; exchange.In.Headers[FirestoreHeaders.CollectionPath] = _endpoint.CollectionPath; exchange.In.Headers[FirestoreHeaders.ChangeType] = changeType; exchange.In.Headers[FirestoreHeaders.CreateTime] = doc.CreateTime; exchange.In.Headers[FirestoreHeaders.UpdateTime] = doc.UpdateTime; exchange.In.Headers[FirestoreHeaders.ReadTime] = readTime; // MessagesIn is counted by the core StatisticsProcessor - ownership audit. using var span = StartReceiveSpan(); await ProcessWithTracking(exchange, span, ct).ConfigureAwait(false); } /// The root span of one change: a Firestore document carries no trace context. private TransportSpan StartReceiveSpan() => RouteTelemetryExtensions.StartConsumerSpan( (_endpoint.Component as ComponentBase)?.Context, $"firestore {_endpoint.CollectionPath} receive", ActivityKind.Consumer, "db.system", "firestore", _endpoint.Uri.NormalizedKey, null, static (_, _) => null, destination: _endpoint.CollectionPath, operation: "receive"); private async Task EmitRemovedAsync(string docId, Timestamp readTime, CancellationToken ct) { var exchange = Exchange.Create(new Message(null), _endpoint.ScopeFactory); exchange.In.Headers[FirestoreHeaders.DocumentId] = docId; exchange.In.Headers[FirestoreHeaders.CollectionPath] = _endpoint.CollectionPath; exchange.In.Headers[FirestoreHeaders.ChangeType] = "Removed"; exchange.In.Headers[FirestoreHeaders.ReadTime] = readTime; // MessagesIn is counted by the core StatisticsProcessor - ownership audit. using var span = StartReceiveSpan(); await ProcessWithTracking(exchange, span, ct).ConfigureAwait(false); } }