diff --git a/WebsitePanel/Database/update_db.sql b/WebsitePanel/Database/update_db.sql index cca0ebdb..50de0516 100644 --- a/WebsitePanel/Database/update_db.sql +++ b/WebsitePanel/Database/update_db.sql @@ -1075,7 +1075,7 @@ SELECT FROM BackgroundTasks AS T INNER JOIN BackgroundTaskStack AS TS ON TS.TaskId = T.ID -WHERE T.EffectiveUserID = @ActorID AND T.Guid = @Guid +WHERE T.Guid = @Guid GO IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE type = 'P' AND name = 'GetBackgroundTopTask') @@ -1113,7 +1113,7 @@ SELECT TOP 1 FROM BackgroundTasks AS T INNER JOIN BackgroundTaskStack AS TS ON TS.TaskId = T.ID -WHERE T.EffectiveUserID = @ActorID AND T.Guid = @Guid +WHERE T.Guid = @Guid ORDER BY T.StartDate DESC GO @@ -1372,6 +1372,7 @@ AS SELECT T.ID, + T.Guid, T.TaskID, T.ScheduleId, T.PackageId, diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Log/AuditLog.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Log/AuditLog.cs index ec012ecf..e315d227 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Log/AuditLog.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Log/AuditLog.cs @@ -92,7 +92,7 @@ namespace WebsitePanel.EnterpriseServer DataProvider.AddAuditLogRecord(recordId, severityId, userId, username, packageId, itemId, itemName, startDate, finishDate, sourceName, taskName, executionLog); } - catch + catch(Exception ex) { // skip error } diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/Scheduler.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/Scheduler.cs index 05834c09..ae4a819a 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/Scheduler.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/Scheduler.cs @@ -97,18 +97,20 @@ namespace WebsitePanel.EnterpriseServer private static void RunManualTasks() { - var tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Starting); + var tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Stopping); foreach (var task in tasks) { - new Thread(() => RunBackgroundTask(task)) {Priority = ThreadPriority.Highest}.Start(); + TaskManager.StopTask(task.TaskId); } - tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Stopping); + tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Starting); foreach (var task in tasks) { - TaskManager.StopTask(task); + var taskThread = new Thread(() => RunBackgroundTask(task)) { Priority = ThreadPriority.Highest }; + taskThread.Start(); + TaskManager.AddTaskThread(task, taskThread); } } @@ -130,8 +132,6 @@ namespace WebsitePanel.EnterpriseServer var objTask = (SchedulerTask)Activator.CreateInstance(Type.GetType(schedule.Task.TaskType)); objTask.DoWork(); - - Thread.Sleep(100000); } catch (Exception ex) { diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerController.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerController.cs index 02298569..e6eb34cd 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerController.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerController.cs @@ -165,7 +165,7 @@ namespace WebsitePanel.EnterpriseServer var backgroundTask = new BackgroundTask( Guid.NewGuid(), - schedule.ScheduleInfo.TaskId, + Guid.NewGuid().ToString("N"), SecurityContext.User.UserId, SecurityContext.User.IsPeer ? SecurityContext.User.OwnerId diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerJob.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerJob.cs index 7475b76c..688442cc 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerJob.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Scheduling/SchedulerJob.cs @@ -66,7 +66,6 @@ namespace WebsitePanel.EnterpriseServer { // create worker Thread worker = new Thread(new ThreadStart(RunSchedule)); - // set worker priority switch (scheduleInfo.Priority) { @@ -109,7 +108,7 @@ namespace WebsitePanel.EnterpriseServer objTask.DoWork(); else throw new Exception(String.Format("Could not create scheduled task of '{0}' type", - task.TaskType)); + task.TaskType)); } catch (Exception ex) { diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskController.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskController.cs index 29d03634..b85ced39 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskController.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskController.cs @@ -91,7 +91,7 @@ namespace WebsitePanel.EnterpriseServer AddTaskParams(task.Id, task.Params); - if (task.Completed || task.Status == BackgroundTaskStatus.Abort || task.Status == BackgroundTaskStatus.Stopping) + if (task.Completed || task.Status == BackgroundTaskStatus.Abort) { DeleteTaskStack(task.Id); } diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskManager.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskManager.cs index 15465ad3..ecf5d5f3 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskManager.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer.Code/Tasks/TaskManager.cs @@ -45,6 +45,8 @@ namespace WebsitePanel.EnterpriseServer public class TaskManager { private static Hashtable eventHandlers = null; + //using id instead of guid + private static Dictionary _taskThreadsDictionary = new Dictionary(); // purge timer, used for killing old tasks from the hash static Timer purgeTimer = new Timer(new TimerCallback(PurgeCompletedTasks), @@ -164,6 +166,8 @@ namespace WebsitePanel.EnterpriseServer BackgroundTask task = new BackgroundTask(Guid, taskId, userId, effectiveUserId, source, taskName, itemNameStr, itemId, scheduleId, packageId, maximumExecutionTime, parameters); + AddTaskThread(task, Thread.CurrentThread); + List tasks = TaskController.GetTasks(Guid); if (tasks.Count > 0) @@ -289,7 +293,7 @@ namespace WebsitePanel.EnterpriseServer topTask.Logs = TaskController.GetLogs(topTask.Id, topTask.StartDate); string executionLog = FormatExecutionLog(topTask); - UserInfo user = UserController.GetUserInternally(topTask.UserId); + UserInfo user = UserController.GetUserInternally(topTask.EffectiveUserId); string username = user != null ? user.Username : null; AuditLog.AddAuditLogRecord(topTask.TaskId, topTask.Severity, topTask.EffectiveUserId, @@ -419,7 +423,7 @@ namespace WebsitePanel.EnterpriseServer if (task.MaximumExecutionTime != -1 && ((TimeSpan)(DateTime.Now - task.StartDate)).TotalSeconds > task.MaximumExecutionTime) { - task.Status = BackgroundTaskStatus.Abort; + task.Status = BackgroundTaskStatus.Stopping; TaskController.UpdateTask(task); } @@ -526,6 +530,7 @@ namespace WebsitePanel.EnterpriseServer { if (task.ScheduleId > 0 && !task.Completed + && (task.Status == BackgroundTaskStatus.Run || task.Status == BackgroundTaskStatus.Starting) && !scheduledTasks.ContainsKey(task.ScheduleId)) scheduledTasks.Add(task.ScheduleId, task); } @@ -546,6 +551,14 @@ namespace WebsitePanel.EnterpriseServer task.NotifyOnComplete = true; } + internal static void AddTaskThread(BackgroundTask task, Thread taskThread) + { + if (_taskThreadsDictionary.ContainsKey(task.Id)) + _taskThreadsDictionary[task.Id] = taskThread; + else + _taskThreadsDictionary.Add(task.Id, taskThread); + } + public static void StopTask(string taskId) { BackgroundTask task = GetTask(taskId); @@ -557,35 +570,18 @@ namespace WebsitePanel.EnterpriseServer task.Status = BackgroundTaskStatus.Abort; - StopProcess(task.TaskId); + StopProcess(task.Id); TaskController.UpdateTask(task); } - public static void StopTask(BackgroundTask task) + private static void StopProcess(int key) { - if (task == null) - { - return; - } - - task.Status = BackgroundTaskStatus.Abort; - - StopProcess(task.TaskId); - - TaskController.UpdateTask(task); - } - - private static void StopProcess(string taskId) - { - var process = Process.GetProcesses().FirstOrDefault( - p => p.ProcessName.Equals(taskId, StringComparison.CurrentCultureIgnoreCase)); - - if (process != null) - { - process.Kill(); - process.WaitForExit(10000); - } + if (_taskThreadsDictionary.ContainsKey(key)) + { + _taskThreadsDictionary[key].Abort(); + _taskThreadsDictionary.Remove(key); + } } public static List GetUserTasks(int userId) @@ -827,6 +823,7 @@ namespace WebsitePanel.EnterpriseServer } #endregion + } }