Exemple #1
0
        public async Task <IActionResult> CreateMinutelyBackgroundTask([FromBody] CreateBackgroundTaskModel model,
                                                                       [FromRoute] int minutes = 0, [FromRoute] int atSecond = 0)
        {
            model.Expression = CronTemplates.Minutely(minutes, atSecond);

            return(await CreateBackgroundTask(model));
        }
Exemple #2
0
        public async Task <IActionResult> CreateSecondlyBackgroundTask([FromBody] CreateBackgroundTaskModel model,
                                                                       [FromRoute] int seconds = 0)
        {
            model.Expression = CronTemplates.Secondly(seconds);

            return(await CreateBackgroundTask(model));
        }
Exemple #3
0
        public async Task <IActionResult> CreateDailyBackgroundTask([FromBody] CreateBackgroundTaskModel model,
                                                                    [FromRoute] int atHour = 0, [FromRoute] int atMinute = 0, [FromRoute] int atSecond = 0)
        {
            model.Expression = CronTemplates.Daily(atHour, atMinute, atSecond);

            return(await CreateBackgroundTask(model));
        }
Exemple #4
0
        public void Queues_for_delayed_execution_and_continuous_repeating_task()
        {
            var host = CreateBackgroundTaskHost(o =>
            {
                o.DelayTasks           = true;
                o.Concurrency          = 1;
                o.SleepIntervalSeconds = 1;
            });

            host.TryScheduleTaskAsync(typeof(StaticCountingHandler), null, o =>
            {
                o.Data  = "{ \"Foo\": \"123\" }";
                o.RunAt = Store.GetTaskTimestamp() + TimeSpan.FromSeconds(1);
                o.RepeatIndefinitely(CronTemplates.Secondly(1));
            });
            host.Start();             // <-- starts background thread to poll for tasks

            Assert.True(StaticCountingHandler.Count == 0,
                        "handler should not have queued immediately since tasks are delayed");

            Thread.Sleep(TimeSpan.FromSeconds(2));             // <-- enough time for the next occurrence
            Assert.True(StaticCountingHandler.Count > 0,
                        "handler should have executed since we scheduled it in the future");
            Assert.NotNull(StaticCountingHandler.Data /*, "handler did not preserve data"*/);
            Assert.Equal((string)StaticCountingHandler.Data, "123" /*, "handler misread data"*/);

            StaticCountingHandler.Data = null;

            Thread.Sleep(TimeSpan.FromSeconds(2));             // <-- enough time for the next occurrence
            Assert.True(StaticCountingHandler.Count >= 2);
            Assert.NotNull(StaticCountingHandler.Data /*, "handler did not preserve data"*/);
            Assert.Equal(StaticCountingHandler.Data, "123" /*, "handler misread data"*/);
        }
Exemple #5
0
        public void Every_nth_weekday(DayOfWeek n)
        {
            var cron     = CronTemplates.Weekly(n);
            var schedule = CronTemplates.Parse(cron);
            var diff     = CompareTwoCronOccurrences(schedule);

            Assert.Equal(7, diff.Days);
        }
Exemple #6
0
        public void Every_n_days(int n)
        {
            var cron     = CronTemplates.Daily(n);
            var schedule = CronTemplates.Parse(cron);
            var diff     = CompareTwoCronOccurrences(schedule);

            Assert.Equal(n, diff.Days);
        }
Exemple #7
0
        public void Monthly_on_first_of_month()
        {
            var cron     = CronTemplates.Monthly();
            var schedule = CronTemplates.Parse(cron);
            var diff     = CompareTwoCronOccurrences(schedule);

            Assert.True(diff.Days == 30 || diff.Days == 31);
        }
Exemple #8
0
        public async Task <IActionResult> CreateWeeklyBackgroundTask([FromBody] CreateBackgroundTaskModel model,
                                                                     [FromRoute] DayOfWeek dayOfWeek = DayOfWeek.Sunday, [FromRoute] int atHour = 0,
                                                                     [FromRoute] int atMinute        = 0, [FromRoute] int atSecond = 0)
        {
            model.Expression = CronTemplates.Weekly(dayOfWeek, atHour, atMinute, atSecond);

            return(await CreateBackgroundTask(model));
        }
Exemple #9
0
        public void Monthly_on_first_of_month()
        {
            string          cron     = CronTemplates.Monthly();
            CrontabSchedule schedule = CrontabSchedule.Parse(cron);
            TimeSpan        diff     = CompareTwoCronOccurences(schedule);

            Assert.True(diff.Days == 30 || diff.Days == 31);
        }
