public void Cleanup_does_not_delete_commands_that_have_not_been_applied()
        {
            // arrange: set up some test data
            WriteScheduledCommand(
                finalAttemptTime: null,
                appliedTime: null,
                dueTime: Clock.Now().Subtract(1.Days()));

            // act
            var migration = new CommandSchedulerCleanupMigration(
                frequencyInDays: 1,
                completedCommandsOlderThan: 7.Days());
            Run(migration);

            // assert
            ScheduledCommandExists().Should().BeTrue();
        }
        public void Cleanup_deletes_commands_that_were_applied_before_the_cutoff()
        {
            // arrange: set up some test data
            WriteScheduledCommand(
                finalAttemptTime: null,
                appliedTime: Clock.Now().Subtract(10.Days()),
                dueTime: Clock.Now().Subtract(100.Days()));

            // act
            var migration = new CommandSchedulerCleanupMigration(
                frequencyInDays: 1,
                completedCommandsOlderThan: 7.Days());
            Run(migration);

            // assert
            ScheduledCommandExists().Should().BeFalse();
        }
        public CommandSchedulerConfiguration CleanUp(
            int frequencyInDays,
            TimeSpan completedCommandsOlderThan)
        {
            var migration = new CommandSchedulerCleanupMigration(
                frequencyInDays,
                completedCommandsOlderThan);

            Action<Configuration> action = configuration =>
            {
                configuration.QueueBackgroundWork(_ =>
                {
                    using (var db = configuration.Container
                                                 .Resolve<CommandSchedulerDbContext>())
                    {
                        db.EnsureDatabaseIsUpToDate(migration);
                    }
                });
            };

            configureActions.Add(action);

            return this;
        }
        /// <summary>
        /// Deletes successfully delivered scheduled commands from SQL storage on a periodic basis in order to keep database size from growing endlessly.
        /// </summary>
        /// <param name="frequencyInDays">The frequency, in days, at which the cleanup should be performed.</param>
        /// <param name="completedCommandsOlderThan">The age after which completed scheduled commands should be deleted from storage.</param>
        /// <returns></returns>
        public CommandSchedulerConfiguration CleanUp(
            int frequencyInDays,
            TimeSpan completedCommandsOlderThan)
        {
            var migration = new CommandSchedulerCleanupMigration(
                frequencyInDays,
                completedCommandsOlderThan);

            Action <Configuration> action = configuration =>
            {
                configuration.QueueBackgroundWork(_ =>
                {
                    using (var db = configuration.Container
                                    .Resolve <CommandSchedulerDbContext>())
                    {
                        db.EnsureDatabaseIsUpToDate(migration);
                    }
                });
            };

            configureActions.Add(action);

            return(this);
        }