示例#1
0
        /// <summary>
        /// Adds an <see cref="OutboundQueueWorker" /> to publish the queued messages to the configured broker.
        /// </summary>
        /// <param name="outboundQueueConsumerFactory"></param>
        /// <param name="distributedLockSettings">The settings for the locking mechanism.</param>
        /// <param name="interval">The interval between each run (default is 500ms).</param>
        /// <param name="enforceMessageOrder">If set to <c>true</c> the message order will be preserved (no message will be skipped).</param>
        /// <param name="readPackageSize">The number of messages to be loaded from the queue at once.</param>
        // TODO: Test
        public BrokerOptionsBuilder AddOutboundWorker(
            Func <IServiceProvider, IOutboundQueueConsumer> outboundQueueConsumerFactory,
            DistributedLockSettings distributedLockSettings, TimeSpan?interval = null,
            bool enforceMessageOrder = true, int readPackageSize = 100)
        {
            if (outboundQueueConsumerFactory == null)
            {
                throw new ArgumentNullException(nameof(outboundQueueConsumerFactory));
            }
            if (distributedLockSettings == null)
            {
                throw new ArgumentNullException(nameof(distributedLockSettings));
            }

            if (string.IsNullOrEmpty(distributedLockSettings.ResourceName))
            {
                distributedLockSettings.ResourceName = "OutboundQueueWorker";
            }

            AddOutboundWorker(
                interval ?? TimeSpan.FromMilliseconds(500),
                distributedLockSettings,
                enforceMessageOrder,
                readPackageSize);

            Services
            .AddScoped <IOutboundQueueConsumer>(outboundQueueConsumerFactory);

            return(this);
        }
        public async Task Acquire_DefaultLockSettings_LockIsAcquired()
        {
            var distributedLockSettings = new DistributedLockSettings("test.resource");
            var distributedLock         = await new DbDistributedLockManager(_servicesProvider)
                                          .Acquire(distributedLockSettings);

            distributedLock.Should().NotBeNull();
        }
示例#3
0
        public async Task Acquire_DefaultLockSettings_LockIsAcquired()
        {
            var distributedLockSettings = new DistributedLockSettings("test.resource");
            var distributedLock         = await _serviceProvider.GetRequiredService <DbDistributedLockManager>()
                                          .AcquireAsync(distributedLockSettings);

            distributedLock.Should().NotBeNull();
        }
示例#4
0
        public void LogAcquiringLock_Logged()
        {
            var expectedMessage = "Trying to acquire lock lock-name (lock-id)...";
            var lockSettings    = new DistributedLockSettings("lock-name", "lock-id");

            _silverbackLogger.LogAcquiringLock(lockSettings);

            _logger.Received(LogLevel.Information, null, expectedMessage, 21);
        }
示例#5
0
        public void LogLockReleased_Logged()
        {
            var expectedMessage = "Released lock lock-name (lock-id).";
            var lockSettings    = new DistributedLockSettings("lock-name", "lock-id");

            _silverbackLogger.LogLockReleased(lockSettings);

            _logger.Received(LogLevel.Information, null, expectedMessage, 24);
        }
示例#6
0
 public static void LogFailedToReleaseLock(
     this ISilverbackLogger logger,
     DistributedLockSettings lockSettings,
     Exception exception) =>
 FailedToReleaseLock(
     logger.InnerLogger,
     lockSettings.ResourceName,
     lockSettings.UniqueId,
     exception);
