diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/HostedSolution/OrganizationController.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/HostedSolution/OrganizationController.cs index c868b440..dbc21e84 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/HostedSolution/OrganizationController.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/HostedSolution/OrganizationController.cs @@ -2132,6 +2132,7 @@ namespace WebsitePanel.EnterpriseServer catch(Exception ex) { TaskManager.WriteError(ex); + res.IsSuccess = false; } return res; } diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Scheduling/Scheduler.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Scheduling/Scheduler.cs index 9da7a64b..8da3adaf 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Scheduling/Scheduler.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Scheduling/Scheduler.cs @@ -40,9 +40,9 @@ namespace WebsitePanel.EnterpriseServer public sealed class Scheduler { - static SchedulerJob nextSchedule = null; + public static SchedulerJob nextSchedule = null; - // main timer + // main timer and put it to sleep static Timer scheduleTimer = new Timer(new TimerCallback(RunNextSchedule), null, Timeout.Infinite, @@ -92,6 +92,8 @@ namespace WebsitePanel.EnterpriseServer // this will put the timer to sleep scheduleTimer.Change(Timeout.Infinite, Timeout.Infinite); + System.Threading.Thread.Sleep(1000); + // run immediately RunNextSchedule(null); } @@ -122,31 +124,41 @@ namespace WebsitePanel.EnterpriseServer static void RunSchedule(SchedulerJob schedule, bool changeNextRun) { - // update next run (if required) - if (changeNextRun) + + + try { - SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo); + // update next run (if required) + if (changeNextRun) + { + SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo); + } + + // disable run once task + if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime) + schedule.ScheduleInfo.Enabled = false; + + Dictionary scheduledTasks = TaskManager.GetScheduledTasks(); + if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId)) + // this task should be run, so + // update its last run + schedule.ScheduleInfo.LastRun = DateTime.Now; + + // update schedule + SchedulerController.UpdateSchedule(schedule.ScheduleInfo); + + // skip execution if the current task is still running + scheduledTasks = TaskManager.GetScheduledTasks(); + if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId)) + { + // run the schedule in the separate thread + schedule.Run(); + } + } + catch (Exception Ex) + { + TaskManager.WriteError(string.Format("RunSchedule Error : {0}", Ex.Message)); } - - // disable run once task - if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime) - schedule.ScheduleInfo.Enabled = false; - - Dictionary scheduledTasks = TaskManager.GetScheduledTasks(); - if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId)) - // this task should be run, so - // update its last run - schedule.ScheduleInfo.LastRun = DateTime.Now; - - // update schedule - SchedulerController.UpdateSchedule(schedule.ScheduleInfo); - - // skip execution if the current task is still running - if (scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId)) - return; - - // run the schedule in the separate thread - schedule.Run(); } } } diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Tasks/TaskManager.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Tasks/TaskManager.cs index e7b3c904..f69cb478 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Tasks/TaskManager.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Tasks/TaskManager.cs @@ -150,8 +150,8 @@ namespace WebsitePanel.EnterpriseServer { // ERROR WriteLogRecord(2, ex.Message, ex.StackTrace); - return new Exception(String.Format("Error executing '{0}' task on '{1}' {2}", - TopTask.TaskName, TopTask.ItemName, TopTask.Source), ex); + return new Exception((TopTask != null) ? String.Format("Error executing '{0}' task on '{1}' {2}", + TopTask.TaskName, TopTask.ItemName, TopTask.Source) : String.Format("Error executing task"), ex); } public static void WriteError(Exception ex, string text, params string[] textParameters) @@ -182,12 +182,15 @@ namespace WebsitePanel.EnterpriseServer logRecord.TextParameters = textParameters; logRecord.TextIdent = TasksStack.Count - 1; logRecord.ExceptionStackTrace = stackTrace; - RootTask.LogRecords.Add(logRecord); - RootTask.LastLogRecord = logRecord; + if (RootTask != null) + { + RootTask.LogRecords.Add(logRecord); + RootTask.LastLogRecord = logRecord; - // change entire task severity - if (severity > RootTask.Severity) - RootTask.Severity = severity; + // change entire task severity + if (severity > RootTask.Severity) + RootTask.Severity = severity; + } } public static void CompleteTask() diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Global.asax.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Global.asax.cs index a1dc2caa..64282fa2 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Global.asax.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Global.asax.cs @@ -46,7 +46,12 @@ namespace WebsitePanel.EnterpriseServer protected void Application_Start(object sender, EventArgs e) { - Scheduler.Start(); + if (ConfigurationManager.AppSettings["WebsitePanel.DistableScheduler"] != null) + if (Boolean.Parse(ConfigurationManager.AppSettings["WebsitePanel.DistableScheduler"]) == false) + { + if (Scheduler.nextSchedule == null) + Scheduler.Start(); + } } protected void Application_End(object sender, EventArgs e)