コード例 #1
0
ファイル: CommandScheduler.cs プロジェクト: yeowgit/Its.Cqrs
        /// <summary>
        /// Allows awaiting delivery of all commands that are currently due on the command scheduler.
        /// </summary>
        /// <param name="scheduler">The command scheduler.</param>
        /// <param name="clockName">The name of the clock on which the commands are scheduled.</param>
        /// <returns></returns>
        public static async Task Done(
            this ISchedulerClockTrigger scheduler,
            string clockName = null)
        {
            clockName = clockName ?? SqlCommandScheduler.DefaultClockName;

            for (var i = 0; i < 10; i++)
            {
                using (var db = new CommandSchedulerDbContext())
                {
                    var due = db.ScheduledCommands
                              .Due()
                              .Where(c => c.Clock.Name == clockName);

                    if (!await due.AnyAsync())
                    {
                        return;
                    }

                    var commands = await due.ToArrayAsync();

                    foreach (var scheduledCommand in commands)
                    {
                        await scheduler.Trigger(
                            scheduledCommand,
                            new SchedulerAdvancedResult(),
                            db);
                    }

                    await Task.Delay(400);
                }
            }
        }
コード例 #2
0
            /// <summary>
            /// Raises an event that occurs when a message has been brokered.
            /// </summary>
            /// <returns>
            /// The task object representing the asynchronous operation.
            /// </returns>
            /// <param name="session">The message session.</param>
            /// <param name="message">The brokered message.</param>
            public async Task OnMessageAsync(MessageSession session, BrokeredMessage message)
            {
                var json = message.GetBody <string>();

                var @event = json.FromJsonTo <ServiceBusScheduledCommand>();

                @event.BrokeredMessage = message;

                onMessage(@event);

                var result = await clockTrigger.Trigger(commands => commands.Due(@event.DueTime)
                                                        .Where(c => c.AggregateId == @event.AggregateId));

                if (!result.FailedCommands.Any())
                {
                    if (result.SuccessfulCommands.Any())
                    {
                        Debug.WriteLine("ServiceBusCommandQueueReceiver: completing on success: " + @event.AggregateId);
                        await message.CompleteAsync();

                        return;
                    }

                    using (var db = createCommandSchedulerDbContext())
                    {
                        // if the command was already applied, we can complete the message. its job is done.
                        if (db.ScheduledCommands
                            .Where(cmd => cmd.AppliedTime != null || cmd.FinalAttemptTime != null)
                            .Where(cmd => cmd.SequenceNumber == @event.SequenceNumber)
                            .Any(cmd => cmd.AggregateId == @event.AggregateId))
                        {
                            Debug.WriteLine("ServiceBusCommandQueueReceiver: completing because command was previously applied: " + @event.AggregateId);

                            await message.CompleteAsync();
                        }
                    }
                }
            }