ln.skyscanner/checks/CheckJob.cs

95 lines
2.8 KiB
C#

// /**
// * File: CheckTask.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using ln.types.threads;
using ln.skyscanner.entities;
using ln.logging;
using Newtonsoft.Json;
using ln.types.odb;
using System.Linq;
using System.Collections.Generic;
namespace ln.skyscanner.checks
{
public class CheckJob : PoolJob
{
[JsonIgnore]
public Node Node { get; }
Dictionary<string, SkyCheckState> checkStates = new Dictionary<string, SkyCheckState>();
public CheckJob(Node node)
{
Name = String.Format("Interval check: {0} [{1}]",node.UniqueIdentity,node.PrimaryIP.ToString());
Node = node;
Query stateQuery = Query.Equals<SkyCheckState>("Node.ID", Node.ID);
SkyCheckState[] skyCheckStates = SkyScanner.Instance.Entities.SkyCheckStates.Query(stateQuery).ToArray();
foreach (SkyCheckState checkState in skyCheckStates)
{
checkStates.Add(checkState.CheckName, checkState);
}
}
public override void RunJob()
{
SkyCheck[] checks = SkyCheck.SkyChecks;
for (int n=0;n<checks.Length;n++)
{
setState("current check: {0}", checks[n].Name);
Progress = (double)n / (double)checks.Length;
if (checks[n].IsValid(Node))
{
try
{
SkyCheckState checkState = checkStates.ContainsKey(checks[n].Name) ? checkStates[checks[n].Name] : null;
if (checkState != null)
{
checkState.LastCheckTime = DateTime.Now;
}
checks[n].Check(SkyScanner.Instance.Checker, ref checkState, Node);
if (checkState != null)
SkyScanner.Instance.Checker.Save(checkState);
} catch (Exception e)
{
Logging.Log(e);
if (checks[n].IsCritical)
break;
}
}
else if (checks[n].IsCritical)
{
break;
}
}
}
public override int GetHashCode()
{
return Node.UniqueIdentity.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is CheckJob)
{
return Node.UniqueIdentity.Equals((obj as CheckJob).Node.UniqueIdentity);
}
return false;
}
}
}