public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IContainer>(out var existingContainer))
            {
                existingContainer.Inject <ConsumeContext>(context);

                var activity = existingContainer
                               .With(context.Arguments)
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var nestedContainer = _container?.CreateNestedContainer(context) ?? _context?.CreateNestedContainer(context);

            try
            {
                var activity = nestedContainer
                               .With(context.Arguments)
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);
                activityContext.UpdatePayload(nestedContainer);

                return(new CreatedExecuteActivityScopeContext <IContainer, TActivity, TArguments>(nestedContainer, activityContext));
            }
            catch
            {
                nestedContainer.Dispose();
                throw;
            }
        }
Пример #2
0
        public async Task Send(ExecuteActivityContext <TArguments> context, IPipe <ExecuteActivityContext <TArguments> > next)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Executing: {0}", context.TrackingNumber);
            }

            try
            {
                try
                {
                    ExecutionResult result = await context.Activity.Execute(context).ConfigureAwait(false);

                    await result.Evaluate().ConfigureAwait(false);

                    await next.Send(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    ExecutionResult result = context.Faulted(ex);

                    await result.Evaluate().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _log.Error($"The activity {TypeMetadataCache.GetShortName(context.Activity.GetType())} threw an exception", ex);

                throw;
            }
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IKernel>(out var kernel))
            {
                kernel.UpdateScope(context);

                var activity = kernel.Resolve <TActivity>(new Arguments().AddTyped(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext, ReleaseComponent));
            }

            var scope = _kernel.CreateNewOrUseExistingMessageScope();

            try
            {
                ExecuteContext <TArguments> scopeContext = new ExecuteContextScope <TArguments>(context, _kernel);

                _kernel.UpdateScope(scopeContext);

                var activity = _kernel.Resolve <TActivity>(new Arguments().AddTyped(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = scopeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <IDisposable, TActivity, TArguments>(scope, activityContext, ReleaseComponent));
            }
            catch
            {
                scope.Dispose();
                throw;
            }
        }
Пример #4
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(lifetimeScope);

                return(new CreatedExecuteActivityScopeContext <ILifetimeScope, TActivity, TArguments>(lifetimeScope, activityContext));
            }
            catch
            {
                lifetimeScope.Dispose();
                throw;
            }
        }
Пример #5
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IUnityContainer>(out var existingContainer))
            {
                var activity = existingContainer.Resolve <TActivity>(new DependencyOverride(typeof(TArguments), new InjectionParameter(context.Arguments)));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = _container.CreateChildContainer();

            try
            {
                var activity = scope.Resolve <TActivity>(new DependencyOverride(typeof(TArguments), new InjectionParameter(context.Arguments)));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                var contextScope = scope;
                activityContext.GetOrAddPayload(() => contextScope);

                return(new CreatedExecuteActivityScopeContext <IUnityContainer, TActivity, TArguments>(contextScope, activityContext));
            }
            catch
            {
                scope.Dispose();

                throw;
            }
        }
        public async Task Execute(ExecuteContext <TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            TActivity activity = null;

            try
            {
                activity = _executeFactory(context.Arguments);

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                await next.Send(activityContext).ConfigureAwait(false);
            }
            finally
            {
                switch (activity)
                {
                case IAsyncDisposable asyncDisposable:
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);

                    break;

                case IDisposable disposable:
                    disposable.Dispose();
                    break;
                }
            }
        }
        public Task ExecuteFault <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context, Exception exception)
            where TActivity : class, IExecuteActivity <TArguments>
            where TArguments : class
        {
            PrometheusMetrics.MeasureExecute(context, exception);

            return(Task.CompletedTask);
        }
        public Task PostExecute <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context)
            where TActivity : class, IExecuteActivity <TArguments>
            where TArguments : class
        {
            PrometheusMetrics.MeasureExecute(context);

            return(Task.CompletedTask);
        }
Пример #9
0
        public Task Send(ExecuteActivityContext <TArguments> context)
        {
            if (context is ExecuteActivityContext <TActivity, TArguments> consumerContext)
            {
                return(_output.Send(consumerContext));
            }

            throw new ArgumentException($"THe context could not be retrieved: {TypeMetadataCache<TActivity>.ShortName}", nameof(context));
        }
        public async ValueTask <IExecuteActivityScopeContext <TActivity, TArguments> > GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.SetCurrentConsumeContext(context);

                var activity = existingServiceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.CreateScope();

            try
            {
                ExecuteContext <TArguments> scopeContext = new ExecuteContextScope <TArguments>(context, serviceScope, serviceScope.ServiceProvider);

                serviceScope.SetCurrentConsumeContext(scopeContext);

                var activity = serviceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = scopeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <IServiceScope, TActivity, TArguments>(serviceScope, activityContext));
            }
            catch
            {
                if (serviceScope is IAsyncDisposable asyncDisposable)
                {
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);
                }
                else
                {
                    serviceScope.Dispose();
                }

                throw;
            }
        }
Пример #11
0
        public async Task Send(ExecuteActivityContext <TActivity, TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            context.Result = await context.Activity.Execute(context).ConfigureAwait(false);

            var result = context.Result ?? context.Faulted(new ActivityExecutionException("The activity execute did not return a result"));

            if (result.IsFaulted(out var exception))
            {
                throw new AggregateException(exception);
            }

            await next.Send(context).ConfigureAwait(false);
        }
Пример #12
0
        public static void MeasureExecute <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context, Exception exception = default)
            where TActivity : class, IExecuteActivity <TArguments>
            where TArguments : class
        {
            var argumentType = GetArgumentTypeLabel <TArguments>();

            _executeTotal.Labels(_serviceLabel, context.ActivityName, argumentType).Inc();
            _executeDuration.Labels(_serviceLabel, context.ActivityName, argumentType).Observe(context.Elapsed.TotalSeconds);

            if (exception != null)
            {
                var exceptionType = exception.GetType().Name;
                _executeFaultTotal.Labels(_serviceLabel, context.ActivityName, argumentType, exceptionType).Inc();
            }
        }
