Esempio n. 1
0
        public async Task Queues_for_delayed_execution()
        {
            var host = CreateBackgroundTaskHost(o =>
            {
                o.DelayTasks           = true;
                o.Concurrency          = 1;
                o.SleepIntervalSeconds = 1;
            });
            await host.TryScheduleTaskAsync(typeof(StaticCountingHandler), null, o =>
            {
                o.RunAt =
                    Store.GetTaskTimestamp() + TimeSpan.FromSeconds(1);
            });

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

            host.Start();             // <-- starts background thread to poll for tasks

            await Task.Delay(2000);   // <-- enough time for the next occurrence

            Assert.True(StaticCountingHandler.Count > 0,
                        "handler should have executed since we scheduled it in the future");
            Assert.True(StaticCountingHandler.Count == 1,
                        "handler should have only executed once since it does not repeat");
        }
        public void Occurrence_is_in_UTC()
        {
            var task = new BackgroundTask
            {
                RunAt = _store.GetTaskTimestamp(), 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);
        }
Esempio n. 3
0
        private BackgroundTask CreateNewTask()
        {
            var task    = new BackgroundTask();
            var options = new BackgroundTaskOptions();

            // these values are required and must be set by implementation
            task.Handler         = "{}";
            task.MaximumAttempts = options.MaximumAttempts;
            task.MaximumRuntime  = TimeSpan.FromSeconds(options.MaximumRuntimeSeconds);
            task.DeleteOnError   = options.DeleteOnError;
            task.DeleteOnFailure = options.DeleteOnFailure;
            task.DeleteOnSuccess = options.DeleteOnSuccess;
            task.RunAt           = Store.GetTaskTimestamp();

            return(task);
        }
Esempio n. 4
0
        public bool IsRunningOvertime(IBackgroundTaskStore store)
        {
            if (!LockedAt.HasValue)
            {
                return(false);
            }

            if (!MaximumRuntime.HasValue)
            {
                return(false);
            }

            var now     = store.GetTaskTimestamp();
            var elapsed = now - LockedAt.Value;

            // overtime = 125% of maximum runtime
            var overage  = TimeSpan.FromTicks((long)(MaximumRuntime.Value.Ticks * 0.25f));
            var overtime = MaximumRuntime.Value + overage;

            return(elapsed >= overtime);
        }