Implementation of basic PatchComplianceTask. First real iteration, basic/raw asf.
153 lines
4.5 KiB
C#
153 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace OversightService.Services
|
|
{
|
|
/// <summary>
|
|
/// Manages scheduled osquery-based tasks
|
|
/// Designed to be portable to Windows Service (OversightService) later
|
|
/// </summary>
|
|
public class OsqueryTaskScheduler
|
|
{
|
|
private readonly List<IScheduledTask> _tasks = new();
|
|
private readonly Timer _timer;
|
|
private readonly string _stateFilePath;
|
|
private bool _isRunning;
|
|
|
|
public OsqueryTaskScheduler()
|
|
{
|
|
// Store task state in AppData to persist last run times
|
|
var appDataDir = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"PSG-Oversight"
|
|
);
|
|
Directory.CreateDirectory(appDataDir);
|
|
_stateFilePath = Path.Combine(appDataDir, "task_scheduler_state.json");
|
|
|
|
// Check every minute for tasks that need to run
|
|
_timer = new Timer(CheckAndRunTasks, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register a task with the scheduler
|
|
/// </summary>
|
|
public void RegisterTask(IScheduledTask task)
|
|
{
|
|
_tasks.Add(task);
|
|
LoadTaskState(task);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start the scheduler
|
|
/// </summary>
|
|
public void Start()
|
|
{
|
|
_isRunning = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop the scheduler
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
_isRunning = false;
|
|
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
}
|
|
|
|
private async void CheckAndRunTasks(object? state)
|
|
{
|
|
if (!_isRunning) return;
|
|
|
|
foreach (var task in _tasks.Where(t => t.ShouldRun()))
|
|
{
|
|
try
|
|
{
|
|
await task.ExecuteAsync();
|
|
task.LastRun = DateTime.Now;
|
|
SaveTaskState(task);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError($"Task '{task.TaskName}' failed: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LoadTaskState(IScheduledTask task)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(_stateFilePath)) return;
|
|
|
|
var json = File.ReadAllText(_stateFilePath);
|
|
var states = JsonConvert.DeserializeObject<Dictionary<string, DateTime>>(json);
|
|
|
|
if (states != null && states.TryGetValue(task.TaskName, out var lastRun))
|
|
{
|
|
task.LastRun = lastRun;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError($"Failed to load task state: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void SaveTaskState(IScheduledTask task)
|
|
{
|
|
try
|
|
{
|
|
Dictionary<string, DateTime> states;
|
|
|
|
if (File.Exists(_stateFilePath))
|
|
{
|
|
var json = File.ReadAllText(_stateFilePath);
|
|
states = JsonConvert.DeserializeObject<Dictionary<string, DateTime>>(json)
|
|
?? new Dictionary<string, DateTime>();
|
|
}
|
|
else
|
|
{
|
|
states = new Dictionary<string, DateTime>();
|
|
}
|
|
|
|
states[task.TaskName] = task.LastRun ?? DateTime.Now;
|
|
|
|
var updatedJson = JsonConvert.SerializeObject(states, Formatting.Indented);
|
|
File.WriteAllText(_stateFilePath, updatedJson);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError($"Failed to save task state: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void LogError(string message)
|
|
{
|
|
try
|
|
{
|
|
var logDir = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"PSG-Oversight"
|
|
);
|
|
var logPath = Path.Combine(logDir, "scheduler_error.log");
|
|
File.AppendAllText(logPath, $"[{DateTime.Now}] {message}\n");
|
|
}
|
|
catch
|
|
{
|
|
// Silently fail if we can't write logs
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Stop();
|
|
_timer?.Dispose();
|
|
}
|
|
}
|
|
}
|