using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.Integration.Operation;
namespace Tango.Integration.JobRuns
{
///
/// Represents a basic database job runs logger.
///
///
public class BasicJobRunsLogger : ExtendedObject, IJobRunsLogger
{
private DateTime _start_date;
private Job _job;
#region Properties
///
/// Gets the machine operator.
///
public IMachineOperator MachineOperator { get; private set; }
///
/// Gets a value indicating whether this instance is started.
///
public bool IsStarted { get; private set; }
///
/// Gets or sets the job designations of which the logger should log (supports multiple flags).
///
public JobDesignations JobDesignation { get; set; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The machine operator.
public BasicJobRunsLogger(IMachineOperator machineOperator)
{
JobDesignation = JobDesignations.Default;
MachineOperator = machineOperator;
Init();
}
#endregion
#region Private Methods
///
/// Initializes this instance.
///
private void Init()
{
MachineOperator.PrintingStarted -= Machine_PrintingStarted;
MachineOperator.PrintingStarted += Machine_PrintingStarted;
MachineOperator.PrintingCompleted -= Machine_PrintingCompleted;
MachineOperator.PrintingCompleted += Machine_PrintingCompleted;
MachineOperator.PrintingAborted -= Machine_PrintingAborted;
MachineOperator.PrintingAborted += Machine_PrintingAborted;
MachineOperator.PrintingFailed -= Machine_PrintingFailed;
MachineOperator.PrintingFailed += Machine_PrintingFailed;
}
private bool ShouldLog()
{
return IsStarted && _job != null && JobDesignation.HasFlag(_job.Designation);
}
#endregion
#region Public Methods
///
/// Starts the logger.
///
public void Start()
{
IsStarted = true;
}
///
/// Stops the logger.
///
public void Stop()
{
IsStarted = false;
}
#endregion
#region Event Handlers
private void Machine_PrintingFailed(object sender, PrintingFailedEventArgs e)
{
if (ShouldLog())
{
if (e.Job.Guid == _job.Guid)
{
Task.Factory.StartNew(() =>
{
try
{
using (var db = ObservablesContext.CreateDefault())
{
db.JobRuns.Add(new JobRun()
{
StartDate = _start_date,
EndDate = DateTime.UtcNow,
JobGuid = _job.Guid,
JobRunStatus = JobRunStatus.Failed,
EndPosition = e.JobHandler.Status.Progress,
FailedMessage = e.Exception.Message,
});
e.Job.LastRun = DateTime.UtcNow;
_job.LastRun = DateTime.UtcNow;
var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid);
if (job != null)
{
job.LastRun = DateTime.UtcNow;
}
db.SaveChanges();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error logging the current job run to the database.");
}
});
}
}
}
private void Machine_PrintingAborted(object sender, PrintingEventArgs e)
{
if (ShouldLog())
{
if (e.Job.Guid == _job.Guid)
{
Task.Factory.StartNew(() =>
{
try
{
using (var db = ObservablesContext.CreateDefault())
{
db.JobRuns.Add(new JobRun()
{
StartDate = _start_date,
EndDate = DateTime.UtcNow,
JobGuid = _job.Guid,
EndPosition = e.JobHandler.Status.Progress,
JobRunStatus = JobRunStatus.Aborted,
});
e.Job.LastRun = DateTime.UtcNow;
_job.LastRun = DateTime.UtcNow;
var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid);
if (job != null)
{
job.LastRun = DateTime.UtcNow;
}
db.SaveChanges();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error logging the current job run to the database.");
}
});
}
}
}
private void Machine_PrintingCompleted(object sender, PrintingEventArgs e)
{
if (ShouldLog())
{
if (e.Job.Guid == _job.Guid)
{
Task.Factory.StartNew(() =>
{
try
{
using (var db = ObservablesContext.CreateDefault())
{
db.JobRuns.Add(new JobRun()
{
StartDate = _start_date,
EndDate = DateTime.UtcNow,
JobGuid = _job.Guid,
EndPosition = e.JobHandler.Status.Progress,
JobRunStatus = JobRunStatus.Completed,
});
e.Job.LastRun = DateTime.UtcNow;
_job.LastRun = DateTime.UtcNow;
var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid);
if (job != null)
{
job.LastRun = DateTime.UtcNow;
}
db.SaveChanges();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error logging the current job run to the database.");
}
});
}
}
}
private void Machine_PrintingStarted(object sender, PrintingEventArgs e)
{
_job = e.Job;
_start_date = DateTime.UtcNow;
}
#endregion
}
}