Scheduler service fixes

This commit is contained in:
vfedosevich 2013-05-27 19:10:30 +03:00
parent 2cf0890e14
commit bcceb95f92
3 changed files with 62 additions and 41 deletions

View file

@ -42,18 +42,12 @@ namespace WebsitePanel.EnterpriseServer
public sealed class Scheduler
{
public static SchedulerJob nextSchedule = null;
public static SchedulerJob nextSchedule = null;
private static Timer timer = new Timer(ScheduleTasks, null, 30000, 30000);
public static void Start()
{
var serviceController = new ServiceController("WebsitePanel Scheduler");
if (serviceController != null && serviceController.Status != ServiceControllerStatus.Running)
{
serviceController.WaitForStatus(ServiceControllerStatus.Running);
}
ScheduleTasks(null);
{
ScheduleTasks();
}
public static bool IsScheduleActive(int scheduleId)
@ -82,21 +76,23 @@ namespace WebsitePanel.EnterpriseServer
TaskManager.StopTask(activeTask.TaskId);
}
public static void ScheduleTasks()
{
ScheduleTasks(null);
}
public static void ScheduleTasks(object obj)
{
RunManualTasks();
nextSchedule = SchedulerController.GetNextSchedule();
RunManualTasks();
nextSchedule = SchedulerController.GetNextSchedule();
if (nextSchedule != null)
{
{
if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
{
{
RunNextSchedule(null);
}
}
}
Thread.Sleep(30000);
ScheduleTasks(null);
}
private static void RunManualTasks()
@ -155,14 +151,14 @@ namespace WebsitePanel.EnterpriseServer
// call back for the timer function
static void RunNextSchedule(object obj) // obj ignored
{
{
if (nextSchedule == null)
return;
RunSchedule(nextSchedule, true);
// schedule next task
ScheduleTasks(null);
ScheduleTasks();
}
static void RunSchedule(SchedulerJob schedule, bool changeNextRun)

View file

@ -15,6 +15,11 @@ namespace WebsitePanel.EnterpriseServer
BackgroundTask task = ObjectUtils.FillObjectFromDataReader<BackgroundTask>(
DataProvider.GetBackgroundTask(SecurityContext.User.UserId, taskId));
if (task == null)
{
return null;
}
task.Params = GetTaskParams(task.Id);
return task;
@ -27,9 +32,14 @@ namespace WebsitePanel.EnterpriseServer
}
public static List<BackgroundTask> GetTasks()
{
return GetTasks(SecurityContext.User.UserId);
}
public static List<BackgroundTask> GetTasks(int actorId)
{
return ObjectUtils.CreateListFromDataReader<BackgroundTask>(
DataProvider.GetBackgroundTasks(SecurityContext.User.UserId));
DataProvider.GetBackgroundTasks(actorId));
}
public static List<BackgroundTask> GetTasks(Guid guid)
@ -49,6 +59,11 @@ namespace WebsitePanel.EnterpriseServer
BackgroundTask task = ObjectUtils.FillObjectFromDataReader<BackgroundTask>(
DataProvider.GetBackgroundTopTask(SecurityContext.User.UserId, guid));
if (task == null)
{
return null;
}
task.Params = GetTaskParams(task.Id);
return task;
@ -88,7 +103,7 @@ namespace WebsitePanel.EnterpriseServer
}
public static void AddTaskParams(int taskId, List<BackgroundTaskParameter> parameters)
{
{
foreach (BackgroundTaskParameter param in SerializeParams(parameters))
{
DataProvider.AddBackgroundTaskParam(taskId, param.Name, param.SerializerValue, param.TypeName);
@ -118,7 +133,7 @@ namespace WebsitePanel.EnterpriseServer
{
log.TextParameters = ReBuildParametersXml(log.XmlParameters);
}
return logs;
}
@ -142,7 +157,7 @@ namespace WebsitePanel.EnterpriseServer
return parameters;
}
private static List<BackgroundTaskParameter> DeserializeParams(List<BackgroundTaskParameter> parameters)
private static List<BackgroundTaskParameter> DeserializeParams(List<BackgroundTaskParameter> parameters)
{
foreach (BackgroundTaskParameter param in parameters)
{
@ -188,3 +203,4 @@ namespace WebsitePanel.EnterpriseServer
}
}
}

