Пример #1
0
        public InboundConsumer(IBroker broker,
                               IEndpoint endpoint,
                               InboundConnectorSettings settings,
                               Func <IEnumerable <IInboundMessage>, IServiceProvider, Task> messagesHandler,
                               Func <IServiceProvider, Task> commitHandler,
                               Func <IServiceProvider, Task> rollbackHandler,
                               IErrorPolicy errorPolicy,
                               IServiceProvider serviceProvider)
        {
            if (broker == null)
            {
                throw new ArgumentNullException(nameof(broker));
            }

            _endpoint    = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
            _settings    = settings ?? throw new ArgumentNullException(nameof(settings));
            _errorPolicy = errorPolicy;

            _messagesHandler = messagesHandler ?? throw new ArgumentNullException(nameof(messagesHandler));
            _commitHandler   = commitHandler ?? throw new ArgumentNullException(nameof(commitHandler));
            _rollbackHandler = rollbackHandler ?? throw new ArgumentNullException(nameof(rollbackHandler));

            _serviceProvider   = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
            _logger            = serviceProvider.GetRequiredService <ILogger <InboundConsumer> >();
            _errorPolicyHelper = serviceProvider.GetRequiredService <ErrorPolicyHelper>();

            _consumer = broker.GetConsumer(_endpoint);

            Bind();
        }
Пример #2
0
        public InboundConsumer(IBroker broker,
                               IEndpoint endpoint,
                               InboundConnectorSettings settings,
                               Action <IEnumerable <IInboundMessage>, IServiceProvider> messagesHandler,
                               Action <IServiceProvider> commitHandler,
                               Action <IServiceProvider> rollbackHandler,
                               IErrorPolicy errorPolicy,
                               IServiceProvider serviceProvider)
        {
            _endpoint    = endpoint;
            _settings    = settings;
            _errorPolicy = errorPolicy;

            _messagesHandler = messagesHandler;
            _commitHandler   = commitHandler;
            _rollbackHandler = rollbackHandler;

            _serviceProvider         = serviceProvider;
            _logger                  = serviceProvider.GetRequiredService <ILogger <InboundConsumer> >();
            _messageLogger           = serviceProvider.GetRequiredService <MessageLogger>();
            _inboundMessageProcessor = serviceProvider.GetRequiredService <InboundMessageProcessor>();

            _consumer = broker.GetConsumer(_endpoint);

            Bind();
        }
Пример #3
0
        public CommandHandler(ILogPolicy logService, IErrorPolicy errorService, IEventBus eventBus)
        {
            this.LogService   = logService;
            this.ErrorService = errorService;

            this.EventBus = eventBus;
        }
