Fixed Indexer SwaggerUI, Added worker & call management endpoints to Indexer, restructuring to improve project separation

This commit is contained in:
2025-07-12 22:55:56 +02:00
parent a884a2734d
commit e8e5b742d1
30 changed files with 558 additions and 189 deletions

View File

@@ -1,15 +1,125 @@
using System.Diagnostics;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Indexer.Exceptions;
namespace Indexer.Models;
public class WorkerCollection
{
public List<Worker> Workers;
public Dictionary<string, Worker> Workers;
public List<Type> types;
public WorkerCollection()
private readonly ILogger _logger;
private readonly IConfiguration _configuration;
private readonly Client.Client client;
public WorkerCollection(ILogger<WorkerCollection> logger, IConfiguration configuration, Client.Client client)
{
Workers = [];
types = [typeof(PythonScriptable)];
_logger = logger;
_configuration = configuration;
this.client = client;
}
public void InitializeWorkers()
{
_logger.LogInformation("Initializing workers");
// Load and configure all workers
var sectionMain = _configuration.GetSection("EmbeddingsearchIndexer");
if (!sectionMain.Exists())
{
_logger.LogCritical("Unable to load section \"EmbeddingsearchIndexer\"");
throw new IndexerConfigurationException("Unable to load section \"EmbeddingsearchIndexer\"");
}
WorkerCollectionConfig? sectionWorker = (WorkerCollectionConfig?)sectionMain.Get(typeof(WorkerCollectionConfig)); //GetValue<WorkerCollectionConfig>("Worker");
if (sectionWorker is not null)
{
foreach (WorkerConfig workerConfig in sectionWorker.Worker)
{
ScriptToolSet toolSet = new(workerConfig.Script, client);
InitializeWorker(toolSet, workerConfig);
}
}
else
{
_logger.LogCritical("Unable to load section \"Worker\"");
throw new IndexerConfigurationException("Unable to load section \"Worker\"");
}
_logger.LogInformation("Initialized workers");
}
public void InitializeWorker(ScriptToolSet toolSet, WorkerConfig workerConfig)
{
_logger.LogInformation("Initializing worker: {Name}", workerConfig.Name);
Worker worker = new(workerConfig.Name, workerConfig, GetScriptable(toolSet));
Workers[workerConfig.Name] = worker;
foreach (CallConfig callConfig in workerConfig.Calls)
{
_logger.LogInformation("Initializing call of type: {Type}", callConfig.Type);
switch (callConfig.Type)
{
case "interval":
if (callConfig.Interval is null)
{
_logger.LogError("Interval not set for a Call in Worker \"{Name}\"", workerConfig.Name);
throw new IndexerConfigurationException($"Interval not set for a Call in Worker \"{workerConfig.Name}\"");
}
var timer = new System.Timers.Timer((double)callConfig.Interval);
timer.AutoReset = true;
timer.Enabled = true;
DateTime now = DateTime.Now;
IntervalCall call = new(timer, worker.Scriptable, _logger, callConfig)
{
LastExecution = now,
LastSuccessfulExecution = now
};
timer.Elapsed += (sender, e) =>
{
try
{
call.LastExecution = DateTime.Now;
worker.Scriptable.Update(new IntervalCallbackInfos() { sender = sender, e = e });
call.LastSuccessfulExecution = DateTime.Now;
}
catch (Exception ex)
{
_logger.LogError("Exception occurred in a Call of Worker \"{name}\": \"{ex}\"", worker.Name, ex.Message);
}
};
worker.Calls.Add(call);
break;
case "schedule": // TODO implement scheduled tasks using Quartz
throw new NotImplementedException("schedule not implemented yet");
case "fileupdate":
if (callConfig.Path is null)
{
_logger.LogError("Path not set for a Call in Worker \"{Name}\"", workerConfig.Name);
throw new IndexerConfigurationException($"Path not set for a Call in Worker \"{workerConfig.Name}\"");
}
throw new NotImplementedException("fileupdate not implemented yet");
//break;
default:
throw new IndexerConfigurationException($"Unknown Type specified for a Call in Worker \"{workerConfig.Name}\"");
}
}
}
public IScriptable GetScriptable(ScriptToolSet toolSet)
{
string fileName = toolSet.filePath;
foreach (Type type in types)
{
IScriptable? instance = (IScriptable?)Activator.CreateInstance(type, [toolSet, _logger]);
if (instance is not null && instance.IsScript(fileName))
{
return instance;
}
}
_logger.LogError("Unable to determine the script's language: \"{fileName}\"", fileName);
throw new UnknownScriptLanguageException(fileName);
}
}
@@ -76,6 +186,12 @@ public class CallConfig
public interface ICall
{
public HealthCheckResult HealthCheck();
public void Start();
public void Stop();
public bool IsRunning { get; set; }
public CallConfig CallConfig { get; set; }
public DateTime? LastExecution { get; set; }
public DateTime? LastSuccessfulExecution { get; set; }
}
public class IntervalCall : ICall
@@ -83,11 +199,31 @@ public class IntervalCall : ICall
public System.Timers.Timer Timer;
public IScriptable Scriptable;
public ILogger _logger;
public IntervalCall(System.Timers.Timer timer, IScriptable scriptable, ILogger logger)
public bool IsRunning { get; set; }
public CallConfig CallConfig { get; set; }
public DateTime? LastExecution { get; set; }
public DateTime? LastSuccessfulExecution { get; set; }
public IntervalCall(System.Timers.Timer timer, IScriptable scriptable, ILogger logger, CallConfig callConfig)
{
Timer = timer;
Scriptable = scriptable;
_logger = logger;
CallConfig = callConfig;
IsRunning = true;
}
public void Start()
{
Timer.Start();
IsRunning = true;
}
public void Stop()
{
Scriptable.Stop();
Timer.Stop();
IsRunning = false;
}
public HealthCheckResult HealthCheck()
@@ -113,6 +249,24 @@ public class IntervalCall : ICall
public class ScheduleCall : ICall
{
public bool IsRunning { get; set; }
public CallConfig CallConfig { get; set; }
public DateTime? LastExecution { get; set; }
public DateTime? LastSuccessfulExecution { get; set; }
public ScheduleCall(CallConfig callConfig)
{
CallConfig = callConfig;
}
public void Start()
{
}
public void Stop()
{
}
public HealthCheckResult HealthCheck()
{
return HealthCheckResult.Unhealthy(); // Not implemented yet
@@ -121,6 +275,24 @@ public class ScheduleCall : ICall
public class FileUpdateCall : ICall
{
public bool IsRunning { get; set; }
public CallConfig CallConfig { get; set; }
public DateTime? LastExecution { get; set; }
public DateTime? LastSuccessfulExecution { get; set; }
public FileUpdateCall(CallConfig callConfig)
{
CallConfig = callConfig;
}
public void Start()
{
}
public void Stop()
{
}
public HealthCheckResult HealthCheck()
{
return HealthCheckResult.Unhealthy(); // Not implemented yet