Repository navigation
Expand file tree
/
Copy pathTaskSchedulerController.cs
More file actions
87 lines (74 loc) · 2.84 KB
/
Copy pathTaskSchedulerController.cs
File metadata and controls
87 lines (74 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using SmartStore.Core.Domain.Tasks;
using SmartStore.Services.Tasks;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Services.Security;
using SmartStore.Services;
using SmartStore.Collections;
namespace SmartStore.Web.Controllers
{
[SessionState(SessionStateBehavior.ReadOnly)]
public class TaskSchedulerController : Controller
{
private readonly ITaskScheduler _taskScheduler;
private readonly IScheduleTaskService _scheduledTaskService;
private readonly ITaskExecutor _taskExecutor;
private readonly ICommonServices _services;
public TaskSchedulerController(
ITaskScheduler taskScheduler,
IScheduleTaskService scheduledTaskService,
ITaskExecutor taskExecutor,
ICommonServices services)
{
this._taskScheduler = taskScheduler;
this._scheduledTaskService = scheduledTaskService;
this._taskExecutor = taskExecutor;
this._services = services;
}
[HttpPost]
public ActionResult Sweep()
{
if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
return new HttpUnauthorizedResult();
var pendingTasks = _scheduledTaskService.GetPendingTasks();
var prevTaskStart = DateTime.UtcNow;
var count = 0;
foreach (var task in pendingTasks)
{
var elapsedSincePrevTask = DateTime.UtcNow - prevTaskStart;
if (elapsedSincePrevTask >= TimeSpan.FromMinutes(_taskScheduler.SweepIntervalMinutes))
{
// the previous Task execution in this loop took longer
// than the scheduler's sweep interval. Most likely a subsequent
// Sweep call executed it already in another HTTP request.
// 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);
}
if (task.IsPending)
{
prevTaskStart = DateTime.UtcNow;
_taskExecutor.Execute(task);
count++;
}
}
return Content("{0} of {1} pending tasks executed".FormatInvariant(count, pendingTasks.Count));
}
[HttpPost]
public ActionResult Execute(int id /* taskId */)
{
if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
return new HttpUnauthorizedResult();
var task = _scheduledTaskService.GetTaskById(id);
if (task == null)
return HttpNotFound();
_taskExecutor.Execute(task, QueryString.Current.ToDictionary());
return Content("Task '{0}' executed".FormatCurrent(task.Name));
}
}
}