コード例 #1
0
        public void CalculateNextRunTimeTest()
        {
            var task = new ScheduledTask {
                StartDate      = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0).ToUniversalTime(),
                RepeatInterval = TimeSpan.FromMinutes(60),
            };

            task.NextRunTime = TaskCalculator.CalculateNextRunTime(task);

            var expectedTime = task.StartDate.AddHours(1);

            Assert.That(task.NextRunTime, Is.EqualTo(expectedTime));
        }
コード例 #2
0
        public void CalculateNextRetryTime()
        {
            var task = new ScheduledTask
            {
                StartDate      = new DateTime(2000, 5, 16, DateTime.Now.Hour, 0, 0).ToUniversalTime(),
                RepeatInterval = TimeSpan.FromMinutes(60),
                RetryInterval  = TimeSpan.FromMinutes(10),
            };

            task.NextRunTime = TaskCalculator.CalculateNextRetryTime(task);

            var expectedTime = DateTime.UtcNow.AddMinutes(10);

            Assert.That(task.NextRunTime, Is.InRange(expectedTime.AddMinutes(-1), expectedTime.AddMinutes(1)));
        }
コード例 #3
0
        public void CalculateRandomDelay()
        {
            var task = new ScheduledTask
            {
                StartDate      = new DateTime(2000, 5, 16, DateTime.Now.Hour, 30, 22).ToUniversalTime(),
                RepeatInterval = TimeSpan.FromMinutes(15),
                RetryInterval  = TimeSpan.FromMinutes(10),
                RandomDelay    = TimeSpan.FromMinutes(10),
            };

            for (var i = 0; i < 100; i++)
            {
                var delay = TaskCalculator.CalculateRandomDelay(task);
                System.Threading.Thread.Sleep(1);
                Console.WriteLine(delay);
                Assert.That(delay, Is.InRange(-task.RandomDelay, task.RandomDelay));
            }
        }
コード例 #4
0
ファイル: TaskExecutor.cs プロジェクト: awhatley/pendulum
        private void RecordResult(ScheduledTask task, TaskResult result, string message, Exception exception = null)
        {
            if (exception != null)
            {
                message += Environment.NewLine + Environment.NewLine + exception.Message +
                           Environment.NewLine + Environment.NewLine + exception.StackTrace;
            }

            switch (result)
            {
            case TaskResult.Success:
                task.RetryCount  = 0;
                task.Status      = TaskStatus.Ready;
                task.NextRunTime = TaskCalculator.CalculateNextRunTime(task);
                break;

            case TaskResult.RecoverableFault:
                if (task.RetryCount >= task.MaxRetryCount)
                {
                    goto case TaskResult.FatalError;
                }

                task.RetryCount += 1;
                task.Status      = TaskStatus.Retrying;
                task.NextRunTime = TaskCalculator.CalculateNextRetryTime(task);
                break;

            case TaskResult.FatalError:
                task.IsEnabled   = false;
                task.RetryCount += 1;
                task.Status      = TaskStatus.Faulted;
                break;
            }

            task.History.Add(new ScheduledTaskExecution {
                Message = message,
                Result  = result,
                RunTime = DateTime.UtcNow,
            });

            _repository.Save(task);
        }
コード例 #5
0
        public void CalculateNextRunTimeAfterRetry()
        {
            var task = new ScheduledTask
            {
                StartDate      = new DateTime(2000, 5, 16, DateTime.Now.Hour, 0, 0).ToUniversalTime(),
                RepeatInterval = TimeSpan.FromMinutes(60),
                RetryInterval  = TimeSpan.FromMinutes(10),
            };

            task.NextRunTime = TaskCalculator.CalculateNextRetryTime(task);
            task.NextRunTime = TaskCalculator.CalculateNextRunTime(task);

            var expectedTime = new DateTime(
                DateTime.Now.Year,
                DateTime.Now.Month,
                DateTime.Now.Day,
                DateTime.Now.Hour + 1, 0, 0).ToUniversalTime();

            Assert.That(task.NextRunTime, Is.EqualTo(expectedTime));
        }
コード例 #6
0
        public void CalculateNextRunTimeTest_RandomDelay()
        {
            var task = new ScheduledTask
            {
                StartDate      = new DateTime(2000, 5, 16, DateTime.Now.Hour, 0, 0).ToUniversalTime(),
                RepeatInterval = TimeSpan.FromMinutes(60),
                RandomDelay    = TimeSpan.FromMinutes(10),
            };

            task.NextRunTime = TaskCalculator.CalculateNextRunTime(task);

            var expectedTime = new DateTime(
                DateTime.Now.Year,
                DateTime.Now.Month,
                DateTime.Now.Day,
                DateTime.Now.Hour + 1, 0, 0).ToUniversalTime();

            Console.WriteLine(task.NextRunTime.ToLocalTime());

            Assert.That(task.NextRunTime, Is.InRange(expectedTime.AddMinutes(-10), expectedTime.AddMinutes(10)));
        }