public SqlCommandSchedulerBinder(SqlCommandScheduler <TAggregate> scheduler)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }

            this.scheduler = scheduler;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceBusCommandQueueReceiver"/> class.
        /// </summary>
        /// <param name="settings">The service bus settings.</param>
        /// <param name="scheduler">The command scheduler.</param>
        /// <exception cref="System.ArgumentNullException">
        /// settings
        /// or
        /// scheduler
        /// </exception>
        public ServiceBusCommandQueueReceiver(ServiceBusSettings settings, SqlCommandScheduler scheduler)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }
            this.settings = settings;
            this.scheduler = scheduler;

#if DEBUG
            exceptionSubject
                .Where(ex => !(ex is OperationCanceledException))
                .Subscribe(ex => Debug.WriteLine("ServiceBusCommandQueueReceiver error: " + ex));
#endif
        }
Exemple #3
0
        private static async Task <string> ClockNameForEvent(
            SqlCommandScheduler <TAggregate> sqlCommandScheduler,
            IScheduledCommand <TAggregate> scheduledCommandEvent,
            CommandSchedulerDbContext db)
        {
            var clockName = sqlCommandScheduler.GetClockName(scheduledCommandEvent);

            if (clockName == null)
            {
                var lookupValue = sqlCommandScheduler.GetClockLookupKey(scheduledCommandEvent);
                clockName = (await db.ClockMappings
                             .Include(m => m.Clock)
                             .SingleOrDefaultAsync(c => c.Value == lookupValue))
                            .IfNotNull()
                            .Then(c => c.Clock.Name)
                            .Else(() => SqlCommandScheduler.DefaultClockName);
            }

            return(clockName);
        }
        public override void SetUp()
        {
            base.SetUp();

            using (VirtualClock.Start(DateTimeOffset.Now.AddMonths(1)))
            {
                disposables = new CompositeDisposable();
                Settings.Sources = new ISettingsSource[] { new ConfigDirectorySettings(@"c:\dev\.config") }.Concat(Settings.Sources);

                serviceBusSettings = Settings.Get<ServiceBusSettings>();
                serviceBusSettings.NamePrefix = "itscqrstests";
                serviceBusSettings.ConfigureQueue = q =>
                {
                    q.AutoDeleteOnIdle = TimeSpan.FromMinutes(15);
                };

                bus = new FakeEventBus();
                orderRepository = new SqlEventSourcedRepository<Order>(bus);

                var configuration = new Configuration()
                    .UseSqlEventStore(() => new EventStoreDbContext())
                    .UseEventBus(bus)
                    .UseSqlCommandScheduling()
                    .UseDependency<IEventSourcedRepository<Order>>(t => orderRepository);

                var clockName = Any.Paragraph(4);
                scheduler = new SqlCommandScheduler(configuration) { GetClockName = @event => clockName };

                queueSender = new ServiceBusCommandQueueSender(serviceBusSettings)
                {
                    MessageDeliveryOffsetFromCommandDueTime = TimeSpan.FromSeconds(30)
                };

                disposables.Add(scheduler.Activity.Subscribe(s => Console.WriteLine("SqlCommandScheduler: " + s.ToJson())));
                disposables.Add(queueSender.Messages.Subscribe(s => Console.WriteLine("ServiceBusCommandQueueSender: " + s.ToJson())));
                disposables.Add(bus.Subscribe(scheduler));
                disposables.Add(configuration);
                disposables.Add(ConfigurationContext.Establish(configuration));
            }
        }
        public static Configuration UseSqlCommandScheduling(
            this Configuration configuration,
            Action<ReadModelCatchup<CommandSchedulerDbContext>> configureCatchup = null)
        {
            var container = configuration.Container;

            container.RegisterDefaultClockName();

            var scheduler = new SqlCommandScheduler(
                configuration,
                container.Resolve<Func<CommandSchedulerDbContext>>(),
                container.Resolve<GetClockName>());

            if (container.All(r => r.Key != typeof (SqlCommandScheduler)))
            {
                container.Register(c => scheduler)
                         .Register<ISchedulerClockTrigger>(c => scheduler)
                         .Register<ISchedulerClockRepository>(c => scheduler);
            }

            var subscription = container.Resolve<IEventBus>().Subscribe(scheduler);
            configuration.RegisterForDisposal(subscription);
            container.RegisterSingle(c => scheduler);

            if (configureCatchup != null)
            {
                var catchup = new ReadModelCatchup<CommandSchedulerDbContext>(scheduler)
                {
                    CreateReadModelDbContext = scheduler.CreateCommandSchedulerDbContext
                };
                configureCatchup(catchup);
                catchup.PollEventStore();
                container.RegisterSingle(c => catchup);
                configuration.RegisterForDisposal(catchup);
            }

            configuration.IsUsingCommandSchedulerPipeline(false);

            return configuration;
        }
 public SessionHandler(
     Action<IScheduledCommand> onMessage,
     Action<Exception> onError,
     SqlCommandScheduler scheduler)
 {
     this.onMessage = onMessage;
     this.onError = onError;
     this.scheduler = scheduler;
 }
 protected override void Configure(Configuration configuration)
 {
     configuration.UseSqlCommandScheduling();
     sqlCommandScheduler = configuration.SqlCommandScheduler();
     sqlCommandScheduler.GetClockName = e => clockName;
     clockTrigger = sqlCommandScheduler;
     clockRepository = sqlCommandScheduler;
     configuration.TriggerSqlCommandSchedulerWithVirtualClock();
 }
        public async Task A_command_is_not_marked_as_applied_if_no_handler_is_registered()
        {
            // arrange
            var order = CommandSchedulingTests_EventSourced.CreateOrder();
            order.Apply(
                new ChargeCreditCardOn
                {
                    Amount = 10,
                    ChargeDate = Clock.Now().AddDays(10)
                });
            await orderRepository.Save(order);

            // act
            var schedulerWithNoHandlers = new SqlCommandScheduler(
                new Configuration().UseSqlEventStore());
            await schedulerWithNoHandlers.AdvanceClock(clockName, @by: TimeSpan.FromDays(20));

            // assert
            using (var db = new CommandSchedulerDbContext())
            {
                db.ScheduledCommands.Single(c => c.AggregateId == order.Id)
                  .AppliedTime
                  .Should()
                  .NotHaveValue();
            }
        }