using System; using Autofac; namespace PalGain.Core.Tasks { /// /// Task /// public partial class Task { /// /// Ctor for Task /// private Task() { this.Enabled = true; } /// /// Ctor for Task /// /// Task public Task(ScheduleTask task) { this.Type = task.Type; this.Enabled = task.Enabled; this.StopOnError = task.StopOnError; this.Name = task.Name; } private ITask CreateTask(ILifetimeScope scope) { ITask task = null; if (this.Enabled) { var type2 = System.Type.GetType(this.Type); if (type2 != null) { object instance; if (!PalGainEngine.Instance.ContainerManager.TryResolve(type2, scope, out instance)) { //not resolved instance = PalGainEngine.Instance.ContainerManager.ResolveUnregistered(type2, scope); } task = instance as ITask; } } return task; } /// /// Executes the task /// /// A value indicating whether exception should be thrown if some error happens /// A value indicating whether all instances hsould be disposed after task run public void Execute(bool throwException = false, bool dispose = true) { this.IsRunning = true; //background tasks has an issue with Autofac //because scope is generated each time it's requested //that's why we get one single scope here //this way we can also dispose resources once a task is completed var scope = PalGainEngine.Instance.ContainerManager.Scope(); var scheduleTaskService = PalGainEngine.Instance.ContainerManager.Resolve("", scope); var scheduleTask = scheduleTaskService.GetTaskByType(this.Type); try { var task = this.CreateTask(scope); if (task != null) { this.LastStartUtc = DateTime.UtcNow; if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastStartUtc = this.LastStartUtc; scheduleTaskService.UpdateTask(scheduleTask); } //execute task task.Execute(); this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow; } } catch (Exception exc) { this.Enabled = !this.StopOnError; this.LastEndUtc = DateTime.UtcNow; //log error //var logger = PalGainEngine.Instance.ContainerManager.Resolve("", scope); //logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, exc.Message), exc); if (throwException) throw; } if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastEndUtc = this.LastEndUtc; scheduleTask.LastSuccessUtc = this.LastSuccessUtc; scheduleTaskService.UpdateTask(scheduleTask); } //dispose all resources if (dispose) { scope.Dispose(); } this.IsRunning = false; } /// /// A value indicating whether a task is running /// public bool IsRunning { get; private set; } /// /// Datetime of the last start /// public DateTime? LastStartUtc { get; private set; } /// /// Datetime of the last end /// public DateTime? LastEndUtc { get; private set; } /// /// Datetime of the last success /// public DateTime? LastSuccessUtc { get; private set; } /// /// A value indicating type of the task /// public string Type { get; private set; } /// /// A value indicating whether to stop task on error /// public bool StopOnError { get; private set; } /// /// Get the task name /// public string Name { get; private set; } /// /// A value indicating whether the task is enabled /// public bool Enabled { get; set; } } }