ln.skyscanner/checks/CheckJob.cs

104 lines
3.3 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 ln.skyscanner.services;
namespace ln.skyscanner.checks
{
public class CheckJob : PoolJob
{
public CheckService CheckService { get; }
public Node Node { get; }
public CheckJob(CheckService checkService,Node node)
{
CheckService = checkService;
Name = String.Format("Interval check: {0} [{1}]",node.UniqueIdentity,node.PrimaryIP.ToString());
Node = node;
}
public override void RunJob()
{
SkyCheck[] checks = SkyCheck.SkyChecks;
long startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
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 = null;
if (Node.ContainsCheckState(checks[n].Name))
{
checkState = Node.GetCheckState(checks[n].Name);
}
else
{
checkState = checks[n].PrepareCheckState(CheckService, Node);
Node.AddCheckState(checkState);
}
if (checkState != null)
{
checkState.LastCheckTime = DateTime.Now;
}
checks[n].Check(CheckService, ref checkState, Node);
if (checkState != null)
{
checkState.CheckHistory();
}
} catch (Exception e)
{
Logging.Log(LogLevel.WARNING, "Exception caught by CheckJob {0} [{1} / {3}]: {2}", checks[n].Name,Node.Name,e,Node.UniqueIdentity);
Logging.Log(e);
if (checks[n].IsCritical)
break;
}
}
else if (checks[n].IsCritical)
{
break;
}
}
long stopTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (CheckService.DEBUG)
Logging.Log(LogLevel.DEBUGDETAIL, "CheckTime: {0} ({1}): {2}ms", Name, Node.Name, (stopTime - startTime));
CheckService.EntityService.EnqueueUpsert(Node);
}
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;
}
}
}