private static async Task SendToInstance <T>(SagaQueryConsumeContext <TSaga, T> context, ISagaPolicy <TSaga, T> policy, TSaga instance, IPipe <SagaConsumeContext <TSaga, T> > next, IAsyncDocumentSession session) where T : class { try { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Used {2}", TypeMetadataCache <TSaga> .ShortName, instance.CorrelationId, TypeMetadataCache <T> .ShortName); } var sagaConsumeContext = new RavenDbSagaConsumeContext <TSaga, T>(session, context, instance); await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false); } catch (SagaException) { throw; } catch (Exception ex) { throw new SagaException(ex.Message, typeof(TSaga), typeof(T), instance.CorrelationId, ex); } }
public async Task Send(SagaConsumeContext <TSaga, TMessage> context) { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Added {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } SagaConsumeContext <TSaga, TMessage> proxy = new RavenDbSagaConsumeContext <TSaga, TMessage>(_session, context, context.Saga); await _next.Send(proxy).ConfigureAwait(false); if (!proxy.IsCompleted) { await _session.StoreAsync(context.Saga, GetSagaId(_session, context.Saga)); } }
async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) { if (!context.CorrelationId.HasValue) { throw new SagaException("The CorrelationId was not specified", typeof(TSaga), typeof(T)); } var sagaId = context.CorrelationId.Value; using (var session = OpenSession()) { var inserted = false; TSaga instance; if (policy.PreInsertInstance(context, out instance)) { inserted = await PreInsertSagaInstance <T>(session, instance); } try { if (instance == null) { instance = await session.LoadAsync <TSaga>(ConvertToSagaId(session, sagaId)) .ConfigureAwait(false); } if (instance == null) { var missingSagaPipe = new MissingPipe <T>(session, next); await policy.Missing(context, missingSagaPipe).ConfigureAwait(false); } else { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Used {2}", TypeMetadataCache <TSaga> .ShortName, instance.CorrelationId, TypeMetadataCache <T> .ShortName); } var sagaConsumeContext = new RavenDbSagaConsumeContext <TSaga, T>(session, context, instance); await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false); if (inserted && !sagaConsumeContext.IsCompleted) { await session.StoreAsync(instance, GetSagaId(session, instance)).ConfigureAwait(false); } if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA (Send): New saga state: {@Saga}", instance); } } await session.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error( $"SAGA:{TypeMetadataCache<TSaga>.ShortName} Exception {TypeMetadataCache<T>.ShortName}", ex); } throw; } } }