using Microsoft.Azure.Management.AppService.Fluent;
using System;
using System.Collections.Generic;
usingusing 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
{
/// <summary>
/// Represents a basic database job runs logger.
/// </summary>
/// <seealso cref="Tango.Integration.JobRuns.IJobRunsLogger" />
public class BasicJobRunsLogger : ExtendedObject, IJobRunsLogger
{
private DateTime _start_date;
private Job _job;
#region Properties
/// <summary>
/// Gets the machine operator.
/// </summary>
public IMachineOperator MachineOperator { get; private set; }
/// <summary>
/// Gets a value indicating whether this instance is started.
/// </summary>
public bool IsStarted { get; private set; }
/// <summary>
/// Gets or sets the job designations of which the logger should log (supports multiple flags).
/// </summary>
public JobDesignations JobDesignation { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="BasicJobRunsLogger"/> class.
/// </summary>
/// <param name="machineOperator">The machine operator.</param>
public BasicJobRunsLogger(IMachineOperator machineOperator)
{
JobDesignation = JobDesignations.Default;
MachineOperator = machineOperator;
Init();
}
#endregion
#region Private Methods
/// <summary>
/// Initializes this instance.
/// </summary>
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
/// <summary>
/// Starts the logger.
/// </summary>
public void Start()
{
IsStarted = true;
}
/// <summary>
/// Stops the logger.
/// </summary>
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
}
}