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