Пример #1
0
 private static ScheduledCommand CreateStoredScheduledCommand <TAggregate>(
     IScheduledCommand <TAggregate> scheduledCommand,
     DateTimeOffset domainTime,
     Clock schedulerClock)
     where TAggregate : class =>
 new ScheduledCommand
 {
     AggregateId    = ScheduledCommand <TAggregate> .TargetGuid(scheduledCommand),
     SequenceNumber = scheduledCommand
                      .IfTypeIs <IEvent>()
                      .Then(e => e.SequenceNumber)
                      .Else(() => - DateTimeOffset.UtcNow.Ticks),
     AggregateType     = Command.TargetNameFor(scheduledCommand.Command.GetType()),
     SerializedCommand = scheduledCommand.ToJson(),
     CreatedTime       = domainTime,
     DueTime           = scheduledCommand.DueTime ?? schedulerClock.Now(),
     Clock             = schedulerClock
 };
Пример #2
0
        internal static async Task UpdateScheduledCommand <TAggregate>(
            IScheduledCommand <TAggregate> scheduledCommand,
            Func <CommandSchedulerDbContext> createDbContext) where TAggregate : class
        {
            using (var db = createDbContext())
            {
                var scheduledCommandGuid = ScheduledCommand <TAggregate> .TargetGuid(scheduledCommand);

                var storedCommand = await GetStoredScheduledCommand(
                    scheduledCommand,
                    db,
                    scheduledCommandGuid);

                if (storedCommand == null)
                {
                    if (!scheduledCommand.Command.RequiresDurableScheduling())
                    {
                        return;
                    }

                    throw new InvalidOperationException("Scheduled command not found");
                }

                storedCommand.Attempts++;

                var result = scheduledCommand.Result;

                if (result is CommandSucceeded)
                {
                    storedCommand.AppliedTime = Domain.Clock.Now();
                }
                else
                {
                    RescheduleIfAppropriate(storedCommand, result, db);
                }

                await db.SaveChangesAsync();
            }
        }