Migration to a ServiceWorker for most tasks.
Implementation of basic PatchComplianceTask. First real iteration, basic/raw asf.
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
using OversightService.Services;
|
||||
|
||||
namespace OversightService
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
private OsqueryTaskScheduler? _taskScheduler;
|
||||
private AppConfig? _config;
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
{
|
||||
@@ -11,14 +15,77 @@ namespace OversightService
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("OversightService starting...");
|
||||
|
||||
try
|
||||
{
|
||||
// Load configuration
|
||||
_config = LoadConfig();
|
||||
_logger.LogInformation("Configuration loaded successfully.");
|
||||
|
||||
// Initialize osquery task scheduler
|
||||
InitializeTaskScheduler();
|
||||
_logger.LogInformation("Task scheduler initialized successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to initialize OversightService");
|
||||
throw;
|
||||
}
|
||||
|
||||
// Keep service running
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
}
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
||||
}
|
||||
|
||||
// Cleanup on shutdown
|
||||
_logger.LogInformation("OversightService stopping...");
|
||||
_taskScheduler?.Stop();
|
||||
}
|
||||
|
||||
private AppConfig LoadConfig()
|
||||
{
|
||||
var configPath = Path.Combine(AppContext.BaseDirectory, "config.json");
|
||||
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
throw new FileNotFoundException($"Config file not found at {configPath}");
|
||||
}
|
||||
|
||||
var json = File.ReadAllText(configPath);
|
||||
var config = Newtonsoft.Json.JsonConvert.DeserializeObject<AppConfig>(json);
|
||||
|
||||
if (config == null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to deserialize config.json");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
private void InitializeTaskScheduler()
|
||||
{
|
||||
if (_config == null)
|
||||
{
|
||||
throw new InvalidOperationException("Config must be loaded before initializing scheduler");
|
||||
}
|
||||
|
||||
_taskScheduler = new OsqueryTaskScheduler();
|
||||
|
||||
// Register patch compliance task if enabled
|
||||
if (_config.PatchCompliance.Enabled)
|
||||
{
|
||||
var apiClient = new ApiClient(_config);
|
||||
var patchTask = new PatchComplianceTask(_config, apiClient);
|
||||
_taskScheduler.RegisterTask(patchTask);
|
||||
|
||||
_logger.LogInformation("Patch compliance task registered (interval: {hours} hours)",
|
||||
_config.PatchCompliance.CheckIntervalHours);
|
||||
}
|
||||
|
||||
// Start the scheduler
|
||||
_taskScheduler.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user