async Task <IStreamProcessorState> GetOrCreateStreamProcessorState(IStreamProcessorId streamProcessorId, IStreamProcessorState initialState, CancellationToken cancellationToken) { var tryGetStreamProcessorState = await _streamProcessorStates.TryGetFor(streamProcessorId, cancellationToken).ConfigureAwait(false); if (tryGetStreamProcessorState.Success) { return(tryGetStreamProcessorState.Result); } await _streamProcessorStates.Persist(streamProcessorId, initialState, cancellationToken).ConfigureAwait(false); return(initialState); }
/// <summary> /// Initializes a new instance of the <see cref="ScopedStreamProcessor"/> class. /// </summary> /// <param name="tenantId">The <see cref="TenantId"/>.</param> /// <param name="streamProcessorId">The <see cref="IStreamProcessorId" />.</param> /// <param name="sourceStreamDefinition">The source stream <see cref="IStreamDefinition" />.</param> /// <param name="initialState">The <see cref="StreamProcessorState" />.</param> /// <param name="processor">An <see cref="IEventProcessor" /> to process the event.</param> /// <param name="streamProcessorStates">The <see cref="IResilientStreamProcessorStateRepository" />.</param> /// <param name="eventsFromStreamsFetcher">The<see cref="ICanFetchEventsFromStream" />.</param> /// <param name="executionContext">The <see cref="ExecutionContext"/> of the stream processor.</param> /// <param name="eventFetcherPolicies">The policies to use while fetching events.</param> /// <param name="eventWatcher">The <see cref="IStreamEventWatcher" /> to wait for events to be available in stream.</param> /// <param name="timeToRetryGetter">The <see cref="ICanGetTimeToRetryFor{T}" /> <see cref="StreamProcessorState" />.</param> /// <param name="logger">An <see cref="ILogger" /> to log messages.</param> public ScopedStreamProcessor( TenantId tenantId, IStreamProcessorId streamProcessorId, IStreamDefinition sourceStreamDefinition, StreamProcessorState initialState, IEventProcessor processor, IResilientStreamProcessorStateRepository streamProcessorStates, ICanFetchEventsFromStream eventsFromStreamsFetcher, ExecutionContext executionContext, IEventFetcherPolicies eventFetcherPolicies, IStreamEventWatcher eventWatcher, ICanGetTimeToRetryFor <StreamProcessorState> timeToRetryGetter, ILogger logger) : base(tenantId, streamProcessorId, sourceStreamDefinition, initialState, processor, eventsFromStreamsFetcher, executionContext, eventFetcherPolicies, eventWatcher, logger) { _streamProcessorStates = streamProcessorStates; _timeToRetryGetter = timeToRetryGetter; }
/// <inheritdoc /> public async Task <AbstractScopedStreamProcessor> Create( IStreamDefinition streamDefinition, IStreamProcessorId streamProcessorId, IEventProcessor eventProcessor, ExecutionContext executionContext, CancellationToken cancellationToken) { var processorState = await GetOrCreateStreamProcessorState( streamProcessorId, streamDefinition.Partitioned ?Partitioned.StreamProcessorState.New : StreamProcessorState.New, cancellationToken) .ConfigureAwait(false); AbstractScopedStreamProcessor streamProcessor; if (streamDefinition.Partitioned) { if (processorState is not Partitioned.StreamProcessorState partitionedProcessorState) { throw new ExpectedPartitionedStreamProcessorState(streamProcessorId); } var eventFetcher = await _eventFetchers.GetPartitionedFetcherFor(eventProcessor.Scope, streamDefinition, cancellationToken).ConfigureAwait(false); streamProcessor = _createPartitionedStreamProcessor(streamDefinition, streamProcessorId, eventFetcher, eventProcessor, partitionedProcessorState, executionContext); } else { if (processorState is not StreamProcessorState unpartitionedProcessorState) { throw new ExpectedUnpartitionedStreamProcessorState(streamProcessorId); } var eventFetcher = await _eventFetchers.GetFetcherFor(eventProcessor.Scope, streamDefinition, cancellationToken).ConfigureAwait(false); streamProcessor = _createUnpartitionedStreamProcessor(streamDefinition, streamProcessorId, eventFetcher, eventProcessor, unpartitionedProcessorState, executionContext); } NotifyStream(streamProcessorId.ScopeId, streamDefinition, processorState.Position); return(streamProcessor); }