using System;
|
using Autofac;
|
|
namespace PalGain.Core.Tasks
|
{
|
/// <summary>
|
/// Task
|
/// </summary>
|
public partial class Task
|
{
|
/// <summary>
|
/// Ctor for Task
|
/// </summary>
|
private Task()
|
{
|
this.Enabled = true;
|
}
|
|
/// <summary>
|
/// Ctor for Task
|
/// </summary>
|
/// <param name="task">Task </param>
|
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;
|
}
|
|
/// <summary>
|
/// Executes the task
|
/// </summary>
|
/// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param>
|
/// <param name="dispose">A value indicating whether all instances hsould be disposed after task run</param>
|
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<IScheduleTaskService>("", 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<ILogger>("", 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;
|
}
|
|
/// <summary>
|
/// A value indicating whether a task is running
|
/// </summary>
|
public bool IsRunning { get; private set; }
|
|
/// <summary>
|
/// Datetime of the last start
|
/// </summary>
|
public DateTime? LastStartUtc { get; private set; }
|
|
/// <summary>
|
/// Datetime of the last end
|
/// </summary>
|
public DateTime? LastEndUtc { get; private set; }
|
|
/// <summary>
|
/// Datetime of the last success
|
/// </summary>
|
public DateTime? LastSuccessUtc { get; private set; }
|
|
/// <summary>
|
/// A value indicating type of the task
|
/// </summary>
|
public string Type { get; private set; }
|
|
/// <summary>
|
/// A value indicating whether to stop task on error
|
/// </summary>
|
public bool StopOnError { get; private set; }
|
|
/// <summary>
|
/// Get the task name
|
/// </summary>
|
public string Name { get; private set; }
|
|
/// <summary>
|
/// A value indicating whether the task is enabled
|
/// </summary>
|
public bool Enabled { get; set; }
|
}
|
}
|