using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Svrnty.CQRS.Events.ConsumerGroups.Abstractions; /// /// Store for managing consumer group offsets and consumer registration. /// Enables multiple consumers to coordinate processing of event streams. /// public interface IConsumerOffsetStore { /// /// Commit an offset for a consumer in a group. /// This records that the consumer has successfully processed all events up to this offset. /// /// The consumer group ID. /// The consumer ID within the group. /// The stream name. /// The offset to commit. /// Cancellation token. /// A task representing the asynchronous operation. Task CommitOffsetAsync( string groupId, string consumerId, string streamName, long offset, CancellationToken cancellationToken = default); /// /// Get the last committed offset for a consumer group. /// Returns the minimum offset across all consumers in the group, representing /// the safe point up to which all events have been processed. /// /// The consumer group ID. /// The stream name. /// Cancellation token. /// The last committed offset, or null if no offset has been committed. Task GetCommittedOffsetAsync( string groupId, string streamName, CancellationToken cancellationToken = default); /// /// Get offsets for all consumers in a group. /// Useful for monitoring consumer lag and group status. /// /// The consumer group ID. /// The stream name. /// Cancellation token. /// Dictionary mapping consumer IDs to their committed offsets. Task> GetGroupOffsetsAsync( string groupId, string streamName, CancellationToken cancellationToken = default); /// /// Register a consumer as active (heartbeat). /// Consumers should call this periodically to signal they are alive. /// /// The consumer group ID. /// The consumer ID. /// Cancellation token. /// A task representing the asynchronous operation. Task RegisterConsumerAsync( string groupId, string consumerId, CancellationToken cancellationToken = default); /// /// Unregister a consumer (graceful shutdown). /// Should be called when a consumer is shutting down gracefully. /// /// The consumer group ID. /// The consumer ID. /// Cancellation token. /// A task representing the asynchronous operation. Task UnregisterConsumerAsync( string groupId, string consumerId, CancellationToken cancellationToken = default); /// /// Get all active consumers in a group. /// Returns consumers that have registered and are sending heartbeats. /// /// The consumer group ID. /// Cancellation token. /// List of active consumer information. Task> GetActiveConsumersAsync( string groupId, CancellationToken cancellationToken = default); /// /// Get all registered consumer groups. /// /// Cancellation token. /// List of group IDs. Task> GetAllGroupsAsync( CancellationToken cancellationToken = default); /// /// Clean up stale consumers that haven't sent heartbeats within the timeout period. /// Typically called by a background health monitor. /// /// Time after which a consumer is considered stale. /// Cancellation token. /// List of removed consumer infos. Task> CleanupStaleConsumersAsync( TimeSpan sessionTimeout, CancellationToken cancellationToken = default); }