예제 #1
0
        public async Task <ActionResult> Sweep()
        {
            if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
            {
                return(new HttpUnauthorizedResult());
            }

            var pendingTasks = await _scheduleTaskService.GetPendingTasksAsync();

            var count          = 0;
            var taskParameters = QueryString.Current.ToDictionary();

            if (pendingTasks.Count > 0)
            {
                Virtualize(taskParameters);
            }

            for (var i = 0; i < pendingTasks.Count; 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.
                    _services.DbContext.ReloadEntity(task);
                    task.LastHistoryEntry = _scheduleTaskService.GetLastHistoryEntryByTaskId(task.Id);
                }

                if (task.IsPending)
                {
                    await _taskExecutor.ExecuteAsync(task, taskParameters);

                    count++;
                }
            }

            return(Content("{0} of {1} pending tasks executed".FormatInvariant(count, pendingTasks.Count)));
        }