Exemple #10
0
        public void Every_n_hours(int n)
        {
            var cron     = CronTemplates.Hourly(n);
            var schedule = CrontabSchedule.Parse(cron);
            var diff     = CompareTwoCronOccurences(schedule);

            Assert.Equal(n, diff.Hours);
        }
 public void Occurrence_is_in_UTC()
 {
     var task = new ScheduledTask();
     task.RunAt = DateTimeOffset.UtcNow;
     
     task.Expression = CronTemplates.Daily(1, 3, 30);
     DateTimeOffset? next = task.NextOccurrence;
     Assert.NotNull(next);
     Assert.True(next.Value.Hour == 3);
     Assert.Equal(next.Value.Hour, next.Value.UtcDateTime.Hour);
 }
Exemple #12
0
        public void Occurrence_is_in_UTC()
        {
            var task = new BackgroundTask {
                RunAt = DateTimeOffset.UtcNow, Expression = CronTemplates.Daily(1, 3, 30)
            };

            var next = task.NextOccurrence;

            Assert.NotNull(next);
            Assert.True(next.Value.Hour == 3);
            Assert.Equal(next.Value.Hour, next.Value.UtcDateTime.Hour);
        }
Exemple #13
0
        public void Every_nth_and_mth_weekday(DayOfWeek n, DayOfWeek m, int expected)
        {
            var cron     = CronTemplates.Weekly(onDays: new[] { n, m });
            var schedule = CronTemplates.Parse(cron);

            // These tests would be temporal if we used 'now', so must start from a known fixed date
            var start = new DateTime(2016, 9, 4);
            var from  = schedule.GetNextOccurrence(start);            // should always start on 9/5/2016 (Monday)
            var to    = schedule.GetNextOccurrence(from);
            var diff  = to - from;

            Assert.Equal(expected, diff.Days);
        }
Exemple #14
0
        public void Queues_for_delayed_execution_and_continuous_repeating_task()
        {
            var host = CreateBackgroundTaskHost(o =>
            {
                o.DelayTasks           = true;
                o.Concurrency          = 1;
                o.SleepIntervalSeconds = 1;
            });

            host.TryScheduleTaskAsync(typeof(StaticCountingHandler), null, o =>
            {
                o.RunAt = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(1);
                o.RepeatIndefinitely(CronTemplates.Secondly(1));
            });
            host.Start(); // <-- starts background thread to poll for tasks

            Assert.True(StaticCountingHandler.Count == 0, "handler should not have queued immediately since tasks are delayed");
            Thread.Sleep(TimeSpan.FromSeconds(2)); // <-- enough time for the next occurrence
            Assert.True(StaticCountingHandler.Count > 0, "handler should have executed since we scheduled it in the future");
            Thread.Sleep(TimeSpan.FromSeconds(2)); // <-- enough time for the next occurrence
            Assert.Equal(2, StaticCountingHandler.Count);
        }
Exemple #15
0
        public void Queues_for_delayed_execution_and_continous_repeating_task()
        {
            using (var db = new SqlServerFixture())
            {
                MigrationHelper.MigrateToLatest("sqlserver", db.ConnectionString);

                ScheduledProducerSettings settings = new ScheduledProducerSettings
                {
                    DelayTasks    = true,
                    Concurrency   = 0,
                    SleepInterval = TimeSpan.FromSeconds(1),
                    Store         = new SqlScheduleStore(db.ConnectionString)
                };

                ScheduledProducer scheduler = new ScheduledProducer(settings);
                scheduler.ScheduleAsync <StaticCountingHandler>(o => o.RepeatIndefinitely(CronTemplates.Minutely()));
                scheduler.Start(); // <-- starts background thread to poll for tasks

                Assert.True(StaticCountingHandler.Count == 0, "handler should not have queued immediately since tasks are delayed");
                Thread.Sleep(TimeSpan.FromMinutes(1.1)); // <-- enough time for the next occurrence
                Assert.True(StaticCountingHandler.Count > 0, "handler should have executed since we scheduled it in the future");
                Assert.Equal(2, StaticCountingHandler.Count);
            }
        }
Exemple #16
0
 private CrontabSchedule TryParseCron()
 {
     return(string.IsNullOrWhiteSpace(Expression) ? null :
            !CronTemplates.TryParse(Expression, out var schedule) ? null : schedule);
 }