Пример #4
0
        public MessageBatch(IEndpoint endpoint,
                            BatchSettings settings,
                            Action <IEnumerable <IInboundMessage>, IServiceProvider> messagesHandler,
                            Action <IEnumerable <IOffset>, IServiceProvider> commitHandler,
                            Action <IServiceProvider> rollbackHandler,
                            IErrorPolicy errorPolicy,
                            IServiceProvider serviceProvider)
        {
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            _messagesHandler = messagesHandler ?? throw new ArgumentNullException(nameof(messagesHandler));
            _commitHandler   = commitHandler ?? throw new ArgumentNullException(nameof(commitHandler));
            _rollbackHandler = rollbackHandler ?? throw new ArgumentNullException(nameof(rollbackHandler));

            _serviceProvider         = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
            _inboundMessageProcessor = serviceProvider.GetRequiredService <InboundMessageProcessor>();

            _errorPolicy = errorPolicy;
            _settings    = settings;

            _messages = new List <IInboundMessage>(_settings.Size);

            if (_settings.MaxWaitTime < TimeSpan.MaxValue)
            {
                _waitTimer          = new Timer(_settings.MaxWaitTime.TotalMilliseconds);
                _waitTimer.Elapsed += OnWaitTimerElapsed;
            }

            _logger        = serviceProvider.GetRequiredService <ILogger <MessageBatch> >();
            _messageLogger = serviceProvider.GetRequiredService <MessageLogger>();
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceRepository" /> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="logService">The log service.</param>
 /// <param name="sqlQueryProvider">The SQL query provider.</param>
 /// <param name="cacheQueryProvider">The cache query provider.</param>
 public DeviceRepository(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService,
                         SqlQProvider.IDeviceQueryDataProvider sqlQueryProvider,
                         CacheQProvider.IDeviceQueryDataProvider cacheQueryProvider)
     : base(configService, errorService, logService)
 {
     this.SqlQueryProvider   = sqlQueryProvider;
     this.CacheQueryProvider = cacheQueryProvider;
 }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceController" /> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="deviceService">The device service.</param>
 public DeviceController(
     IConfiguration configService,
     ILogPolicy logService,
     IErrorPolicy errorService,
     IDeviceService deviceService)
     : base(configService, logService, errorService)
 {
     this.DeviceService = deviceService;
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceController" /> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="deviceRepository">The device repository.</param>
 public DeviceController(
     IConfiguration configService,
     ILogPolicy logService,
     IErrorPolicy errorService,
     IDeviceRepository deviceRepository)
     : base(configService, logService, errorService)
 {
     this.DeviceRepository = deviceRepository;
 }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceDataProvider"/> class.
        /// </summary>
        /// <param name="configService">The configuration service.</param>
        /// <param name="errorService">The error service.</param>
        /// <param name="logService">The log service.</param>
        /// <param name="cacheService">The cache service.</param>
        public DeviceDataProvider(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService, ICachePolicy cacheService)
            : base(configService, errorService, logService)
        {
            this.CacheService = cacheService;

            this.cacheScope = this.GetType().ToString();

            // Create new scope where to save the entities from this provider
            this.CacheService.CreateScope(this.cacheScope);
        }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceRepository"/> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="logService">The log service.</param>
 /// <param name="deviceIdentityProvider">The device identity provider.</param>
 /// <param name="sqlCommandProvider">The device provider.</param>
 public DeviceRepository(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService,
                         IoTCHubProvider.IDeviceCommandDataProvider deviceIdentityProvider,
                         SqlCProvider.IDeviceCommandDataProvider sqlCommandProvider,
                         CacheCProvider.IDeviceCommandDataProvider cacheCommandProvider,
                         CacheQProvider.IDeviceQueryDataProvider cacheQueryProvider)
     : base(configService, errorService, logService)
 {
     this.IoTDeviceProvider    = deviceIdentityProvider;
     this.SqlDeviceProvider    = sqlCommandProvider;
     this.CacheCommandProvider = cacheCommandProvider;
     this.CacheQueryProvider   = cacheQueryProvider;
 }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SiliconSQLProvider"/> class.
        /// </summary>
        /// <param name="configService">The configuration service.</param>
        /// <param name="errorService">The error service.</param>
        /// <param name="logService">The log service.</param>
        /// <exception cref="IoTException"></exception>
        public SiliconSQLProvider(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService)
        {
            this.ConfigService = configService;
            this.ErrorService  = errorService;
            this.LogService    = logService;

            //set connection string
            if (!string.IsNullOrWhiteSpace(ConfigService[DB_CONNECTION_KEY]))
            {
                this.ConnectionString = ConfigService[DB_CONNECTION_KEY];
            }
            else
            {
                throw new IoTException(ErrorMessages.DataProvider_ConnectionStringKeyMissing, ErrorReasonTypeEnum.Configuration);
            }
        }
Пример #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SiliconIoTHubProvider"/> class.
        /// </summary>
        /// <param name="configService">The configuration service.</param>
        /// <param name="errorService">The error service.</param>
        /// <param name="logService">The log service.</param>
        /// <exception cref="IoTException"></exception>
        public SiliconIoTHubProvider(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService)
        {
            this.ConfigService = configService;
            this.ErrorService  = errorService;
            this.LogService    = logService;

            //set connection string
            if (!string.IsNullOrWhiteSpace(ConfigService[IOTHUB_CONNECTION_KEY]))
            {
                this.ConnectionString = ConfigService[IOTHUB_CONNECTION_KEY];
            }
            else
            {
                throw new IoTException(ErrorMessages.DataProvider_ConnectionStringKeyMissing, ErrorReasonTypeEnum.Configuration);
            }

            this.ProvisioningAttempts = this.ConfigService.GetValue <int>(PROVITIONING_ATTEPTS, defaultProvisioningAttempts);
        }
Пример #12
0
        public async Task TryProcessAsync(
            IEnumerable <IInboundMessage> messages,
            IErrorPolicy errorPolicy,
            Func <IEnumerable <IInboundMessage>, Task> messagesHandler)
        {
            var attempt = GetAttemptNumber(messages);

            while (true)
            {
                var result = await HandleMessages(messages, messagesHandler, errorPolicy, attempt);

                if (result.IsSuccessful || result.Action == ErrorAction.Skip)
                {
                    return;
                }

                attempt++;
            }
        }
Пример #13
0
        public virtual IInboundConnector Bind(IEndpoint endpoint, IErrorPolicy errorPolicy = null, InboundConnectorSettings settings = null)
        {
            settings = settings ?? new InboundConnectorSettings();

            for (int i = 0; i < settings.Consumers; i++)
            {
                _inboundConsumers.Add(new InboundConsumer(
                                          _broker,
                                          endpoint,
                                          settings,
                                          HandleMessages,
                                          Commit,
                                          Rollback,
                                          errorPolicy,
                                          _serviceProvider));
            }

            // TODO: Carefully test with multiple endpoints!
            // TODO: Test if consumer gets properly disposed etc.
            return(this);
        }
Пример #14
0
        private async Task <MessageHandlerResult> HandleMessages(
            IEnumerable <IInboundMessage> messages,
            Func <IEnumerable <IInboundMessage>, Task> messagesHandler,
            IErrorPolicy errorPolicy, int attempt)
        {
            try
            {
                _messageLogger.LogProcessing(_logger, messages);

                await messagesHandler(messages);

                return(MessageHandlerResult.Success);
            }
            catch (Exception ex)
            {
                _messageLogger.LogProcessingError(_logger, messages, ex);

                if (errorPolicy == null)
                {
                    throw;
                }

                UpdateFailedAttemptsHeader(messages, attempt);

                if (!errorPolicy.CanHandle(messages, ex))
                {
                    throw;
                }

                var action = errorPolicy.HandleError(messages, ex);

                if (action == ErrorAction.StopConsuming)
                {
                    throw;
                }

                return(MessageHandlerResult.Error(action));
            }
        }
Пример #15
0
 /// <inheritdoc cref="IConsumerEndpointBuilder{TBuilder}.OnError(IErrorPolicy)" />
 public TBuilder OnError(IErrorPolicy errorPolicy)
 {
     _errorPolicy = Check.NotNull(errorPolicy, nameof(errorPolicy));
     return(This);
 }
Пример #16
0
        public void TryDeserializeAndProcess <TInboundMessage>(TInboundMessage message, IErrorPolicy errorPolicy, Action <TInboundMessage> messageHandler)
            where TInboundMessage : IInboundMessage
        {
            var attempt = message.FailedAttempts + 1;

            while (true)
            {
                var result = HandleMessage(message, messageHandler, errorPolicy, attempt);

                if (result.IsSuccessful || result.Action == ErrorAction.Skip)
                {
                    return;
                }

                attempt++;
            }
        }
Пример #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandEventHandler" /> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 public CommandEventHandler(ILogPolicy logService, IErrorPolicy errorService,
                            ICommandService commandService)
     : this(logService, errorService)
 {
     this.CommandService = commandService;
 }
Пример #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProvisionDeviceCommandHandler" /> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="eventBus">The event bus.</param>
 /// <param name="deviceRepository">The device repository.</param>
 public SubmitReceivedCommandHandler(ILogPolicy logService, IErrorPolicy errorService, IEventBus eventBus)
     : base(logService, errorService, eventBus)
 {
 }
Пример #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SiliconRepository"/> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="logService">The log service.</param>
 public SiliconRepository(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService)
 {
     this.ConfigService = configService;
     this.ErrorService = errorService;
     this.LogService = logService;
 }
Пример #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommonController" /> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 public CommonController(IConfiguration configService, ILogPolicy logService, IErrorPolicy errorService)
 {
     this.ConfigService = configService;
     this.LogService    = logService;
     this.ErrorService  = errorService;
 }
Пример #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventHandler{T}"/> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 public EventHandler(ILogPolicy logService, IErrorPolicy errorService)
 {
     this.LogService   = logService;
     this.ErrorService = errorService;
 }
Пример #22
0
        private MessageHandlerResult HandleMessage <TInboundMessage>(TInboundMessage message, Action <TInboundMessage> messageHandler, IErrorPolicy errorPolicy, int attempt)
            where TInboundMessage : IInboundMessage
        {
            try
            {
                message = (TInboundMessage)DeserializeIfNeeded(message);

                _messageLogger.LogProcessing(_logger, message);

                messageHandler(message);

                return(MessageHandlerResult.Success);
            }
            catch (Exception ex)
            {
                _messageLogger.LogProcessingError(_logger, message, ex);

                if (errorPolicy == null)
                {
                    throw;
                }

                UpdateFailedAttemptsHeader(message, attempt);

                if (!errorPolicy.CanHandle(message, ex))
                {
                    throw;
                }

                var action = errorPolicy.HandleError(message, ex);

                if (action == ErrorAction.StopConsuming)
                {
                    throw;
                }

                return(MessageHandlerResult.Error(action));
            }
        }
Пример #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IoTHubEventHandler" /> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 public IoTHubEventHandler(ILogPolicy logService, IErrorPolicy errorService)
     : base(logService, errorService)
 {
 }
Пример #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SiliconCacheProvider"/> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="logService">The log service.</param>
 public SiliconCacheProvider(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService)
 {
     this.ConfigService = configService;
     this.ErrorService  = errorService;
     this.LogService    = logService;
 }
Пример #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandEventHandler" /> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 public CommandEventHandler(ILogPolicy logService, IErrorPolicy errorService)
     : base(logService, errorService)
 {
 }
Пример #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceDataProvider" /> class.
 /// </summary>
 /// <param name="configService">The configuration service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="logService">The log service.</param>
 public DeviceDataProvider(IConfiguration configService, IErrorPolicy errorService, ILogPolicy logService)
     : base(configService, errorService, logService)
 {
 }
Пример #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProvisionDeviceCommandHandler" /> class.
 /// </summary>
 /// <param name="logService">The log service.</param>
 /// <param name="errorService">The error service.</param>
 /// <param name="eventBus">The event bus.</param>
 /// <param name="deviceRepository">The device repository.</param>
 public ProvisionDeviceCommandHandler(ILogPolicy logService, IErrorPolicy errorService, IEventBus eventBus, IDeviceRepository deviceRepository)
     : base(logService, errorService, eventBus)
 {
     this.DeviceRepository = deviceRepository;
 }