Exemplo n.º 1
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"*/);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateSecondlyBackgroundTask([FromBody] CreateBackgroundTaskModel model,
                                                                       [FromRoute] int seconds = 0)
        {
            model.Expression = CronTemplates.Secondly(seconds);

            return(await CreateBackgroundTask(model));
        }
Exemplo n.º 3
0
        public void Every_n_seconds(int n)
        {
            var cron     = CronTemplates.Secondly(n);
            var schedule = CronTemplates.Parse(cron);
            var diff     = CompareTwoCronOccurrences(schedule);

            Assert.Equal(n, diff.Seconds);
        }
Exemplo n.º 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.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);
        }