initial source commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Quartz;
|
||||
using Disco.Data.Repository;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public abstract class ScheduledTask : IJob
|
||||
{
|
||||
public abstract void InitalizeScheduledTask(DiscoDataContext dbContext);
|
||||
|
||||
internal protected ScheduledTaskStatus Status { get; private set; }
|
||||
internal protected IJobExecutionContext ExecutionContext { get; private set; }
|
||||
|
||||
public abstract bool CancelInitiallySupported { get; }
|
||||
public abstract bool SingleInstanceTask { get; }
|
||||
public virtual bool IsSilent { get { return false; } }
|
||||
public virtual bool LogExceptionsOnly { get { return false; } }
|
||||
public abstract string TaskName { get; }
|
||||
protected abstract void ExecuteTask();
|
||||
|
||||
#region Protected Triggers
|
||||
/// <summary>
|
||||
/// Schedules the Task to Begin Immediately
|
||||
/// </summary>
|
||||
protected ScheduledTaskStatus ScheduleTask()
|
||||
{
|
||||
return ScheduleTask(null, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// Schedules the Task to Begin Immediately
|
||||
/// </summary>
|
||||
/// <param name="DataMap">DataMap passed into the executing Task</param>
|
||||
/// <returns></returns>
|
||||
protected ScheduledTaskStatus ScheduleTask(JobDataMap DataMap)
|
||||
{
|
||||
return ScheduleTask(null, DataMap);
|
||||
}
|
||||
/// <summary>
|
||||
/// Schedules the Task to Begin based on the Trigger
|
||||
/// </summary>
|
||||
/// <param name="Trigger">Trigger for the Task</param>
|
||||
protected ScheduledTaskStatus ScheduleTask(TriggerBuilder Trigger)
|
||||
{
|
||||
return ScheduleTask(Trigger, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// Schedules the Task to Begin based on the Trigger including the DataMap
|
||||
/// </summary>
|
||||
/// <param name="Trigger">Trigger for the Task</param>
|
||||
/// <param name="DataMap">DataMap passed into the executing Task</param>
|
||||
/// <returns></returns>
|
||||
protected ScheduledTaskStatus ScheduleTask(TriggerBuilder Trigger, JobDataMap DataMap)
|
||||
{
|
||||
if (Trigger == null)
|
||||
Trigger = TriggerBuilder.Create(); // Defaults to Start Immediately
|
||||
|
||||
if (DataMap != null)
|
||||
Trigger = Trigger.UsingJobData(DataMap);
|
||||
|
||||
return ScheduledTasks.RegisterTask(this, Trigger);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void Execute(IJobExecutionContext context)
|
||||
{
|
||||
// Task Status
|
||||
this.ExecutionContext = context;
|
||||
this.Status = context.GetDiscoScheduledTaskStatus();
|
||||
if (this.Status == null)
|
||||
this.Status = ScheduledTasks.RegisterTask(this);
|
||||
|
||||
try
|
||||
{
|
||||
if (!this.LogExceptionsOnly)
|
||||
ScheduledTasksLog.LogScheduledTaskExecuted(this.Status.TaskName, this.Status.SessionId);
|
||||
|
||||
this.Status.Started();
|
||||
this.ExecuteTask();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ScheduledTasksLog.LogScheduledTaskException(this.Status.TaskName, this.Status.SessionId, this.GetType(), ex);
|
||||
this.Status.SetTaskException(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!this.Status.FinishedTimestamp.HasValue) // Scheduled Task Didn't Trigger 'Finished'
|
||||
this.Status.Finished();
|
||||
|
||||
var nextTriggerTime = context.NextFireTimeUtc;
|
||||
if (nextTriggerTime.HasValue)
|
||||
{ // Continuous Task
|
||||
this.Status.Reset(nextTriggerTime.Value.LocalDateTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.UnregisterTask();
|
||||
}
|
||||
|
||||
if (!this.LogExceptionsOnly)
|
||||
ScheduledTasksLog.LogScheduledTaskFinished(this.Status.TaskName, this.Status.SessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Quartz;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Quartz;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public class ScheduledTaskStatus
|
||||
{
|
||||
#region Backing Fields
|
||||
|
||||
private string _sessionId;
|
||||
private string _triggerKey;
|
||||
private string _taskName;
|
||||
private Type _taskType;
|
||||
private bool _isSilent;
|
||||
|
||||
private byte _progress;
|
||||
private string _currentProcess;
|
||||
private string _currentDescription;
|
||||
|
||||
private Exception _taskException;
|
||||
private bool _cancelInitiallySupported;
|
||||
private bool _cancelSupported;
|
||||
private bool _isCanceling;
|
||||
|
||||
private DateTime? _startedTimestamp;
|
||||
private DateTime? _nextScheduledTimestamp;
|
||||
private DateTime? _finishedTimestamp;
|
||||
|
||||
private string _finishedMessage;
|
||||
private string _finishedUrl;
|
||||
|
||||
private int _statusVersion = 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public string SessionId { get { return this._sessionId; } }
|
||||
public string TriggerKey { get { return this._triggerKey; } }
|
||||
public string TaskName { get { return this._taskName; } }
|
||||
public Type TaskType { get { return this._taskType; } }
|
||||
public bool IsSilent { get { return this._isSilent; } }
|
||||
|
||||
public byte Progress { get { return this._progress; } }
|
||||
public string CurrentProcess { get { return this._currentProcess; } }
|
||||
public string CurrentDescription { get { return this._currentDescription; } }
|
||||
|
||||
public Exception TaskException { get { return this._taskException; } }
|
||||
public bool CancelSupported { get { return this._cancelSupported; } }
|
||||
public bool IsCanceling { get { return this._isCanceling; } }
|
||||
|
||||
public DateTime? StartedTimestamp { get { return this._startedTimestamp; } }
|
||||
public DateTime? FinishedTimestamp { get { return this._finishedTimestamp; } }
|
||||
public DateTime? NextScheduledTimestamp { get { return this._nextScheduledTimestamp; } }
|
||||
|
||||
public string FinishedMessage { get { return this._finishedMessage; } }
|
||||
public string FinishedUrl { get { return this._finishedUrl; } }
|
||||
|
||||
public int StatusVersion { get { return this._statusVersion; } }
|
||||
|
||||
public bool IsRunning
|
||||
{
|
||||
get
|
||||
{
|
||||
return _startedTimestamp.HasValue && !_finishedTimestamp.HasValue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
public delegate void UpdatedEvent(ScheduledTaskStatus sender, string[] ChangedProperties);
|
||||
public delegate void CancelingEvent(ScheduledTaskStatus sender);
|
||||
public event UpdatedEvent Updated;
|
||||
public event CancelingEvent Canceling;
|
||||
#endregion
|
||||
|
||||
public ScheduledTaskStatus(ScheduledTask Task, string SessionId, string TriggerKey, string FinishedUrl = null)
|
||||
{
|
||||
this._taskName = Task.TaskName;
|
||||
this._taskType = Task.GetType();
|
||||
|
||||
this._sessionId = SessionId;
|
||||
this._triggerKey = TriggerKey;
|
||||
this._cancelInitiallySupported = Task.CancelInitiallySupported;
|
||||
this._cancelSupported = this._cancelInitiallySupported;
|
||||
|
||||
this._finishedUrl = FinishedUrl;
|
||||
|
||||
this._currentProcess = "Scheduled";
|
||||
this._currentDescription = "Scheduled Task for Execution";
|
||||
|
||||
this._progress = 0;
|
||||
}
|
||||
|
||||
#region Progress Actions
|
||||
public void UpdateStatus(byte Progress)
|
||||
{
|
||||
this._progress = Progress;
|
||||
UpdateTriggered(new string[] { "Progress" });
|
||||
}
|
||||
public void UpdateStatus(double Progress)
|
||||
{
|
||||
UpdateStatus((byte)Progress);
|
||||
}
|
||||
public void UpdateStatus(string CurrentDescription)
|
||||
{
|
||||
this._currentDescription = CurrentDescription;
|
||||
UpdateTriggered(new string[] { "CurrentDescription" });
|
||||
}
|
||||
public void UpdateStatus(byte Progress, string CurrentDescription)
|
||||
{
|
||||
this._progress = Progress;
|
||||
this._currentDescription = CurrentDescription;
|
||||
UpdateTriggered(new string[] { "Progress", "CurrentDescription" });
|
||||
}
|
||||
public void UpdateStatus(double Progress, string CurrentDescription)
|
||||
{
|
||||
UpdateStatus((byte)Progress, CurrentDescription);
|
||||
}
|
||||
public void UpdateStatus(byte Progress, string CurrentProcess, string CurrentDescription)
|
||||
{
|
||||
this._progress = Progress;
|
||||
this._currentProcess = CurrentProcess;
|
||||
this._currentDescription = CurrentDescription;
|
||||
UpdateTriggered(new string[] { "Progress", "CurrentProcess", "CurrentDescription" });
|
||||
}
|
||||
public void UpdateStatus(double Progress, string CurrentProcess, string CurrentDescription)
|
||||
{
|
||||
UpdateStatus((byte)Progress, CurrentProcess, CurrentDescription);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region State Actions
|
||||
public bool Canceled()
|
||||
{
|
||||
if (!this._isCanceling)
|
||||
{
|
||||
if (_cancelSupported)
|
||||
{ // Cancelling
|
||||
this._isCanceling = true;
|
||||
UpdateTriggered(new string[] { "IsCancelling" });
|
||||
if (this.Canceling != null)
|
||||
Canceling(this);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{ // Cancelling not supported
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // Already Cancelling
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public void SetCancelSupported(bool CancelSupported)
|
||||
{
|
||||
if (this._cancelSupported != CancelSupported)
|
||||
{
|
||||
this._cancelSupported = CancelSupported;
|
||||
UpdateTriggered(new string[] { "CancelSupported" });
|
||||
}
|
||||
}
|
||||
public void SetTaskException(Exception TaskException)
|
||||
{
|
||||
if (this._taskException != TaskException)
|
||||
{
|
||||
this._taskException = TaskException;
|
||||
UpdateTriggered(new string[] { "TaskException" });
|
||||
}
|
||||
}
|
||||
public void SetIsSilent(bool IsSilent)
|
||||
{
|
||||
if (this._isSilent != IsSilent)
|
||||
this._isSilent = IsSilent;
|
||||
}
|
||||
public void SetFinishedUrl(string FinishedUrl)
|
||||
{
|
||||
if (this._finishedUrl != FinishedUrl)
|
||||
{
|
||||
this._finishedUrl = FinishedUrl;
|
||||
UpdateTriggered(new string[] { "FinishedUrl" });
|
||||
}
|
||||
}
|
||||
public void SetFinishedMessage(string FinishedMessage)
|
||||
{
|
||||
if (this._finishedMessage != FinishedMessage)
|
||||
{
|
||||
this._finishedMessage = FinishedMessage;
|
||||
UpdateTriggered(new string[] { "FinishedMessage" });
|
||||
}
|
||||
}
|
||||
public void SetNextScheduledTimestamp(DateTime? NextScheduledTimestamp)
|
||||
{
|
||||
if (this._nextScheduledTimestamp != NextScheduledTimestamp)
|
||||
{
|
||||
this._nextScheduledTimestamp = NextScheduledTimestamp;
|
||||
UpdateTriggered(new string[] { "NextScheduledTimestamp" });
|
||||
}
|
||||
}
|
||||
public void Started()
|
||||
{
|
||||
List<string> changedProperties = new List<string>() { "IsRunning", "StartedTimestamp" };
|
||||
|
||||
this._startedTimestamp = DateTime.Now;
|
||||
|
||||
if (this._nextScheduledTimestamp != null)
|
||||
{
|
||||
this._nextScheduledTimestamp = null;
|
||||
changedProperties.Add("NextScheduledTimestamp");
|
||||
}
|
||||
if (this._finishedTimestamp != null)
|
||||
{
|
||||
this._finishedTimestamp = null;
|
||||
changedProperties.Add("FinishedTimestamp");
|
||||
}
|
||||
if (this._progress != 0)
|
||||
{
|
||||
this._progress = 0;
|
||||
changedProperties.Add("Progress");
|
||||
}
|
||||
if (this._currentProcess != "Starting")
|
||||
{
|
||||
this._currentProcess = "Starting";
|
||||
changedProperties.Add("CurrentProcess");
|
||||
}
|
||||
if (this._currentDescription != "Initializing Task for Execution")
|
||||
{
|
||||
this._currentDescription = "Initializing Task for Execution";
|
||||
changedProperties.Add("CurrentDescription");
|
||||
}
|
||||
if (this._taskException != null)
|
||||
{
|
||||
this._taskException = null;
|
||||
changedProperties.Add("TaskException");
|
||||
}
|
||||
if (this._cancelSupported != this._cancelInitiallySupported)
|
||||
{
|
||||
this._cancelSupported = this._cancelInitiallySupported;
|
||||
changedProperties.Add("CancelSupported");
|
||||
}
|
||||
{
|
||||
this._isCanceling = false;
|
||||
changedProperties.Add("IsCanceling");
|
||||
}
|
||||
if (this._isCanceling)
|
||||
{
|
||||
this._isCanceling = false;
|
||||
changedProperties.Add("IsCanceling");
|
||||
}
|
||||
UpdateTriggered(changedProperties.ToArray());
|
||||
}
|
||||
public void Finished()
|
||||
{
|
||||
Finished(this._finishedMessage, this._finishedUrl);
|
||||
}
|
||||
public void Finished(string FinishedMessage, string FinishedUrl)
|
||||
{
|
||||
List<string> changedProperties = new List<string>() { "IsRunning", "FinishedTimestamp" };
|
||||
|
||||
this._finishedTimestamp = DateTime.Now;
|
||||
|
||||
if (FinishedMessage != this._finishedMessage)
|
||||
{
|
||||
this._finishedMessage = FinishedMessage;
|
||||
changedProperties.Add("FinishedMessage");
|
||||
}
|
||||
if (FinishedUrl != this._finishedUrl)
|
||||
{
|
||||
this._finishedUrl = FinishedUrl;
|
||||
changedProperties.Add("FinishedUrl");
|
||||
}
|
||||
|
||||
if (this._isCanceling)
|
||||
{
|
||||
this._isCanceling = false;
|
||||
changedProperties.Add("IsCanceling");
|
||||
}
|
||||
UpdateTriggered(changedProperties.ToArray());
|
||||
}
|
||||
public void Reset(DateTime? NextScheduledTimestamp)
|
||||
{
|
||||
List<string> changedProperties = new List<string>();
|
||||
|
||||
if (this._nextScheduledTimestamp != NextScheduledTimestamp)
|
||||
{
|
||||
this._nextScheduledTimestamp = NextScheduledTimestamp;
|
||||
changedProperties.Add("NextScheduledTimestamp");
|
||||
}
|
||||
|
||||
if (this._startedTimestamp != null)
|
||||
{
|
||||
this._startedTimestamp = null;
|
||||
changedProperties.Add("StartedTimestamp");
|
||||
}
|
||||
if (this._finishedTimestamp != null)
|
||||
{
|
||||
this._finishedTimestamp = null;
|
||||
changedProperties.Add("FinishedTimestamp");
|
||||
}
|
||||
if (this._finishedMessage != null)
|
||||
{
|
||||
this._finishedMessage = null;
|
||||
changedProperties.Add("FinishedMessage");
|
||||
}
|
||||
if (this._finishedUrl != null)
|
||||
{
|
||||
this._finishedUrl = null;
|
||||
changedProperties.Add("FinishedUrl");
|
||||
}
|
||||
if (this._progress != 0)
|
||||
{
|
||||
this._progress = 0;
|
||||
changedProperties.Add("Progress");
|
||||
}
|
||||
if (this._currentProcess != "Scheduled")
|
||||
{
|
||||
this._currentProcess = "Scheduled";
|
||||
changedProperties.Add("CurrentProcess");
|
||||
}
|
||||
if (this._currentDescription != "Scheduled Task for Execution")
|
||||
{
|
||||
this._currentDescription = "Scheduled Task for Execution";
|
||||
changedProperties.Add("CurrentDescription");
|
||||
}
|
||||
if (this._isCanceling)
|
||||
{
|
||||
this._isCanceling = false;
|
||||
changedProperties.Add("IsCanceling");
|
||||
}
|
||||
UpdateTriggered(changedProperties.ToArray());
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void UpdateTriggered(string[] ChangedProperties)
|
||||
{
|
||||
this._statusVersion++;
|
||||
|
||||
if (Updated != null)
|
||||
Updated(this, ChangedProperties);
|
||||
|
||||
if (!_isSilent)
|
||||
ScheduledTasksLiveStatusService.Broadcast(ScheduledTaskStatusLive.FromScheduledTaskStatus(this, ChangedProperties));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public class ScheduledTaskStatusLive
|
||||
{
|
||||
public string TaskName { get; set; }
|
||||
public string SessionId { get; set; }
|
||||
|
||||
public byte Progress { get; set; }
|
||||
public string CurrentProcess { get; set; }
|
||||
public string CurrentDescription { get; set; }
|
||||
|
||||
public string TaskExceptionMessage { get; set; }
|
||||
public bool CancelSupported { get; set; }
|
||||
public bool IsCancelling { get; set; }
|
||||
|
||||
public bool IsRunning { get; set; }
|
||||
public DateTime? StartedTimestamp { get; set; }
|
||||
public DateTime? FinishedTimestamp { get; set; }
|
||||
public DateTime? NextScheduledTimestamp { get; set; }
|
||||
|
||||
public string FinishedMessage { get; set; }
|
||||
public string FinishedUrl { get; set; }
|
||||
|
||||
public int StatusVersion { get; set; }
|
||||
|
||||
public string[] ChangedProperties { get; set; }
|
||||
|
||||
public static ScheduledTaskStatusLive FromScheduledTaskStatus(ScheduledTaskStatus Status, string[] ChangedProperties)
|
||||
{
|
||||
return new ScheduledTaskStatusLive()
|
||||
{
|
||||
TaskName = Status.TaskName,
|
||||
SessionId = Status.SessionId,
|
||||
Progress = Status.Progress,
|
||||
CurrentProcess = Status.CurrentProcess,
|
||||
CurrentDescription = Status.CurrentDescription,
|
||||
CancelSupported = Status.CancelSupported,
|
||||
TaskExceptionMessage = (Status.TaskException == null ? null : Status.TaskException.Message),
|
||||
IsCancelling = Status.IsCanceling,
|
||||
IsRunning = Status.IsRunning,
|
||||
StartedTimestamp = Status.StartedTimestamp,
|
||||
FinishedTimestamp = Status.FinishedTimestamp,
|
||||
NextScheduledTimestamp = Status.NextScheduledTimestamp,
|
||||
FinishedMessage = Status.FinishedMessage,
|
||||
FinishedUrl = Status.FinishedUrl,
|
||||
StatusVersion = Status.StatusVersion,
|
||||
ChangedProperties = ChangedProperties
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Disco.Data.Repository;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public static class ScheduledTasks
|
||||
{
|
||||
internal const string SchedulerGroupName = "DiscoScheduledTasks";
|
||||
private static IScheduler _TaskScheduler;
|
||||
|
||||
private static object _RunningTasksLock = new object();
|
||||
private static List<ScheduledTaskStatus> _RunningTasks = new List<ScheduledTaskStatus>();
|
||||
|
||||
public static void InitalizeScheduledTasks(DiscoDataContext dbContext, ISchedulerFactory SchedulerFactory)
|
||||
{
|
||||
ScheduledTasksLog.LogInitializingScheduledTasks();
|
||||
|
||||
try
|
||||
{
|
||||
_TaskScheduler = SchedulerFactory.GetScheduler();
|
||||
_TaskScheduler.Start();
|
||||
|
||||
// Scheduled Cleanup
|
||||
ScheduledTaskCleanup.Schedule(_TaskScheduler);
|
||||
|
||||
// Discover DiscoScheduledTask
|
||||
var appDomain = AppDomain.CurrentDomain;
|
||||
|
||||
var scheduledTaskTypes = (from a in appDomain.GetAssemblies()
|
||||
where !a.GlobalAssemblyCache && !a.IsDynamic
|
||||
from type in a.GetTypes()
|
||||
where typeof(ScheduledTask).IsAssignableFrom(type) && !type.IsAbstract
|
||||
select type);
|
||||
foreach (Type scheduledTaskType in scheduledTaskTypes)
|
||||
{
|
||||
ScheduledTask instance = (ScheduledTask)Activator.CreateInstance(scheduledTaskType);
|
||||
try
|
||||
{
|
||||
instance.InitalizeScheduledTask(dbContext);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ScheduledTasksLog.LogInitializeException(ex, scheduledTaskType);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ScheduledTasksLog.LogInitializeException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static ScheduledTaskStatus GetTaskStatus(string TaskSessionId)
|
||||
{
|
||||
return _RunningTasks.Where(t => t.SessionId == TaskSessionId).FirstOrDefault();
|
||||
}
|
||||
public static List<ScheduledTaskStatus> GetTaskStatuses(Type TaskType)
|
||||
{
|
||||
return _RunningTasks.Where(t => t.TaskType == TaskType).ToList();
|
||||
}
|
||||
public static List<ScheduledTaskStatus> GetTaskStatuses()
|
||||
{
|
||||
return _RunningTasks.ToList();
|
||||
}
|
||||
|
||||
public static ScheduledTaskStatus RegisterTask(ScheduledTask Task)
|
||||
{
|
||||
return RegisterTask(Task, null);
|
||||
}
|
||||
public static ScheduledTaskStatus RegisterTask(ScheduledTask Task, TriggerBuilder TaskBuilder)
|
||||
{
|
||||
var sessionId = Guid.NewGuid().ToString("D");
|
||||
var triggerKey = GenerateTriggerKey();
|
||||
var taskType = Task.GetType();
|
||||
|
||||
var taskStatus = new ScheduledTaskStatus(Task, sessionId, triggerKey.Name);
|
||||
|
||||
lock (_RunningTasksLock)
|
||||
{
|
||||
if (Task.SingleInstanceTask)
|
||||
{
|
||||
var existingGuid = _RunningTasks.Where(t => t.IsRunning && t.TaskType == taskType).Select(t => t.SessionId).FirstOrDefault();
|
||||
if (existingGuid != null)
|
||||
throw new InvalidOperationException(string.Format("This Single-Instance Task is already running, SessionId: {0}", existingGuid));
|
||||
}
|
||||
_RunningTasks.Add(taskStatus);
|
||||
}
|
||||
|
||||
if (TaskBuilder != null)
|
||||
{
|
||||
ITrigger trigger = TaskBuilder.WithIdentity(triggerKey).Build();
|
||||
IJobDetail jobDetails = new JobDetailImpl(sessionId, taskType)
|
||||
{
|
||||
Description = Task.TaskName,
|
||||
JobDataMap = trigger.JobDataMap
|
||||
};
|
||||
|
||||
_TaskScheduler.ScheduleJob(jobDetails, trigger);
|
||||
|
||||
var nextTriggerTime = trigger.GetNextFireTimeUtc();
|
||||
if (nextTriggerTime.HasValue)
|
||||
taskStatus.SetNextScheduledTimestamp(nextTriggerTime.Value.LocalDateTime);
|
||||
}
|
||||
|
||||
return taskStatus;
|
||||
}
|
||||
public static bool UnregisterTask(this ScheduledTask Task)
|
||||
{
|
||||
lock (_RunningTasksLock)
|
||||
{
|
||||
if (_RunningTasks.Contains(Task.Status))
|
||||
{
|
||||
//_RunningTasks.Remove(Task.Status);
|
||||
_TaskScheduler.UnscheduleJob(Task.ExecutionContext.Trigger.Key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static bool UnregisterTask(this ScheduledTaskStatus TaskStatus)
|
||||
{
|
||||
lock (_RunningTasksLock)
|
||||
{
|
||||
if (_RunningTasks.Contains(TaskStatus))
|
||||
{
|
||||
//_RunningTasks.Remove(Task.Status);
|
||||
_TaskScheduler.UnscheduleJob(new TriggerKey(TaskStatus.TriggerKey, SchedulerGroupName));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static TriggerKey GenerateTriggerKey()
|
||||
{
|
||||
return new TriggerKey(Guid.NewGuid().ToString("D"), SchedulerGroupName);
|
||||
}
|
||||
|
||||
public static ScheduledTaskStatus GetDiscoScheduledTaskStatus(this IJobExecutionContext context)
|
||||
{
|
||||
return GetTaskStatus(context.JobDetail.Key.Name);
|
||||
}
|
||||
|
||||
private class ScheduledTaskCleanup : IJob
|
||||
{
|
||||
public void Execute(IJobExecutionContext context)
|
||||
{
|
||||
lock (ScheduledTasks._RunningTasksLock)
|
||||
{
|
||||
// Lifetime = 5mins
|
||||
var expiredTime = DateTime.Now.AddMinutes(-1);
|
||||
var expiredTasks = ScheduledTasks._RunningTasks.Where(
|
||||
t => !t.IsRunning &&
|
||||
!t.NextScheduledTimestamp.HasValue &&
|
||||
t.FinishedTimestamp < expiredTime
|
||||
).ToArray();
|
||||
|
||||
foreach (var expiredTask in expiredTasks)
|
||||
ScheduledTasks._RunningTasks.Remove(expiredTask);
|
||||
}
|
||||
}
|
||||
public static void Schedule(IScheduler TaskScheduler)
|
||||
{
|
||||
// Next 10min interval
|
||||
DateTime now = DateTime.Now;
|
||||
int mins = (10 - (now.Minute % 10));
|
||||
if (mins < 2)
|
||||
mins += 10;
|
||||
DateTimeOffset startAt = new DateTimeOffset(now).AddMinutes(mins).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
|
||||
ITrigger trigger = TriggerBuilder.Create()
|
||||
.StartAt(startAt)
|
||||
.WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(10))
|
||||
.WithIdentity("ScheduledTaskCleanupTrigger", ScheduledTasks.SchedulerGroupName + "_System")
|
||||
.Build();
|
||||
|
||||
IJobDetail job = JobBuilder.Create<ScheduledTaskCleanup>()
|
||||
.WithIdentity("ScheduledTaskCleanupJob", ScheduledTasks.SchedulerGroupName + "_System")
|
||||
.Build();
|
||||
|
||||
_TaskScheduler.ScheduleJob(job, trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SignalR;
|
||||
using SignalR.Hosting.AspNet;
|
||||
using SignalR.Infrastructure;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public class ScheduledTasksLiveStatusService : PersistentConnection
|
||||
{
|
||||
|
||||
protected override System.Threading.Tasks.Task OnReceivedAsync(IRequest request, string connectionId, string data)
|
||||
{
|
||||
// Add to Group
|
||||
if (!string.IsNullOrWhiteSpace(data) && data.StartsWith("/addToGroups:") && data.Length > 13)
|
||||
{
|
||||
var groups = data.Substring(13).Split(',');
|
||||
foreach (var g in groups)
|
||||
{
|
||||
this.Groups.Add(connectionId, g);
|
||||
}
|
||||
}
|
||||
|
||||
return base.OnReceivedAsync(request, connectionId, data);
|
||||
}
|
||||
|
||||
internal static void Broadcast(ScheduledTaskStatusLive SessionStatus)
|
||||
{
|
||||
//var message = Models.LogLiveEvent.Create(logModule, eventType, Timestamp, Arguments);
|
||||
|
||||
var connectionManager = GlobalHost.ConnectionManager; //AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
|
||||
var connectionContext = connectionManager.GetConnectionContext<ScheduledTasksLiveStatusService>();
|
||||
connectionContext.Groups.Send(_GroupNameAll, SessionStatus);
|
||||
connectionContext.Groups.Send(SessionStatus.SessionId, SessionStatus);
|
||||
}
|
||||
|
||||
private const string _GroupNameAll = "__All";
|
||||
//private static string _QualifiedSessionName = typeof(ScheduledTasksLiveStatusService).FullName + ".";
|
||||
//private static string _QualifiedSessionNameAll = _QualifiedSessionName + "__All";
|
||||
//private static string LiveStatusGroup(string SessionId)
|
||||
//{
|
||||
// return string.Concat(_QualifiedSessionName, SessionId);
|
||||
//}
|
||||
public static string LiveStatusAll
|
||||
{
|
||||
get
|
||||
{
|
||||
//return _QualifiedTypeNameAll;
|
||||
return _GroupNameAll;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Services.Logging;
|
||||
using Disco.Services.Logging.Models;
|
||||
|
||||
namespace Disco.Services.Tasks
|
||||
{
|
||||
public class ScheduledTasksLog : LogBase
|
||||
{
|
||||
private const int _ModuleId = 20;
|
||||
|
||||
public override string ModuleDescription { get { return "Scheduled Tasks"; } }
|
||||
public override int ModuleId { get { return _ModuleId; } }
|
||||
public override string ModuleName { get { return "ScheduledTasks"; } }
|
||||
|
||||
public enum EventTypeIds
|
||||
{
|
||||
InitializingScheduledTasks = 10,
|
||||
InitializeException = 15,
|
||||
InitializeExceptionWithInner,
|
||||
InitializeScheduledTasksException,
|
||||
InitializeScheduledTasksExceptionWithInner,
|
||||
ScheduledTasksException = 30,
|
||||
ScheduledTasksExceptionWithInner,
|
||||
ScheduledTaskExecuted = 50,
|
||||
ScheduledTaskFinished = 80,
|
||||
}
|
||||
public static ScheduledTasksLog Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ScheduledTasksLog)LogContext.LogModules[_ModuleId];
|
||||
}
|
||||
}
|
||||
private static void Log(EventTypeIds EventTypeId, params object[] Args)
|
||||
{
|
||||
Current.Log((int)EventTypeId, Args);
|
||||
}
|
||||
|
||||
public static void LogInitializingScheduledTasks()
|
||||
{
|
||||
Current.Log((int)EventTypeIds.InitializingScheduledTasks);
|
||||
}
|
||||
public static void LogScheduledTaskExecuted(string TaskName, string SessionId)
|
||||
{
|
||||
Current.Log((int)EventTypeIds.ScheduledTaskExecuted, TaskName, SessionId);
|
||||
}
|
||||
public static void LogScheduledTaskFinished(string TaskName, string SessionId)
|
||||
{
|
||||
Current.Log((int)EventTypeIds.ScheduledTaskFinished, TaskName, SessionId);
|
||||
}
|
||||
|
||||
public static void LogInitializeException(Exception ex)
|
||||
{
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Log(EventTypeIds.InitializeExceptionWithInner, ex.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log(EventTypeIds.InitializeException, ex.GetType().Name, ex.Message, ex.StackTrace);
|
||||
}
|
||||
}
|
||||
public static void LogInitializeException(Exception ex, Type ScheduledTaskType)
|
||||
{
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Log(EventTypeIds.InitializeScheduledTasksExceptionWithInner, ScheduledTaskType.Name, ScheduledTaskType.Assembly.Location, ex.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log(EventTypeIds.InitializeScheduledTasksException, ScheduledTaskType.Name, ScheduledTaskType.Assembly.Location, ex.GetType().Name, ex.Message, ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
public static void LogScheduledTaskException(string ScheduledTaskName, string SessionId, Type ScheduledTaskType, Exception ex)
|
||||
{
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Log(EventTypeIds.ScheduledTasksExceptionWithInner, ScheduledTaskName, SessionId, ScheduledTaskType.Assembly.Location, ex.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log(EventTypeIds.ScheduledTasksException, ScheduledTaskName, SessionId, ScheduledTaskType.Assembly.Location, ex.GetType().Name, ex.Message, ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
protected override List<Logging.Models.LogEventType> LoadEventTypes()
|
||||
{
|
||||
return new System.Collections.Generic.List<LogEventType>
|
||||
{
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.InitializingScheduledTasks,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Initializing Scheduled Tasks",
|
||||
Format = "Starting Scheduled Task discovery and initialization",
|
||||
Severity = (int)LogEventType.Severities.Information,
|
||||
UseLive = false,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.InitializeException,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Initialize Exception",
|
||||
Format = "Exception: {0}: {1}; {2}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = false,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.InitializeExceptionWithInner,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Initialize Exception with Inner Exception",
|
||||
Format = "Exception: {0}: {1}; {2}; Inner: {3}: {4}; {5}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = false,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.InitializeScheduledTasksException,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Initialize Task Exception",
|
||||
Format = "[{0}] At '{1}'; Exception: {2}: {3}; {4}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = false,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.InitializeScheduledTasksExceptionWithInner,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Initialize Task Exception with Inner Exception",
|
||||
Format = "[{0}] At '{1}'; Exception: {2}: {3}; {4}; Inner: {5}: {6}; {7}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = false,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.ScheduledTasksException,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Scheduled Task Exception",
|
||||
Format = "Task Name: {0}; SessionId: {1}; At: '{2}'; Exception: {3}: {4}; {5}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = true,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.ScheduledTasksExceptionWithInner,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Scheduled Task Exception with Inner Exception",
|
||||
Format = "Task Name: {0}; SessionId: {1}; At: '{2}'; Exception: {3}: {4}; {5}; Inner: {6}: {7}; {8}",
|
||||
Severity = (int)LogEventType.Severities.Error,
|
||||
UseLive = true,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.ScheduledTaskExecuted,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Scheduled Task Started",
|
||||
Format = "Scheduled Task Started: {0}; Session Id: {1}",
|
||||
Severity = (int)LogEventType.Severities.Information,
|
||||
UseLive = true,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
},
|
||||
new LogEventType
|
||||
{
|
||||
Id = (int)EventTypeIds.ScheduledTaskFinished,
|
||||
ModuleId = _ModuleId,
|
||||
Name = "Scheduled Task Finished",
|
||||
Format = "Scheduled Task Finished: {0}; Session Id: {1}",
|
||||
Severity = (int)LogEventType.Severities.Information,
|
||||
UseLive = true,
|
||||
UsePersist = true,
|
||||
UseDisplay = true
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user