View file

@ -153,8 +153,13 @@ namespace WebsitePanel.EnterpriseServer
}
int userId = SecurityContext.User.OwnerId;
int effectiveUserId = SecurityContext.User.IsPeer ? SecurityContext.User.OwnerId : SecurityContext.User.UserId;
String itemNameStr = itemName != null ? itemName.ToString() : String.Empty;
int effectiveUserId = SecurityContext.User.IsPeer
? userId
: SecurityContext.User.UserId;
String itemNameStr = itemName != null
? itemName.ToString()
: String.Empty;
BackgroundTask task = new BackgroundTask(Guid, taskId, userId, effectiveUserId, source, taskName, itemNameStr,
itemId, scheduleId, packageId, maximumExecutionTime, parameters);
@ -170,7 +175,7 @@ namespace WebsitePanel.EnterpriseServer
tasks.Count - 1,
true,
String.Format("{0}_{1}", source, taskName),
new string[] {itemNameStr});
new string[] { itemNameStr });
TaskController.AddLog(log);
@ -234,7 +239,7 @@ namespace WebsitePanel.EnterpriseServer
private static void WriteLogRecord(int severity, string text, string stackTrace, params string[] textParameters)
{
List<BackgroundTask> tasks = TaskController.GetTasks(Guid);
if (tasks.Count > 0)
{
BackgroundTask rootTask = tasks[0];
@ -287,7 +292,7 @@ namespace WebsitePanel.EnterpriseServer
UserInfo user = UserController.GetUserInternally(topTask.UserId);
string username = user != null ? user.Username : null;
AuditLog.AddAuditLogRecord(topTask.TaskId, topTask.Severity, topTask.UserId,
AuditLog.AddAuditLogRecord(topTask.TaskId, topTask.Severity, topTask.EffectiveUserId,
username, topTask.PackageId, topTask.ItemId,
topTask.ItemName, topTask.StartDate, topTask.FinishDate, topTask.Source,
topTask.TaskName, executionLog);
@ -412,7 +417,7 @@ namespace WebsitePanel.EnterpriseServer
foreach (BackgroundTask task in tasks)
{
if (task.MaximumExecutionTime != -1
&& ((TimeSpan) (DateTime.Now - task.StartDate)).TotalSeconds > task.MaximumExecutionTime)
&& ((TimeSpan)(DateTime.Now - task.StartDate)).TotalSeconds > task.MaximumExecutionTime)
{
task.Status = BackgroundTaskStatus.Abort;
@ -503,7 +508,7 @@ namespace WebsitePanel.EnterpriseServer
public static BackgroundTask GetTaskWithLogRecords(string taskId, DateTime startLogTime)
{
BackgroundTask task = GetTask(taskId);
if (task == null)
return null;
@ -549,11 +554,11 @@ namespace WebsitePanel.EnterpriseServer
{
return;
}
task.Status = BackgroundTaskStatus.Abort;
StopProcess(task.TaskId);
TaskController.UpdateTask(task);
}
@ -563,14 +568,14 @@ namespace WebsitePanel.EnterpriseServer
{
return;
}
task.Status = BackgroundTaskStatus.Abort;
StopProcess(task.TaskId);
TaskController.UpdateTask(task);
}
private static void StopProcess(string taskId)
{
var process = Process.GetProcesses().FirstOrDefault(
@ -595,8 +600,11 @@ namespace WebsitePanel.EnterpriseServer
// get user tasks
foreach (BackgroundTask task in TaskController.GetTasks())
{
if (task.UserId == userId && !task.Completed)
if (task.UserId == userId && !task.Completed
&& task.Status == BackgroundTaskStatus.Run)
{
list.Add(task);
}
}
return list;
}
@ -698,7 +706,7 @@ namespace WebsitePanel.EnterpriseServer
List<string> handlersList = (List<string>)eventHandlers[fullTaskName];
return handlersList == null ? null : handlersList.ToArray();
}
#endregion
@ -821,3 +829,4 @@ namespace WebsitePanel.EnterpriseServer
#endregion
}
}