aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-12-26 14:04:34 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-12-26 14:04:34 +0200
commit3e71ab50870db524f7e17aa9d2042da52b15ad63 (patch)
treee1bee015a2ce129316bbc30a0ff6466f239b33fd /Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs
parenta756bdd61609c9bd093e81d1e2a9edf6bd912882 (diff)
downloadTango-3e71ab50870db524f7e17aa9d2042da52b15ad63.tar.gz
Tango-3e71ab50870db524f7e17aa9d2042da52b15ad63.zip
Implemented integration IJobRunsLogger.
Implemented DEBUG/RELEASE JsonController. Implemented Local/Remote machine studio setting.
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs')
-rw-r--r--Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs221
1 files changed, 221 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs
new file mode 100644
index 000000000..07844af17
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs
@@ -0,0 +1,221 @@
+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.Integration.Operation;
+
+namespace Tango.Integration.JobRuns
+{
+ /// <summary>
+ /// Represents a basic database job runs logger.
+ /// </summary>
+ /// <seealso cref="Tango.Integration.JobRuns.IJobRunsLogger" />
+ public class BasicJobRunsLogger : 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(() =>
+ {
+ 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();
+ }
+ });
+ }
+ }
+ }
+
+ private void Machine_PrintingAborted(object sender, PrintingEventArgs e)
+ {
+ if (ShouldLog())
+ {
+ if (e.Job.Guid == _job.Guid)
+ {
+ Task.Factory.StartNew(() =>
+ {
+ 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();
+ }
+ });
+ }
+ }
+ }
+
+ private void Machine_PrintingCompleted(object sender, PrintingEventArgs e)
+ {
+ if (ShouldLog())
+ {
+ if (e.Job.Guid == _job.Guid)
+ {
+ Task.Factory.StartNew(() =>
+ {
+ 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();
+ }
+ });
+ }
+ }
+ }
+
+ private void Machine_PrintingStarted(object sender, PrintingEventArgs e)
+ {
+ _job = e.Job;
+ _start_date = DateTime.UtcNow;
+ }
+
+
+
+ #endregion
+ }
+}