Ejemplo n.º 1
0
        async Task SendToInstance <T>(SagaQueryConsumeContext <TSaga, T> context, ISagaPolicy <TSaga, T> policy, SagaInstance <TSaga> saga,
                                      IPipe <SagaConsumeContext <TSaga, T> > next)
            where T : class
        {
            await saga.MarkInUse(context.CancellationToken).ConfigureAwait(false);

            try
            {
                if (saga.IsRemoved)
                {
                    return;
                }

                SagaConsumeContext <TSaga, T> sagaConsumeContext = new InMemorySagaConsumeContext <TSaga, T>(context, saga.Instance,
                                                                                                             () => Remove(saga, context.CancellationToken));

                sagaConsumeContext.LogUsed();

                await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);
            }
            finally
            {
                saga.Release();
            }
        }
Ejemplo n.º 2
0
        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;

            await _sagas.MarkInUse(context.CancellationToken).ConfigureAwait(false);

            var needToLeaveSagas = true;

            try
            {
                SagaInstance <TSaga> saga = _sagas[sagaId];
                if (saga != null)
                {
                    await saga.MarkInUse(context.CancellationToken).ConfigureAwait(false);

                    try
                    {
                        _sagas.Release();
                        needToLeaveSagas = false;

                        if (saga.IsRemoved)
                        {
                            saga.Release();
                            saga = null;
                        }
                        else
                        {
                            SagaConsumeContext <TSaga, T> sagaConsumeContext = new InMemorySagaConsumeContext <TSaga, T>(context, saga.Instance,
                                                                                                                         () => Remove(saga, context.CancellationToken));

                            sagaConsumeContext.LogUsed();

                            await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);
                        }
                    }
                    finally
                    {
                        saga?.Release();
                    }
                }

                if (saga == null)
                {
                    var missingSagaPipe = new MissingPipe <T>(this, next, true);

                    await policy.Missing(context, missingSagaPipe).ConfigureAwait(false);

                    _sagas.Release();
                    needToLeaveSagas = false;
                }
            }
            finally
            {
                if (needToLeaveSagas)
                {
                    _sagas.Release();
                }
            }
        }