ln.skyscanner/SkyScanner.cs

258 lines
8.8 KiB
C#
Raw Permalink Normal View History

2019-03-13 14:18:05 +01:00
using System;
using ln.logging;
using System.IO;
2019-03-14 13:31:15 +01:00
using ln.types.threads;
2019-08-03 12:57:32 +02:00
using ln.types.net;
using ln.application;
using ln.application.service;
using ln.skyscanner.services;
using System.Threading;
using ln.http.resources;
using ln.types.rpc;
2019-08-19 14:15:10 +02:00
using ln.json;
2019-03-13 14:18:05 +01:00
namespace ln.skyscanner
{
2019-03-15 15:35:44 +01:00
public enum ComponentState { STOPPED, INITIALIZED, STARTED, FAILED, STOPPING }
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
public class SkyScanner : Application
2019-03-13 14:18:05 +01:00
{
2019-03-26 12:53:42 +01:00
public static SkyScanner Instance { get; private set; }
2019-03-13 14:18:05 +01:00
public String BasePath { get; private set; }
2019-03-14 13:31:15 +01:00
public Pool ConveniencePool { get; }
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
public SkyScanner()
2019-03-13 14:18:05 +01:00
{
2019-08-03 12:57:32 +02:00
if (Instance != null)
throw new NotSupportedException("only one SkyScanner may be created");
Instance = this;
2019-03-18 08:12:54 +01:00
2019-08-03 12:57:32 +02:00
Arguments
.Add(0, "base-path", Path.GetFullPath("/var/cache/ln.skyscanner"))
.Add(0, "import-skytron", null)
.Add(0, "benchmark", null)
.Add(0, "debug-check", null)
.Add("disable-checker")
.Add(0, "crawl-vendor", null)
.Add(0, "crawl", null)
;
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
Logging.Log(LogLevel.INFO, "SkyScanner: Constructor");
BasePath = Arguments["base-path"].Value;
2019-03-13 14:18:05 +01:00
Logging.Log(LogLevel.INFO, "SkyScanner: BasePath={0}", BasePath);
2019-08-03 12:57:32 +02:00
ConveniencePool = new Pool();
}
2019-04-04 19:34:19 +02:00
2019-08-03 12:57:32 +02:00
public override void PrepareStart()
{
base.PrepareStart();
2019-06-28 10:01:30 +02:00
2019-08-29 13:14:52 +02:00
FileLogger fileLogger = new FileLogger("skyscanner.log");
fileLogger.MaxLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), Arguments["log-level"].Value);
Logger.Default.Backends.Add(fileLogger);
2019-08-03 12:57:32 +02:00
if (Arguments["import-skytron"].IsSet)
2019-06-28 10:01:30 +02:00
{
2019-08-30 07:16:54 +02:00
//SkytronImport si = new SkytronImport(Arguments["import-skytron"].Value);
//si.Import();
2019-06-28 10:01:30 +02:00
}
2019-08-03 12:57:32 +02:00
if (Arguments["benchmark"].IsSet)
2019-07-03 10:54:46 +02:00
{
2019-08-03 12:57:32 +02:00
switch (Arguments["benchmark"].Value)
2019-07-03 10:54:46 +02:00
{
case "odb":
BenchmarkODB();
break;
default:
2019-08-03 12:57:32 +02:00
Logging.Log(LogLevel.ERROR, "Unknown Benchmark: {0}", Arguments["benchmark"].Value);
2019-07-03 10:54:46 +02:00
break;
}
throw new Exception("Quitting after benchmarking");
}
2019-08-03 12:57:32 +02:00
/*
if (crawlHost != null)
{
CrawlHost(crawlHost);
throw new Exception("Quitting after --crawl");
}
if (debugCheckNode != null)
{
DebugCheck(debugCheckNode);
throw new Exception("Quitting after --debug-check");
}
*/
ServiceContainer.Add(ServiceDefinition.From<Service>(true));
ServiceContainer.Add(ServiceDefinition.From<EntityService>(true));
ServiceContainer.Add(ServiceDefinition.From<CheckService>(true));
ServiceContainer.Add(ServiceDefinition.From<CrawlService>(true));
2019-08-29 13:14:52 +02:00
ServiceContainer.Add(ServiceDefinition.From<PerformanceValueService>(true));
2019-03-13 14:18:05 +01:00
}
2019-08-03 12:57:32 +02:00
public class Service : ApplicationServiceBase
2019-03-13 14:18:05 +01:00
{
2019-08-30 07:16:54 +02:00
public ApplicationWebSocket webSocketInterface { get; private set; }
2019-08-03 12:57:32 +02:00
public RPCContainer RPCContainer { get; private set; }
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
public Service()
: base("SkyScanner Application Service")
2019-03-18 08:12:54 +01:00
{
2019-08-03 12:57:32 +02:00
RPCContainer = new RPCContainer();
2019-03-18 08:12:54 +01:00
}
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
public override void ServiceMain(IApplicationInterface applicationInterface)
{
2019-09-16 20:29:52 +02:00
DirectoryResource staticTemplates = new DirectoryResource(new string[] { "../../www", "www" });
2019-03-18 08:12:54 +01:00
2019-08-03 12:57:32 +02:00
staticTemplates.ResourceTypeHook = ResourceTypeHook;
staticTemplates.DefaultResource = staticTemplates.GetResource("index.html");
2019-09-16 20:29:52 +02:00
staticTemplates.FallBackResource = staticTemplates.DefaultResource;
staticTemplates.GetResource("vue").DefaultResource = staticTemplates.GetResource("vue").GetResource("vue.html");
staticTemplates.GetResource("vue").FallBackResource = staticTemplates.GetResource("vue").DefaultResource;
2019-03-18 08:12:54 +01:00
2019-09-16 20:29:52 +02:00
applicationInterface.HTTPApplication.RootResource = staticTemplates;
2019-03-13 14:18:05 +01:00
2019-09-16 20:29:52 +02:00
webSocketInterface = new ApplicationWebSocket(null, "socket", RPCContainer);
staticTemplates.InjectResource(webSocketInterface);
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
RPCContainer.Add("", new SkyScannerRpc(this));
2019-08-06 00:26:53 +02:00
RPCContainer.Add("ServiceContainer",applicationInterface.ServiceContainer.RPC);
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
long nextCycle = DateTimeOffset.Now.ToUnixTimeMilliseconds();
2019-03-13 14:18:05 +01:00
2019-08-06 00:26:53 +02:00
Ready();
2019-08-03 12:57:32 +02:00
while (!StopRequested)
{
while ((nextCycle - DateTimeOffset.Now.ToUnixTimeMilliseconds()) < 0)
nextCycle += 1000;
try
{
int timeOut = (int)(nextCycle - DateTimeOffset.Now.ToUnixTimeMilliseconds());
if (timeOut > 0)
Thread.Sleep(timeOut);
}
catch (ThreadInterruptedException)
{
if (StopRequested)
break;
}
/* Send Application State to WebSockets */
2019-08-19 14:15:10 +02:00
JSONObject msg = new JSONObject();
JSONObject stateObject = new JSONObject();
stateObject.Add("currentTime", DateTimeOffset.Now.ToUnixTimeMilliseconds());
2019-08-03 12:57:32 +02:00
msg["state"] = stateObject;
webSocketInterface.Broadcast(msg);
2019-03-13 14:18:05 +01:00
}
2019-08-03 12:57:32 +02:00
}
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
private Resource ResourceTypeHook(DirectoryResource directoryResource, FileInfo fileInfo)
{
if (fileInfo.Extension.Equals(".html"))
return new TemplateResource(directoryResource, fileInfo.FullName);
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
return null;
}
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
class SkyScannerRpc
2019-03-13 14:18:05 +01:00
{
2019-08-03 12:57:32 +02:00
Service skyscannerService;
public SkyScannerRpc(Service skyscannerService)
2019-03-13 14:18:05 +01:00
{
2019-08-03 12:57:32 +02:00
this.skyscannerService = skyscannerService;
2019-03-13 14:18:05 +01:00
}
2019-08-03 12:57:32 +02:00
public string GetServerString()
2019-03-14 08:36:02 +01:00
{
2019-08-03 12:57:32 +02:00
return "SkyScanner 0.1a";
2019-03-14 08:36:02 +01:00
}
2019-03-13 14:18:05 +01:00
2019-08-30 12:40:28 +02:00
public void Shutdown()
{
ThreadPool.QueueUserWorkItem((state) => ((Application)this.skyscannerService.CurrentApplicationInterface).Stop());
}
2019-08-03 12:57:32 +02:00
2019-08-06 00:26:53 +02:00
2019-03-14 13:31:15 +01:00
}
2019-08-03 12:57:32 +02:00
2019-03-14 13:31:15 +01:00
}
2019-07-03 10:54:46 +02:00
public void BenchmarkODB()
{
2019-08-30 07:16:54 +02:00
//int maxIterations = 10000;
2019-07-03 10:54:46 +02:00
2019-08-30 07:16:54 +02:00
//for (int i = 0; i < maxIterations; i++)
//{
// int n = 0;
// Logging.Log(LogLevel.INFO, "Benchmark ODB: Iteration {0} of {1}", i, maxIterations);
2019-07-03 10:54:46 +02:00
2019-08-30 07:16:54 +02:00
// foreach (SkyCheckState checkState in SkyScanner.Instance.Entities.SkyCheckStates.ToArray())
// {
// SkyScanner.Instance.Entities.SkyCheckStates.Upsert(checkState);
// n++;
// }
2019-07-03 10:54:46 +02:00
2019-08-30 07:16:54 +02:00
// Logging.Log(LogLevel.INFO, "Saved {0} SkyCheckStates", n);
2019-03-14 13:31:15 +01:00
2019-08-30 07:16:54 +02:00
//}
2019-07-03 10:54:46 +02:00
}
2019-03-13 14:18:05 +01:00
2019-08-03 12:57:32 +02:00
public void CrawlHost(IPv4 ip)
{
2019-08-30 07:16:54 +02:00
//Logger.ConsoleLogger.MaxLogLevel = LogLevel.DEBUGFULL;
2019-08-03 12:57:32 +02:00
2019-08-30 07:16:54 +02:00
//CrawledHost crawledHost = new CrawledHost();
//crawledHost.Name = ip.ToString();
//crawledHost.PrimaryIP = ip;
//crawledHost.IPAddresses = new IPv4[] { ip };
2019-08-03 12:57:32 +02:00
2019-08-30 07:16:54 +02:00
//Crawl crawl = new Crawl(Crawler, crawledHost);
//crawl.Prepare();
//crawl.RunJob();
2019-08-03 12:57:32 +02:00
2019-08-30 07:16:54 +02:00
//Logging.Log(LogLevel.INFO, "CrawledHost: {0}", JSONObject.From(crawledHost).ToString());
2019-08-03 12:57:32 +02:00
}
2019-07-08 13:14:49 +02:00
public void DebugCheck(String uniqueID)
{
2019-08-30 07:16:54 +02:00
//Logger.ConsoleLogger.MaxLogLevel = LogLevel.DEBUGFULL;
//Node node = Entities.NodeCollection.Query("uniqueIdentity", uniqueID).FirstOrDefault();
//if (node == null)
//{
// Logging.Log(LogLevel.INFO, "DebugCheck(): Node not found: uniqueIdentity={0}", uniqueID);
//}
//else
//{
// Logging.Log(LogLevel.INFO, "DebugCheck(): Node: {0}",JSONObject.From(node).ToString());
// CheckJob checkJob = new CheckJob(null, node);
// Logging.Log(LogLevel.INFO, "Prepare...");
// checkJob.Prepare();
// Logging.Log(LogLevel.INFO, "Check...");
// checkJob.RunJob();
//}
2019-07-08 13:14:49 +02:00
}
2019-03-13 14:18:05 +01:00
}
}