示例#7
0
        public void LogFailedToCheckLock_Logged()
        {
            var expectedMessage = "Failed to check lock lock-name (lock-id).";
            var lockSettings    = new DistributedLockSettings("lock-name", "lock-id");

            _silverbackLogger.LogFailedToCheckLock(lockSettings, new TimeoutException());

            _logger.Received(LogLevel.Error, typeof(TimeoutException), expectedMessage, 26);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="OutboxWorkerService" /> class.
 /// </summary>
 /// <param name="interval">
 ///     The interval between each execution.
 /// </param>
 /// <param name="outboxWorker">
 ///     The <see cref="IOutboxWorker" /> implementation.
 /// </param>
 /// <param name="distributedLockSettings">
 ///     Customizes the lock mechanism settings.
 /// </param>
 /// <param name="distributedLockManager">
 ///     The <see cref="IDistributedLockManager" />.
 /// </param>
 /// <param name="logger">
 ///     The <see cref="ISilverbackLogger" />.
 /// </param>
 public OutboxWorkerService(
     TimeSpan interval,
     IOutboxWorker outboxWorker,
     DistributedLockSettings distributedLockSettings,
     IDistributedLockManager distributedLockManager,
     ISilverbackLogger <OutboxWorkerService> logger)
     : base(interval, distributedLockSettings, distributedLockManager, logger)
 {
     _outboxWorker = outboxWorker;
 }
示例#9
0
        public async Task Acquire_DefaultLockSettings_LockIsWrittenToDb()
        {
            var distributedLockSettings = new DistributedLockSettings("test.resource");
            await _serviceProvider.GetRequiredService <DbDistributedLockManager>()
            .AcquireAsync(distributedLockSettings);

            var dbContext = GetDbContext();

            dbContext.Locks.Should().HaveCount(1);
            dbContext.Locks.Single().Name.Should().Be("test.resource");
        }
        public async Task Acquire_DefaultLockSettings_LockIsWrittenToDb()
        {
            var distributedLockSettings = new DistributedLockSettings("test.resource");

            await new DbDistributedLockManager(_servicesProvider)
            .Acquire(distributedLockSettings);

            var dbContext = GetDbContext();

            dbContext.Locks.Count().Should().Be(1);
            dbContext.Locks.Single().Name.Should().Be("test.resource");
        }
示例#11
0
        public async Task Release_LockedResource_LockIsRemoved()
        {
            var settings = new DistributedLockSettings("test.resource", "unique");
            await _serviceProvider.GetRequiredService <DbDistributedLockManager>()
            .AcquireAsync(settings);

            await _serviceProvider.GetRequiredService <DbDistributedLockManager>().ReleaseAsync(settings);

            var dbContext = GetDbContext();

            dbContext.Locks.Should().HaveCount(0);
        }
        public async Task Release_LockedResource_LockIsRemoved()
        {
            var settings = new DistributedLockSettings("test.resource", "unique");

            await new DbDistributedLockManager(_servicesProvider)
            .Acquire(settings);

            await new DbDistributedLockManager(_servicesProvider).Release(settings);

            var dbContext = GetDbContext();

            dbContext.Locks.Count().Should().Be(0);
        }
 public RedisEventSubscriber(
     IRedisClient redis,
     string applicationName,
     string environment,
     IDistributedLockFactory distributedLockFactory,
     DistributedLockSettings lockSettings = null)
 {
     _redisClient            = redis;
     _messageQueueRepository = new MessageQueueRepository(_redisClient, environment, applicationName);
     _applicationName        = applicationName;
     _environment            = environment;
     _distributedLockFactory = distributedLockFactory;
     _lockSettings           = lockSettings ?? new DistributedLockSettings();
 }
示例#14
0
        internal BrokerOptionsBuilder AddOutboundWorker(TimeSpan interval, DistributedLockSettings distributedLockSettings, bool enforceMessageOrder, int readPackageSize)
        {
            Services
            .AddSingleton <IOutboundQueueWorker>(s => new OutboundQueueWorker(
                                                     s.GetRequiredService <IServiceProvider>(),
                                                     s.GetRequiredService <IBroker>(),
                                                     s.GetRequiredService <ILogger <OutboundQueueWorker> >(),
                                                     s.GetRequiredService <MessageLogger>(),
                                                     enforceMessageOrder, readPackageSize))
            .AddSingleton <IHostedService>(s => new OutboundQueueWorkerService(
                                               interval,
                                               s.GetRequiredService <IOutboundQueueWorker>(),
                                               distributedLockSettings,
                                               s.GetService <IDistributedLockManager>() ?? new NullLockManager(),
                                               s.GetRequiredService <ILogger <OutboundQueueWorkerService> >()));

            return(this);
        }
        /// <summary>
        /// Adds an <see cref="OutboundQueueWorker" /> to publish the queued messages to the configured broker.
        /// </summary>
        /// <param name="distributedLockSettings">The settings for the locking mechanism.</param>
        /// <param name="interval">The interval between each run (default is 500ms).</param>
        /// <param name="enforceMessageOrder">if set to <c>true</c> the message order will be preserved (no message will be skipped).</param>
        /// <param name="readPackageSize">The number of messages to be loaded from the queue at once.</param>
        /// <param name="removeProduced">if set to <c>true</c> the messages will be removed from the database immediately after being produced.</param>
        public BrokerOptionsBuilder AddDbOutboundWorker(DistributedLockSettings distributedLockSettings,
                                                        TimeSpan?interval   = null, bool enforceMessageOrder = true, int readPackageSize = 100,
                                                        bool removeProduced = true)
        {
            if (distributedLockSettings == null)
            {
                throw new ArgumentNullException(nameof(distributedLockSettings));
            }

            if (string.IsNullOrEmpty(distributedLockSettings.ResourceName))
            {
                distributedLockSettings.ResourceName = "DbOutboundQueueWorker";
            }

            AddOutboundWorker(
                s => new DbOutboundQueueConsumer(s.GetRequiredService <IDbContext>(), removeProduced),
                distributedLockSettings, interval,
                enforceMessageOrder, readPackageSize);

            return(this);
        }
示例#16
0
        /// <summary>
        /// Adds an <see cref="OutboundQueueWorker" /> to publish the queued messages to the configured broker.
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="distributedLockSettings">The settings for the locking mechanism.</param>
        /// <param name="interval">The interval between each run (default is 500ms).</param>
        /// <param name="enforceMessageOrder">if set to <c>true</c> the message order will be preserved (no message will be skipped).</param>
        /// <param name="readPackageSize">The number of messages to be loaded from the queue at once.</param>
        /// <param name="removeProduced">if set to <c>true</c> the messages will be removed from the database immediately after being produced.</param>
        public static BrokerOptionsBuilder AddDbOutboundWorker <TDbContext>(this BrokerOptionsBuilder builder,
                                                                            DistributedLockSettings distributedLockSettings, TimeSpan?interval = null,
                                                                            bool enforceMessageOrder = true, int readPackageSize = 100, bool removeProduced = true)
            where TDbContext : DbContext
        {
            if (distributedLockSettings == null)
            {
                throw new ArgumentNullException(nameof(distributedLockSettings));
            }

            if (string.IsNullOrEmpty(distributedLockSettings.ResourceName))
            {
                distributedLockSettings.ResourceName = $"OutboundQueueWorker[{typeof(TDbContext).Name}]";
            }

            builder.AddOutboundWorker(
                s => new DbContextOutboundQueueConsumer(s.GetRequiredService <TDbContext>(), removeProduced),
                distributedLockSettings, interval,
                enforceMessageOrder, readPackageSize);

            return(builder);
        }
示例#17
0
 public static void LogAcquiringLock(
     this ISilverbackLogger logger,
     DistributedLockSettings lockSettings) =>
 AcquiringLock(logger.InnerLogger, lockSettings.ResourceName, lockSettings.UniqueId, null);
示例#18
0
 public Task <DistributedLock> Acquire(DistributedLockSettings settings, CancellationToken cancellationToken = default) =>
 Acquire(settings.ResourceName, settings.AcquireTimeout, settings.AcquireRetryInterval);
示例#19
0
 public static void LogLockReleased(
     this ISilverbackLogger logger,
     DistributedLockSettings lockSettings) =>
 LockReleased(logger.InnerLogger, lockSettings.ResourceName, lockSettings.UniqueId, null);
示例#20
0
 public DistributedLockException(IRedLock redLock, DistributedLockSettings settings)
     : base($"Failed to get lock for Aggregate '{redLock.Resource}' within {settings.WaitSeconds} seconds with status: {redLock.Status}")
 {
 }
 public OutboundQueueWorkerService(TimeSpan interval, IOutboundQueueWorker outboundQueueWorker, DistributedLockSettings distributedLockSettings,
                                   IDistributedLockManager distributedLockManager, ILogger <OutboundQueueWorkerService> logger)
     : base(interval, distributedLockSettings, distributedLockManager, logger)
 {
     _outboundQueueWorker = outboundQueueWorker;
     _interval            = interval;
 }