Fixing bugs (Scheduler)

This commit is contained in:
vfedosevich 2013-05-28 17:55:44 +03:00
parent bcceb95f92
commit e8ed10a67b
7 changed files with 36 additions and 39 deletions

View file

@ -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
}

View file

@ -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)
{

View file

@ -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

View file

@ -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)
{

View file

@ -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);
}

View file

@ -45,6 +45,8 @@ namespace WebsitePanel.EnterpriseServer
public class TaskManager
{
private static Hashtable eventHandlers = null;
//using id instead of guid
private static Dictionary<int, Thread> _taskThreadsDictionary = new Dictionary<int, Thread>();
// 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<BackgroundTask> 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<BackgroundTask> GetUserTasks(int userId)
@ -827,6 +823,7 @@ namespace WebsitePanel.EnterpriseServer
}
#endregion
}
}