update start/stop task from windows service

This commit is contained in:
vfedosevich 2013-05-23 18:18:25 +03:00
parent 8577c73c4c
commit d14b5fc01f
16 changed files with 240 additions and 93 deletions

View file

@ -58,10 +58,8 @@ namespace WebsitePanel.EnterpriseServer
public static bool IsScheduleActive(int scheduleId)
{
Dictionary<int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
ScheduleInfo scheduleInfo = SchedulerController.GetSchedule(scheduleId);
return scheduledTasks.ContainsKey(scheduleId) || scheduleInfo.LastRun > scheduleInfo.LastFinish;
return scheduledTasks.ContainsKey(scheduleId);
}
public static void StartSchedule(SchedulerJob schedule)
@ -85,8 +83,10 @@ namespace WebsitePanel.EnterpriseServer
public static void ScheduleTasks()
{
nextSchedule = SchedulerController.GetNextSchedule();
RunManualTasks();
nextSchedule = SchedulerController.GetNextSchedule();
// set timer
if (nextSchedule != null)
{
@ -94,9 +94,7 @@ namespace WebsitePanel.EnterpriseServer
{
// this will put the timer to sleep
scheduleTimer.Change(Timeout.Infinite, Timeout.Infinite);
System.Threading.Thread.Sleep(1000);
Thread.Sleep(1000);
// run immediately
RunNextSchedule(null);
}
@ -106,13 +104,65 @@ namespace WebsitePanel.EnterpriseServer
TimeSpan ts = nextSchedule.ScheduleInfo.NextRun.Subtract(DateTime.Now);
if (ts < TimeSpan.Zero)
ts = TimeSpan.Zero; // cannot be negative !
// invoke after the timespan
scheduleTimer.Change((long)ts.TotalMilliseconds, Timeout.Infinite);
}
}
}
private static void RunManualTasks()
{
var tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Starting);
foreach (var task in tasks)
{
StartManualTask(task);
}
tasks = TaskController.GetProcessTasks(BackgroundTaskStatus.Stopping);
foreach (var task in tasks)
{
TaskManager.StopTask(task.TaskId);
}
}
private static void StartManualTask(BackgroundTask backgroundTask)
{
new Thread(() => RunBackgroundTask(backgroundTask)) { Priority = ThreadPriority.Highest }.Start();
backgroundTask.Status = BackgroundTaskStatus.Run;
TaskController.UpdateTask(backgroundTask);
}
private static void RunBackgroundTask(BackgroundTask backgroundTask)
{
UserInfo user = PackageController.GetPackageOwner(backgroundTask.PackageId);
SecurityContext.SetThreadPrincipal(user.UserId);
var schedule = SchedulerController.GetScheduleComplete(backgroundTask.ScheduleId);
TaskManager.StartTask(backgroundTask.Source, backgroundTask.TaskName, backgroundTask.ItemName, backgroundTask.ItemId, backgroundTask.ScheduleId, backgroundTask.PackageId, backgroundTask.MaximumExecutionTime, backgroundTask.Params);
try
{
var objTask = (SchedulerTask)Activator.CreateInstance(Type.GetType(schedule.Task.TaskType));
objTask.DoWork();
Thread.Sleep(100000);
}
catch (Exception ex)
{
TaskManager.WriteError(ex, "Error executing scheduled task");
}
finally
{
try
{
TaskManager.CompleteTask();
}
catch (Exception)
{
}
}
}
// call back for the timer function
static void RunNextSchedule(object obj) // obj ignored
{

View file

@ -31,6 +31,7 @@ using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Linq;
using WebsitePanel.EnterpriseServer.Base.Scheduling;
namespace WebsitePanel.EnterpriseServer
@ -157,30 +158,64 @@ namespace WebsitePanel.EnterpriseServer
{
// check account
int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);
if (accountCheck < 0) return accountCheck;
if (accountCheck < 0)
return accountCheck;
SchedulerJob schedule = GetScheduleComplete(scheduleId);
if (schedule == null)
return 0;
Scheduler.StartSchedule(schedule);
var parameters = schedule.ScheduleInfo.Parameters.Select(
prm => new BackgroundTaskParameter(prm.ParameterId, prm.ParameterValue)).ToList();
var backgroundTask = new BackgroundTask(
schedule.ScheduleInfo.TaskId,
SecurityContext.User.UserId,
SecurityContext.User.IsPeer
? SecurityContext.User.OwnerId
: SecurityContext.User.UserId, "SCHEDULER", "RUN_SCHEDULE",
schedule.ScheduleInfo.ScheduleName,
schedule.ScheduleInfo.ScheduleId,
schedule.ScheduleInfo.ScheduleId,
schedule.ScheduleInfo.PackageId,
schedule.ScheduleInfo.MaxExecutionTime, parameters) { Status = BackgroundTaskStatus.Starting };
TaskController.AddTask(backgroundTask);
return 0;
}
public static int StopSchedule(int scheduleId)
{
// check account
int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);
if (accountCheck < 0) return accountCheck;
if (accountCheck < 0)
return accountCheck;
SchedulerJob schedule = GetScheduleComplete(scheduleId);
if (schedule == null)
return 0;
Scheduler.StopSchedule(schedule);
var parameters = schedule.ScheduleInfo.Parameters.Select(
prm => new BackgroundTaskParameter(prm.ParameterId, prm.ParameterValue)).ToList();
var backgroundTask = new BackgroundTask(
schedule.ScheduleInfo.TaskId,
SecurityContext.User.UserId,
SecurityContext.User.IsPeer
? SecurityContext.User.OwnerId
: SecurityContext.User.UserId, "SCHEDULER", "STOP_SCHEDULE",
schedule.ScheduleInfo.ScheduleName,
schedule.ScheduleInfo.ScheduleId,
schedule.ScheduleInfo.ScheduleId,
schedule.ScheduleInfo.PackageId,
schedule.ScheduleInfo.MaxExecutionTime, parameters) { Status = BackgroundTaskStatus.Stopping };
TaskController.AddTask(backgroundTask);
return 0;
}
public static void CalculateNextStartTime(ScheduleInfo schedule)

View file

@ -89,7 +89,7 @@ namespace WebsitePanel.EnterpriseServer
UserInfo user = PackageController.GetPackageOwner(scheduleInfo.PackageId);
SecurityContext.SetThreadPrincipal(user.UserId);
IList<BackgroundTaskParameter> parameters = new List<BackgroundTaskParameter>();
List<BackgroundTaskParameter> parameters = new List<BackgroundTaskParameter>();
foreach (ScheduleTaskParameterInfo prm in scheduleInfo.Parameters)
{
parameters.Add(new BackgroundTaskParameter(prm.ParameterId, prm.ParameterValue));