Пример #13
0
 public async Task Send(ExecuteActivityContext <TActivity, TArgument> context, IPipe <ExecuteActivityContext <TActivity, TArgument> > next)
 {
     try
     {
         await next.Send(context).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         if (!context.TryGetPayload(out IErrorTransport transport))
         {
             throw new TransportException(context.ReceiveContext.InputAddress, $"The {nameof(IErrorTransport)} was not available on the {nameof(ReceiveContext)}.");
         }
         var exceptionReceiveContext = new RescueExceptionReceiveContext(context.ReceiveContext, ex);
         await transport.Send(exceptionReceiveContext);
     }
 }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.UpdateScope(context);

                var activity = existingServiceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            try
            {
                serviceScope.UpdateScope(context);

                var activity = serviceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(serviceScope);

                return(new CreatedExecuteActivityScopeContext <IServiceScope, TActivity, TArguments>(serviceScope, activityContext));
            }
            catch
            {
                serviceScope.Dispose();

                throw;
            }
        }
        public async Task Send(ExecuteActivityContext <TActivity, TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            try
            {
                if (_observers.Count > 0)
                {
                    await _observers.PreExecute(context).ConfigureAwait(false);
                }

                var result = context.Result = await context.Activity.Execute(context).ConfigureAwait(false)
                                              ?? context.Faulted(new ActivityExecutionException("The activity execute did not return a result"));

                if (result.IsFaulted(out var exception))
                {
                    exception.Rethrow();
                }

                await next.Send(context).ConfigureAwait(false);

                if (_observers.Count > 0)
                {
                    await _observers.PostExecute(context).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                if (context.Result == null || !context.Result.IsFaulted(out var faultException) || faultException != exception)
                {
                    context.Result = context.Faulted(exception);
                }

                if (_observers.Count > 0)
                {
                    await _observers.ExecuteFault(context, exception).ConfigureAwait(false);
                }

                throw;
            }
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <Scope>(out var existingScope))
            {
                existingScope.UpdateScope(context);

                var activity = existingScope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                scope.UpdateScope(context);

                var activity = scope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(scope);

                return(new CreatedExecuteActivityScopeContext <Scope, TActivity, TArguments>(scope, activityContext));
            }
            catch
            {
                scope.Dispose();

                throw;
            }
        }
        public async ValueTask <IExecuteActivityScopeContext <TActivity, TArguments> > GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <Scope>(out var existingScope))
            {
                existingScope.UpdateScope(context);

                var activity = existingScope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                ExecuteContext <TArguments> scopeContext = new ExecuteContextScope <TArguments>(context, scope);

                scope.UpdateScope(scopeContext);

                var activity = scope.Container.GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = scopeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <Scope, TActivity, TArguments>(scope, activityContext));
            }
            catch
            {
                await scope.DisposeScopeAsync().ConfigureAwait(false);

                throw;
            }
        }
Пример #18
0
 public Task Send(ExecuteActivityContext <TArguments> context)
 {
     return(_output.Send(context.PopContext <TActivity>()));
 }
Пример #19
0
 public Task PostExecute <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context)
     where TActivity : class, IExecuteActivity <TArguments>
     where TArguments : class
 {
     return(ForEachAsync(x => x.PostExecute(context)));
 }
Пример #20
0
 public Task ExecuteFault <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context, Exception exception)
     where TActivity : class, IExecuteActivity <TArguments>
     where TArguments : class
 {
     return(ForEachAsync(x => x.ExecuteFault(context, exception)));
 }
 public ExistingExecuteActivityScopeContext(ExecuteActivityContext <TActivity, TArguments> context)
 {
     Context = context;
 }
Пример #22
0
 public ExistingExecuteActivityScopeContext(ExecuteActivityContext <TActivity, TArguments> context, Action <TActivity> disposeCallback = null)
 {
     _disposeCallback = disposeCallback;
     Context          = context;
 }
        public async Task Send(ExecuteActivityContext <TActivity, TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            using var inProgress = PrometheusMetrics.TrackExecuteActivityInProgress(context);

            await next.Send(context).ConfigureAwait(false);
        }
 public Task PreExecute <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context)
     where TActivity : class, IExecuteActivity <TArguments>
     where TArguments : class
 {
     return(Task.CompletedTask);
 }
 public CreatedExecuteActivityScopeContext(TScope scope, ExecuteActivityContext <TActivity, TArguments> context)
 {
     _scope  = scope;
     Context = context;
 }
Пример #26
0
 public CreatedExecuteActivityScopeContext(TScope scope, ExecuteActivityContext <TActivity, TArguments> context, Action <TActivity> disposeCallback = null)
 {
     _scope           = scope;
     _disposeCallback = disposeCallback;
     Context          = context;
 }
Пример #27
0
        public static IDisposable TrackExecuteActivityInProgress <TActivity, TArguments>(ExecuteActivityContext <TActivity, TArguments> context)
            where TActivity : class, IExecuteActivity <TArguments>
            where TArguments : class
        {
            var argumentType = GetArgumentTypeLabel <TArguments>();

            return(_executeInProgress.Labels(_serviceLabel, context.ActivityName, argumentType).TrackInProgress());
        }
Пример #28
0
        public Task Send(ExecuteActivityContext <TActivity, TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            var mergePipe = new ExecuteActivityMergePipe <TActivity, TArguments>(next);

            return(_next.Send(context, mergePipe));
        }