private async Task Poll(HttpContext context, ITaskStore taskStore, ITaskExecutor executor, IDictionary <string, string> taskParameters) { var pendingTasks = await taskStore.GetPendingTasksAsync(); var numTasks = pendingTasks.Count; var numExecuted = 0; if (pendingTasks.Any()) { await Virtualize(context, taskParameters); } for (var i = 0; i < numTasks; i++) { var task = pendingTasks[i]; if (i > 0 /*&& (DateTime.UtcNow - _sweepStart).TotalMinutes > _taskScheduler.SweepIntervalMinutes*/) { // Maybe a subsequent Sweep call or another machine in a webfarm executed // successive tasks already. // To be able to determine this, we need to reload the entity from the database. // The TaskExecutor will exit when the task should be in running state then. await taskStore.ReloadTaskAsync(task); task.LastExecution = await taskStore.GetLastExecutionInfoByTaskIdAsync(task.Id); } if (task.IsPending) { await executor.ExecuteAsync(task, context, taskParameters); numExecuted++; } } context.Response.StatusCode = StatusCodes.Status200OK; await context.Response.WriteAsync($"{numExecuted} of {numTasks} pending tasks executed."); }